Linux 如何在makefile中包含静态库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11344965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include static library in makefile
提问by pythonic
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 ./libminelibrary 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?
我试图在 中包含./libmine库CXXFLAGS,但似乎包含静态库的方法不正确,因为当我编译程序时,我收到许多未定义的引用错误。那么在makefile中包含静态库的正确方法是什么?
采纳答案by ouah
CXXFLAGS = -O3 -o prog -rdynamic -D_GNU_SOURCE -L./libmine
LIBS = libmine.a -lpthread
回答by 0xC0000022L
The -Lmerely gives the path where to find the .aor .sofile. What you're looking for is to add -lmineto the LIBSvariable.
在-L仅仅给出了路径在哪里可以找到.a或.so文件。您正在寻找的是添加-lmine到LIBS变量中。
Make that -static -lmineto force it to pick the static library (in case both static and dynamic library exist).
作出这样-static -lmine迫使它挑静态库(如果静态和动态库中存在)。
回答by Aftnix
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
说你有 main.c fun.c
and a static library libmine.a
和一个静态库 libmine.a
then you can just do in your final link line of the Makefile
那么你可以在 Makefile 的最后一个链接行中做
$(CC) $(CFLAGS) main.o fun.o libmine.a
回答by Jonathan Leffler
Make sure that the -Loption appears ahead of the -loption; the order of options in linker command lines doesmatter, especiallywith static libraries. The -Loption specifies a directory to be searched for libraries (static or shared). The -lnameoption specifies a library which is with libmine.a(static) or libmine.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 file libmine.a. This is convention, not mandatory, but if the name is not in the libmine.aformat, you cannot use the -lminenotation to find it; you must list it explicitly on the compiler (linker) command line.
确保-L选项出现在选项之前-l;链接器命令行中选项的顺序很重要,尤其是对于静态库。该-L选项指定要搜索库(静态或共享)的目录。该-lname选项指定了一个与libmine.a(静态)或libmine.so(在大多数 Unix 变体上共享,但 Mac OS X 使用.dylib而 HP-UX 过去使用.sl)的库。通常,静态库将位于文件中libmine.a。这是约定,不是强制性的,但如果名称不在libmine.a格式中,则无法使用-lmine符号查找;您必须在编译器(链接器)命令行上明确列出它。
The -L./libmineoption says "there is a sub-directory called libminewhich can be searched to find libraries". I can see three possibilities:
该-L./libmine选项说“有一个名为的子目录libmine,可以搜索它以查找库”。我可以看到三种可能性:
- 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. - 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.
- 您有这样一个包含 的子目录
libmine.a,在这种情况下,您还需要添加-lmine到链接器行(在引用库的目标文件之后)。 - 您有一个
libmine静态存档文件,在这种情况下,您只需将其列为前面./libmine没有 no的文件-L。 - 您
libmine.a在当前目录中有一个要提取的文件。您可以编写./libmine.a或-L . -lmine两者都应该找到该库。

