如何在makefile中包含静态库?

时间:2021-09-19 22:49:46

I have the following makefile

我有以下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?

我正在尝试在CXXFLAGS中包含./libmine库,但它似乎不是包含静态库的正确方法,因为当我编译程序时,我得到了许多未定义的引用错误。那么,在makefile中包含静态库的正确方法是什么呢?

4 个解决方案

#1


14  

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

#2


35  

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

说你有主。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

#3


11  

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 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相关的库。(静态)或libmine。所以(在大多数Unix版本*享,但是Mac OS X使用.dylib和HP-UX用于使用.sl)。通常,静态库将位于libmine.a文件中。这是惯例,不是强制性的,但如果名字不在libmine中。一种格式,你不能用-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的子目录,可以搜索查找库”。我可以看到三种可能性:

  1. 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).
  2. 您有这样一个包含libmine的子目录。在这种情况下,您还需要将-lmine添加到链接器行(在引用库的对象文件之后)。
  3. 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.
  4. 您有一个libmine文件,它是一个静态存档,在这种情况下,您只需将它作为一个文件列出。
  5. 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.
  6. 你有一个libmine文件。在当前目录中,您希望拾取。你可以写。/libmine。或- l。-lmine和他们都应该找到图书馆。

#4


8  

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的路径。你要找的是把-lmine添加到LIBS变量中。

Make that -static -lmine to force it to pick the static library (in case both static and dynamic library exist).

让这个-静态-lmine强制它选择静态库(如果存在静态库和动态库)。

#1


14  

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

#2


35  

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

说你有主。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

#3


11  

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 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相关的库。(静态)或libmine。所以(在大多数Unix版本*享,但是Mac OS X使用.dylib和HP-UX用于使用.sl)。通常,静态库将位于libmine.a文件中。这是惯例,不是强制性的,但如果名字不在libmine中。一种格式,你不能用-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的子目录,可以搜索查找库”。我可以看到三种可能性:

  1. 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).
  2. 您有这样一个包含libmine的子目录。在这种情况下,您还需要将-lmine添加到链接器行(在引用库的对象文件之后)。
  3. 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.
  4. 您有一个libmine文件,它是一个静态存档,在这种情况下,您只需将它作为一个文件列出。
  5. 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.
  6. 你有一个libmine文件。在当前目录中,您希望拾取。你可以写。/libmine。或- l。-lmine和他们都应该找到图书馆。

#4


8  

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的路径。你要找的是把-lmine添加到LIBS变量中。

Make that -static -lmine to force it to pick the static library (in case both static and dynamic library exist).

让这个-静态-lmine强制它选择静态库(如果存在静态库和动态库)。