Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How can link external libraries from the cmd?

  1. Dec 12, 2016 #1
    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 (C):
    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] [Broken]
    Plug '[PLAIN]https://github.com/vim-syntastic/syntastic.git'[/PLAIN] [Broken]
    Plug '[PLAIN]https://github.com/scrooloose/nerdtree.git'[/PLAIN] [Broken]
    Plug '[PLAIN]https://github.com/ervandew/supertab.git'[/PLAIN] [Broken]
    call plug#end()
     
     
    Last edited by a moderator: May 8, 2017
  2. jcsd
  3. Dec 12, 2016 #2

    jedishrfu

    Staff: Mentor

    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
     
  4. Dec 12, 2016 #3
    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.
     
  5. Dec 12, 2016 #4
    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.
     
  6. Dec 16, 2016 #5
    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 (Text):
    main.o : glew.h glfw.g
          g++ #linking command# main.cpp
    Please help me out.
     
  7. Dec 16, 2016 #6

    jtbell

    User Avatar

    Staff: Mentor

    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: Dec 16, 2016
  8. Dec 16, 2016 #7
    Thanks it does work now.
    Code (C):
    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).
     
  9. Dec 18, 2016 #8

    jtbell

    User Avatar

    Staff: Mentor

    Thanks for posting your solution! The "-L." adds the current directory to the list of "standard library directories" and lets you put the required libraries there, provided that their names begin with "lib" as expected.
     
  10. Dec 18, 2016 #9
    Thanks I forgot to mention that.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted