How can link external libraries from the cmd?

AI Thread Summary
To link external libraries like GLEW and GLFW in a C++ project using Vim and the MinGW compiler, it is essential to utilize a Makefile. The Makefile allows for the specification of library dependencies and compilation commands. The basic structure involves defining targets and linking commands, such as using `g++` with the `-L` option to specify the library directory and `-l` to link against the libraries. For example, `g++ -o output main.o -L. -lglfw3 -lopengl32 -lglu32 -lgdi32` compiles the program while linking the necessary libraries. If libraries are located in non-standard directories, their paths should be provided directly. Learning to write a Makefile can streamline the process, especially for multi-file projects. Resources like the GNU Make documentation can provide guidance on creating effective Makefiles.
Buffu
Messages
849
Reaction score
146
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 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 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 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 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.
 
Back
Top