How can link external libraries from the cmd?

In summary, you need to install the vim compiler, and then use the "g++" command to link external libraries.
  • #1
Buffu
849
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
  • #2
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
  • #3
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.
 
  • #4
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
  • #5
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.
 
  • #6
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
  • #7
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).
 
  • #8
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
  • #9
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.
 

1. How do I specify the path for an external library in the cmd?

To specify the path for an external library in the cmd, you can use the "-L" flag followed by the path to the directory where the library is located. For example, if the library is located in the "lib" folder within your project directory, you can use the command "gcc -L/lib myprogram.c" to link the library.

2. Can I link multiple external libraries in one command?

Yes, you can link multiple external libraries in one command by using the "-l" flag followed by the name of each library. For example, if you want to link the libraries "lib1" and "lib2", you can use the command "gcc -l1 -l2 myprogram.c" to link both libraries.

3. How do I know if the external library has been successfully linked?

If the external library has been successfully linked, you will not receive any error messages when compiling your program. You can also use the command "ldd program_name" to check the dependencies of your program and see if the external library is listed.

4. What if the external library is not located in the same directory as my program?

If the external library is not located in the same directory as your program, you can use the "-I" flag followed by the path to the directory where the library is located. This will add the directory to the list of directories searched for libraries. For example, if the library is located in the "include" folder within your project directory, you can use the command "gcc -I/include myprogram.c" to link the library.

5. Can I link external libraries from different programming languages in the cmd?

Yes, you can link external libraries from different programming languages in the cmd as long as they are compatible with the language you are using. For example, you can use the "-l" flag to link a C library in a C++ program, but you cannot link a Java library in a C program. It is important to check the documentation of the library to ensure compatibility with your program's language.

Back
Top