39

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)

Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97

5 Answers5

58
cc -o yourprog yourprog.c -lstatic

or

cc -o yourprog yourprog.c libstatic.a
dajobe
  • 4,938
  • 35
  • 41
  • i tried the 2nd one and it works. thanks. i also could be able to configure for code::blocks to work too. :-) –  Nov 10 '09 at 08:05
11

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
Jacob
  • 34,255
  • 14
  • 110
  • 165
  • thank you. finally i got it done on codeblocks and I figured out that #include .h file not necessary the same name as the .a file. –  Nov 10 '09 at 08:03
9

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

gcc -o myapp main.c -L. -lstatic
pseudomuto
  • 266
  • 4
  • 3
4
gcc -I. -o jvct jvct.c libjvc.a
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Hafiz Muhammad Shafiq
  • 8,168
  • 12
  • 63
  • 121
4

To link purely statically, use -static

cc -static yourprogram.c libstatic.a
msc
  • 33,420
  • 29
  • 119
  • 214
  • 2
    Note that this will cause the compiler to statically link to _all_ libraries, including libc, and will produce statically linked binary. It might be what you want (or not). – WGH Mar 15 '17 at 13:40