How to include static library in makefile-Collection of common programming errors
-
use
LDFLAGS= -L -l
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
-
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 withlibmine.a
(static) orlibmine.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 filelibmine.a
. This is convention, not mandatory, but if the name is not in thelibmine.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 calledlibmine
which can be searched to find libraries”. I can see three possibilities:- 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). - 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. Aga - 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.
- You have such a sub-directory containing
-
The
-L
merely gives the path where to find the.a
or.so
file. What you’re looking for is to add-lmine
to theLIBS
variable.Make that
-static -lmine
to force it to pick the static library (in case both static and dynamic library exist).
Originally posted 2013-11-09 22:41:13.