How to include static library in makefile

176,761

Solution 1

CXXFLAGS = -O3 -o prog -rdynamic -D_GNU_SOURCE -L./libmine
LIBS = libmine.a -lpthread 

Solution 2

use

LDFLAGS= -L<Directory where the library resides> -l<library name>

Like :

LDFLAGS = -L. -lmine

for ensuring static compilation you can also add

LDFLAGS = -static

Or you can just get rid of the whole library searching, and link with with it directly.

say you have main.c fun.c

and a static library libmine.a

then you can just do in your final link line of the Makefile

$(CC) $(CFLAGS) main.o fun.o libmine.a

Solution 3

Make sure that the -L option appears ahead of the -l option; the order of options in linker command lines does matter, especially with static libraries. The -L option specifies a directory to be searched for libraries (static or shared). The -lname option specifies a library which is with libmine.a (static) or libmine.so (shared on most variants of Unix, but Mac OS X uses .dylib and HP-UX used to use .sl). Conventionally, a static library will be in a file libmine.a. This is convention, not mandatory, but if the name is not in the libmine.a format, you cannot use the -lmine notation to find it; you must list it explicitly on the compiler (linker) command line.

The -L./libmine option says "there is a sub-directory called libmine which can be searched to find libraries". I can see three possibilities:

  1. You have such a sub-directory containing libmine.a, in which case you also need to add -lmine to the linker line (after the object files that reference the library).
  2. You have a file libmine that is a static archive, in which case you simply list it as a file ./libmine with no -L in front.
  3. You have a file libmine.a in the current directory that you want to pick up. You can either write ./libmine.a or -L . -lmine and both should find the library.

Solution 4

The -L merely gives the path where to find the .a or .so file. What you're looking for is to add -lmine to the LIBS variable.

Make that -static -lmine to force it to pick the static library (in case both static and dynamic library exist).

Addition: Suppose the path to the file has been conveyed to the linker (or compiler driver) via -L you can also specifically tell it to link libfoo.a by giving -l:libfoo.a. Note that in this case the name includes the conventional lib-prefix. You can also give a full path this way. Sometimes this is the better method to "guide" the linker to the right location.

Share:
176,761
pythonic
Author by

pythonic

Updated on January 29, 2021

Comments

  • pythonic
    pythonic over 3 years

    I have the following makefile

    CXXFILES = pthreads.cpp 
    
    CXXFLAGS = -O3 -o prog -rdynamic -D_GNU_SOURCE -L./libmine
    LIBS = -lpthread -ldl
    
    all:
        $(CXX) $(CXXFILES) $(LIBS) $(CXXFLAGS)
    
    clean:
        rm -f prog *.o
    

    I am trying to include the ./libmine library within CXXFLAGS, but it seems like it is not the right way to include a static library, because when I compile the program, I get many undefined references error. So what is actually the right way to include a static library in the makefile?

  • Blake
    Blake over 9 years
    I read about the -static flag, but I'm concerned that -static will make all the referenced libs static, when I just want one to be. Will -static mark only the proceeding reference as static?
  • 0xC0000022L
    0xC0000022L over 9 years
    @Cokemonkey11: any library after the -static should prefer the static version of the library, yes.
  • Blake
    Blake over 9 years
    @0xC00000022L so if I do -static -lfoo -bar, both foo and bar will be static? I want only foo to be static - that's what I was trying to ask. Edit: I want only foo to be explicitly static, whereas I want bar to have standard linking behavior.
  • 0xC0000022L
    0xC0000022L over 9 years
    @Cokemonkey11: it will prefer the static version with -lbar behind -static. But if there is no static lib, it'll use what it finds, to my knowledge. Depends on the linker scripts you use with the GCC driver, though.
  • ouah
    ouah over 7 years
    OP added -L./libmine in gcc compilation and linking line but didn't add libmine.a. -L./libmine tells gcc to look into ./libmine directory to search for the objects files and libraries. Adding libmine.a tells gcc you want to link your executable with libmine.a library.
  • fchen
    fchen over 6 years
    To clarify, LIBS = libmine.a does not work with -L./libmine. It seems absolute path is necessary. Use LIBS = -lmine, it will look for either libmine.a or libmine.so (tested with gcc 5.4.0)