Linux 用 .a 文件编译 c 文件的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10454263/
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
command to compile c files with .a files
提问by pythonic
I have several .c files and one .a object file. What command with gcc should I use to compile them to one exe file? If we use a makefile, how will it look like?
我有几个 .c 文件和一个 .a 目标文件。我应该使用 gcc 的什么命令将它们编译为一个 exe 文件?如果我们使用 makefile,它会是什么样子?
采纳答案by TJD
The .a file is a library, already compiled. You compile your .c file to a .o, then you use the linker to link your .o with the .a to produce an executable.
.a 文件是一个库,已经编译。您将 .c 文件编译为 .o,然后使用链接器将 .o 与 .a 链接以生成可执行文件。
回答by Chris Stratton
For simple cases you can probably do this:
对于简单的情况,您可能可以这样做:
gcc -o maybe.exe useful.a something.c
Makefiles for non-trivial projects usually first invoke gcc to compile each .c file to a .o object.
非平凡项目的 Makefile 通常首先调用 gcc 将每个 .c 文件编译为 .o 对象。
gcc -c something.c
Then they invoke the linker (these days often using gcc as a wrapper for it) with a list of .o and .a files to link into an output executable.
然后他们使用 .o 和 .a 文件列表调用链接器(这些天经常使用 gcc 作为它的包装器)以链接到输出可执行文件。
gcc -o maybe.exe useful.a something.o
Note also that for most installed libraries, it's typical not to explicitly specify the .a file but instead to say -lhandy which would be short for "try to find something called libhandy.a in the configured (or specified with -L) search directories"
另请注意,对于大多数已安装的库,通常不明确指定 .a 文件,而是说 -lhandy,它是“尝试在配置的(或使用 -L 指定的)搜索目录中找到名为 libhandy.a 的东西的缩写” ”
回答by 0x90
*.a
is a static library and not dynamic (*.dll
in windows and *.so
in linux)
*.a
是一个静态库而不是动态库(*.dll
在 windows 和*.so
linux 中)
gcc -L<here comes the library path> -l<library name>
for example for the file you have libname.a in the current path you should use:
例如,对于当前路径中包含 libname.a 的文件,您应该使用:
gcc *.c -L. -lname -o myprogram.o
from the man(put man gcc in the shell command prompt)
来自man(将 man gcc 放在 shell 命令提示符中)
You can mix options and other arguments. For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.
您可以混合使用选项和其他参数。在大多数情况下,您使用的顺序并不重要。当您使用多个同类选项时,顺序很重要;例如,如果您多次指定 -L,将按指定的顺序搜索目录。此外,-l 选项的位置也很重要。