C语言 C:使用 Makefile 创建静态库和链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31421616/
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
C: Creating static library and linking using a Makefile
提问by user3337714
I am trying to understand static and shared Libraries.
我正在尝试了解静态和共享库。
I want to do the following to create a makefile that does separate compilation and linking such that a static library is created and linked in forming the final static executable.
我想执行以下操作来创建一个 makefile,该文件执行单独的编译和链接,以便创建和链接静态库以形成最终的静态可执行文件。
I have the following code for the Makefile, but I am getting the following error
我有以下 Makefile 代码,但出现以下错误
Makefile:13: *** missing separator. Stop.
Makefile:13: *** missing separator. Stop.
But I am also trying to understand how to actually link/create libraries.
但我也试图了解如何实际链接/创建库。
If I run the commands after line 12in the terminal they work, but not in the makefile.
如果我line 12在终端中运行这些命令,它们会起作用,但不会在 makefile 中运行。
myProgram: main.o addSorted.o freeLinks.o
gcc -lm -o myProgram main.o addSorted.o freeLinks.o
main.o: main.c
gcc -O -c -lm main.c main.h
addSorted.o: addSorted.c addSorted.h
gcc -O -c -lm addSorted.c
freeLinks.o: freeLinks.c freeLinks.h
gcc -O -c -lm freeLinks.c
ar rc libmylib.a main.o addSorted.o freeLinks.o //Error Line
ranlib libmylib.a
gcc -o foo -L. -lmylib foo.o
clean:
rm -f myProgram main.o addSorted.o freeLinks.o
Also, if you can assist in improving the code, I would really appreciate it.
另外,如果您能协助改进代码,我将不胜感激。
回答by Sergey
Try this:
尝试这个:
all: myProgram
myProgram: main.o libmylib.a #libmylib.a is the dependency for the executable
gcc -lm -o myProgram main.o -L. -lmylib
main.o: main.c
gcc -O -c main.c main.h
addSorted.o: addSorted.c addSorted.h
gcc -O -c addSorted.c
freeLinks.o: freeLinks.c freeLinks.h
gcc -O -c freeLinks.c
libmylib.a: addSorted.o freeLinks.o #let's link library files into a static library
ar rcs libmylib.a addSorted.o freeLinks.o
libs: libmylib.a
clean:
rm -f myProgram *.o *.a *.gch #This way is cleaner than your clean
This set of rules first compiles all files, then it makes library (libmylib.a) target and uses it's artifact to link the executable. I also added separate redundant target form making libs only. Needed files:
这组规则首先编译所有文件,然后它使库 (libmylib.a) 成为目标并使用它的工件来链接可执行文件。我还添加了单独的冗余目标表单,仅制作库。需要的文件:
user@host> ls
addSorted.c addSorted.h freeLinks.c freeLinks.h main.c main.h Makefile
回答by user464502
A makefile is not a shell script. It's a configuration file for an expert system. Specifically an expert system that knows, if you tell it, how to efficiently create files and their dependencies with a minimum of re-making files that don't need to be remade.
makefile 不是 shell 脚本。它是专家系统的配置文件。特别是一个专家系统,如果您告诉它,它知道如何有效地创建文件及其依赖项,并且最少需要重新制作不需要重新制作的文件。
If you look at the first rule you have:
如果你看第一条规则,你有:
myProgram: main.o addSorted.o freeLinks.o
gcc -lm -o myProgram main.o addSorted.o freeLinks.o
that tells the system how to make a file called "myProgram" if it decides that it needs to do that. The parts after the colon are files that myProgram needs. If they aren't there, or make decides they are out of date, make will try to find some recipe that can be used to create or update them. Once all that is done, make then executes the "gcc ..." line and assumes that will create or update myProgram.
如果它决定需要这样做,它会告诉系统如何制作一个名为“myProgram”的文件。冒号后面的部分是 myProgram 需要的文件。如果它们不存在,或者 make 认为它们已经过时,make 将尝试找到一些可用于创建或更新它们的配方。完成所有操作后,make 将执行“gcc ...”行并假设这将创建或更新 myProgram。
The ar and ranlib lines you have don't match the needed syntax for a makefile rule. From the look of them, they appear to be a recipe for making libmylib.a. If you put them into the syntax make needs to say that, you get:
您拥有的 ar 和 ranlib 行与 makefile 规则所需的语法不匹配。从它们的外观来看,它们似乎是制作 libmylib.a 的秘诀。如果将它们放入 make 需要说的语法中,您将得到:
libmylib.a: main.o addSorted.o freeLinks.o
ar rcu libmylib.a main.o addSorted.o freeLinks.o
ranlib libmylib.a
myProgram should depend on the library itself, rather than the contents of the library, and it is best to put the library options at the end:
myProgram 应该依赖于库本身,而不是库的内容,最好将库选项放在最后:
myProgram: libmylib.a
gcc -o myProgram libmylib.a -lm
if you like, you can use an option to tell gcc to look for libraries in the current directory:
如果你愿意,你可以使用一个选项告诉 gcc 在当前目录中查找库:
gcc -L. -o myProgram main.o -lmylib -lm
There are also makefile variables that can help you not have to repeat so much, so I would write the first rule as:
还有 makefile 变量可以帮助您不必重复太多,所以我将第一条规则写为:
myProgram: libmylib.a
gcc -L. -o $@ -lmylib -lm
however, it is unlikely that main.o should actually be part of the library, so:
但是,main.o 不太可能真正成为库的一部分,因此:
myProgram: main.o libmylib.a
gcc -L. -o $@ $< -lmylib -lm
and the library rule as:
和图书馆规则为:
libmylib.a: addSorted.o freeLinks.o
ar rcu $@ $+
ranlib $@
the $+ here means "all of the dependency file names".
这里的 $+ 表示“所有依赖文件名”。
Finally, if you want gcc to make an actual static executable, and not just use the library you made, you need to pass the '-static' option to gcc.
最后,如果您希望 gcc 生成一个实际的静态可执行文件,而不仅仅是使用您创建的库,您需要将 '-static' 选项传递给 gcc。

