How can link external libraries from the cmd?

Click For Summary

Discussion Overview

The discussion focuses on linking external libraries, specifically GLEW and GLFW, while using the Vim editor on a Linux system with the MinGW compiler for C++ programming. Participants explore methods for integrating these libraries into a project, including the use of makefiles and command-line instructions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant expresses familiarity with linking libraries in IDEs but seeks guidance on doing so from the command line in Vim.
  • Another participant suggests using makefiles to manage library dependencies, indicating that Vim can call the MinGW make command.
  • Some participants mention the utility of makefiles for multi-file projects and provide links to resources for configuring Vim with MinGW.
  • There is a request for help in writing a makefile, with a participant expressing a desire to avoid spending too much time on it.
  • Several participants discuss the specifics of linking libraries using the g++ command, including the use of paths and the -l flag for standard libraries.
  • A participant shares a makefile example that includes linking commands and expresses gratitude for the assistance received.
  • Another participant notes the importance of the -L flag in adding the current directory to the library search path.

Areas of Agreement / Disagreement

Participants generally agree on the importance of makefiles for managing dependencies, but there is no consensus on the best approach to writing them or the specifics of linking libraries with MinGW.

Contextual Notes

Some participants express uncertainty about the exact library file extensions used by MinGW and the specifics of writing makefiles, indicating a reliance on external resources for learning.

Who May Find This Useful

Individuals interested in C++ programming using Vim on Linux, particularly those looking to integrate external libraries and manage project dependencies effectively.

Buffu
Messages
851
Reaction score
147
I am familiar with linking external libraries in IDE like Eclipse, Netbeans etc. But in my college they use use vim for programming on Linux. To learn more about vim editor I installed it on my home computer.I can compile basic C++ programs on it with one or two .cpp/.h files. I think it is awesome for C++ programming and now I want to move my OpenGL project from Visual Studios to vim. Before I did anything I released I don't know how to link external libraries (GLEW and GLFW) from cmd.

Please tell how can I link external libraries from cmd, if possible from vim (some vim script or plugins) ?
I am using Mingw compiler.

This is my current vimrc file,

Code:
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin

set diffexpr=MyDiff()
function MyDiff()
  let opt = '-a --binary '
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  let arg1 = v:fname_in
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  let eq = ''
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      let cmd = '""' . $VIMRUNTIME . '\diff"'
      let eq = '"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction

:cd E:\C++\
:set tabstop=4
:set shiftwidth=4
:set expandtab

call plug#begin('~/vimfiles/bundle')

Plug '[PLAIN]https://github.com/WolfgangMehner/vim-plugins.git'[/PLAIN] 
Plug '[PLAIN]https://github.com/vim-syntastic/syntastic.git'[/PLAIN] 
Plug '[PLAIN]https://github.com/scrooloose/nerdtree.git'[/PLAIN] 
Plug '[PLAIN]https://github.com/ervandew/supertab.git'[/PLAIN] 
call plug#end()
 
Last edited by a moderator:
Computer science news on Phys.org
Does your project use make files? If so that's where you would add the library dependencies as I think vim just calls the mingw make command. If not it might be a good time to start using them. They are especially useful for multi-module ie multi-file programs.

This website has a discussion on how to configure vim so that it can call the mingw32-make.exe command on your system:

http://stackoverflow.com/questions/2423478/how-to-use-mingw-make-with-vim-on-windows
 
  • Like
Likes   Reactions: Buffu
jedishrfu said:
Does your project use make files? If so that's where you would add the library dependencies as I think vim just calls the mingw make command. If not it might be a good time to start using them. They are especially useful for multi-module ie multi-file programs.

This website has a discussion on how to configure vim so that it can call the mingw32-make.exe command on your system:

http://stackoverflow.com/questions/2423478/how-to-use-mingw-make-with-vim-on-windows
I know make files can be executed directly but I have no idea how to write a make file neither I can find any good resource on make files.
I don't want to spend much time on make files, I just want my project to work.
 
Buffu said:
I know make files can be executed directly but I have no idea how to write a make file neither I can find any good resource on make files.
I don't want to spend much time on make files, I just want my project to work.

I think the best source for learning about Make is the documentation of Gnu Make at https://www.gnu.org/software/make/manual/make.html .
Start by reading chapters 3 and 4. Most of your make files will look practically the same. So, if you learn write a simple Make file you can then easily extend it.
 
  • Like
Likes   Reactions: Buffu
jedishrfu said:
Does your project use make files? If so that's where you would add the library dependencies as I think vim just calls the mingw make command. If not it might be a good time to start using them. They are especially useful for multi-module ie multi-file programs.

This website has a discussion on how to configure vim so that it can call the mingw32-make.exe command on your system:

http://stackoverflow.com/questions/2423478/how-to-use-mingw-make-with-vim-on-windows

eys_physics said:
I think the best source for learning about Make is the documentation of Gnu Make at https://www.gnu.org/software/make/manual/make.html .
Start by reading chapters 3 and 4. Most of your make files will look practically the same. So, if you learn write a simple Make file you can then easily extend it.

Ok I think I got how to make and use make files.
But I still need g++ command for linking libraries because that is what I have to write in recipe part of make.

Code:
main.o : glew.h glfw.g
      g++ #linking command# main.cpp

Please help me out.
 
How you link the library depends on where it's located. If it's in a file named foo.a (.a being the standard extension for libraries on many/most Unix-ish systems), in your current directory (folder) along with main.cpp:

g++ main.cpp foo.a

compiles main.cpp and links the resulting object code with the library foo.a. If foo.a is in some other folder, give the path to it:

g++ main.cpp /path/to/foo.a

Special case: if the library is in one of your system's "standard" directories for libraries (maybe /usr/lib, /usr/local/lib, etc.) and has a name beginning with 'lib', e.g. libfoo.a, then you can use

g++ main.cpp -lfoo

which is equivalent to

g++ main.cpp /usr/lib/libfoo.a (or maybe /usr/local/lib/libfoo.a, or in some other standard directory)

[added] I've never used Mingw so I don't know whether it uses .a or some other extension for libraries.
 
Last edited:
  • Like
Likes   Reactions: Buffu
jtbell said:
How you link the library depends on where it's located. If it's in a file named foo.a (.a being the standard extension for libraries on many/most Unix-ish systems), in your current directory (folder) along with main.cpp:

g++ main.cpp foo.a

compiles main.cpp and links the resulting object code with the library foo.a. If foo.a is in some other folder, give the path to it:

g++ main.cpp /path/to/foo.a

Special case: if the library is in one of your system's "standard" directories for libraries (maybe /usr/lib, /usr/local/lib, etc.) and has a name beginning with 'lib', e.g. libfoo.a, then you can use

g++ main.cpp -lfoo

which is equivalent to

g++ main.cpp /usr/lib/libfoo.a (or maybe /usr/local/lib/libfoo.a, or in some other standard directory)

[added] I've never used Mingw so I don't know whether it uses .a or some other extension for libraries.

Thanks it does work now.
Code:
pig : main.o
    g++ -o pig.exe main.o -L. -lglfw3 -lopengl32 -lglu32 -lgdi32

main.o : E:\DoesWork\glfw3.h
    g++ -c main.cpp -L. -lglfw3 -lopengl32 -lglu32 -lgdi32

clean :
    rm main.o edit.exe

This is the make file if somebody needed help with this in future.(low quality make file).
 
Thanks for posting your solution! The "-L." adds the current directory to the list of "standard library directories" and let's you put the required libraries there, provided that their names begin with "lib" as expected.
 
  • Like
Likes   Reactions: Buffu
jtbell said:
The "-L." adds the current directory to the list of "standard library directories" and let's you put the required libraries there, provided that their names begin with "lib" as expected.
Thanks I forgot to mention that.