command to compile c files with .a files

假如想象 提交于 2019-12-10 12:45:13

问题


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?


回答1:


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.




回答2:


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.

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.

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"




回答3:


*.a is a static library and not dynamic (*.dll in windows and *.so in 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:

gcc *.c -L. -lname -o myprogram.o

from the man (put man gcc in the shell command prompt)

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.



来源:https://stackoverflow.com/questions/10454263/command-to-compile-c-files-with-a-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!