How to include static library in makefile

pythonic picture pythonic · Jul 5, 2012 · Viewed 154.9k times · Source

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?

Answer

Aftnix picture Aftnix · Jul 5, 2012

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