How to link to a static library in C?

2019-01-10 17:11发布

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)

5条回答
戒情不戒烟
2楼-- · 2019-01-10 17:50

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楼-- · 2019-01-10 17:54
cc -o yourprog yourprog.c -lstatic

or

cc -o yourprog yourprog.c libstatic.a
查看更多
闹够了就滚
4楼-- · 2019-01-10 17:55

To link purely statically, use -static

cc -static yourprogram.c libstatic.a
查看更多
倾城 Initia
5楼-- · 2019-01-10 18:08

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

gcc -o myapp main.c -L. -lstatic
查看更多
Juvenile、少年°
6楼-- · 2019-01-10 18:08
gcc -I. -o jvct jvct.c libjvc.a
查看更多
登录 后发表回答