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 ./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?
我试图在 中包含./libmine
库CXXFLAGS
,但似乎包含静态库的方法不正确,因为当我编译程序时,我收到许多未定义的引用错误。那么在makefile中包含静态库的正确方法是什么?
采纳答案by ouah
CXXFLAGS = -O3 -o prog -rdynamic -D_GNU_SOURCE -L./libmine
LIBS = libmine.a -lpthread
回答by 0xC0000022L
The -L
merely gives the path where to find the .a
or .so
file. What you're looking for is to add -lmine
to the LIBS
variable.
在-L
仅仅给出了路径在哪里可以找到.a
或.so
文件。您正在寻找的是添加-lmine
到LIBS
变量中。
Make that -static -lmine
to 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 -L
option appears ahead of the -l
option; the order of options in linker command lines doesmatter, especiallywith static libraries. The -L
option specifies a directory to be searched for libraries (static or shared). The -lname
option specifies a library which is with libmine.a
(static) or libmine.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 file libmine.a
. This is convention, not mandatory, but if the name is not in the libmine.a
format, you cannot use the -lmine
notation 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./libmine
option says "there is a sub-directory called libmine
which 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-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. - 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.
- 您有这样一个包含 的子目录
libmine.a
,在这种情况下,您还需要添加-lmine
到链接器行(在引用库的目标文件之后)。 - 您有一个
libmine
静态存档文件,在这种情况下,您只需将其列为前面./libmine
没有 no的文件-L
。 - 您
libmine.a
在当前目录中有一个要提取的文件。您可以编写./libmine.a
或-L . -lmine
两者都应该找到该库。