How to link to a static library in C?

若如初见. 提交于 2019-11-26 12:39:57

问题


I use code::blocks to compile my static library. The output result is a libstatic.a file. Now, how do I link to my library to use functions that were compiled?

(I tried to use #include \"libstatic.a\" but my project doesn\'t compile)


回答1:


cc -o yourprog yourprog.c -lstatic

or

cc -o yourprog yourprog.c libstatic.a



回答2:


You should #include "libstatic.h", i.e. use the appropriate header file in your code (that's why your code doesn't compile) and include the path to your libstatic.a in the linker options as one of your input libraries.

This webpage has some examples on linking to a static library, e.g.

gcc -I. -o jvct jvct.c libjvc.a



回答3:


I had to set the library path in my makefile. For this case you could use:

gcc -o myapp main.c -L. -lstatic



回答4:


gcc -I. -o jvct jvct.c libjvc.a



回答5:


To link purely statically, use -static

cc -static yourprogram.c libstatic.a


来源:https://stackoverflow.com/questions/1705961/how-to-link-to-a-static-library-in-c

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