简体   繁体   English

如何链接到C中的静态库?

[英]How to link to a static library in C?

I use code::blocks to compile my static library. 我使用code :: blocks编译我的静态库。 The output result is a libstatic.a file. 输出结果是一个libstatic.a文件。 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) (我尝试使用#include“ libstatic.a”,但我的项目无法编译)

cc -o yourprog yourprog.c -lstatic

要么

cc -o yourprog yourprog.c libstatic.a

You should #include "libstatic.h" , ie 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. 您应该#include "libstatic.h" ,即在代码中使用适当的头文件( 就是代码无法编译原因),并将libstatic.a的路径作为输入库之一libstatic.a在链接器选项中。

This webpage has some examples on linking to a static library, eg 网页上有一些链接到静态库的示例,例如

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

I had to set the library path in my makefile. 我必须在我的makefile中设置库路径。 For this case you could use: 对于这种情况,您可以使用:

gcc -o myapp main.c -L. -lstatic
gcc -I. -o jvct jvct.c libjvc.a

要纯粹静态链接,请使用-static

cc -static yourprogram.c libstatic.a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM