How to include static library in makefile-Collection of common programming errors
-
use
LDFLAGS= -L -lLike :
LDFLAGS = -L. -lminefor ensuring static compilation you can also add
LDFLAGS = -staticOr 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
-Loption appears ahead of the-loption; the order of options in linker command lines does matter, especially with static libraries. The-Loption specifies a directory to be searched for libraries (static or shared). The-lnameoption specifies a library which is withlibmine.a(static) orlibmine.so(shared on most variants of Unix, but Mac OS X uses.dyliband 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.aformat, you cannot use the-lminenotation to find it; you must list it explicitly on the compiler (linker) command line.The
-L./libmineoption says “there is a sub-directory calledlibminewhich 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-lmineto the linker line (after the object files that reference the library). - You have a file
libminethat is a static archive, in which case you simply list it as a file./libminewith no-Lin front. Aga - You have a file
libmine.ain the current directory that you want to pick up. You can either write./libmine.aor-L . -lmineand both should find the library.
- You have such a sub-directory containing
-
The
-Lmerely gives the path where to find the.aor.sofile. What you’re looking for is to add-lmineto theLIBSvariable.Make that
-static -lmineto force it to pick the static library (in case both static and dynamic library exist).
Originally posted 2013-11-09 22:41:13.