How to include static library in makefile

34

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?

This question is tagged with c linux gcc x86-64

~ Asked on 2012-07-05 13:06:42

The Best Answer is


19

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

~ Answered on 2012-07-05 13:32:17


52

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

~ Answered on 2012-07-05 13:31:11


Most Viewed Questions: