0

my files dependencies a.c, a.h, b.c, b.h, c.c, c.h, are like that:

// a.c
#include "a.h"
#include "b.h"
#include "c.h"
#include <lib>

// b.c
#include "b.h"
#include <lib>

// c.c
#include "c.h"
#include <lib>

I have no main() function. I need to create out.o and someone else will use this with main in his program (he'll have to write #include "a.h" to use the functions I wrote there).

so I wrote

gcc -std=c99 -c c.c -o c.o -llib

gcc -std=c99 -c b.c -o b.o -llib

gcc -std=c99 -c a.c -o a.o -llib

but when I try to combine them using

gcc -o out.o a.o b.o c.o -llib

I get many errors like relocation 18 has invalid symbol index 13 and in the end undefined reference to 'main'.

How can I create what I need? `

Mano Mini
  • 607
  • 4
  • 15

1 Answers1

2

I think you want to create a library out of your .o files.

ar crf yourlib.a a.o b.o c.o

then, other people can compile their programs by doing, for example:

gcc -o main main.c yourlib.a

zsram
  • 366
  • 2
  • 7
  • and how can I include the `lib` in this static library? using `ar crf yourlib.a a.o b.o c.o -llib` hasn't work – Mano Mini Dec 27 '16 at 19:20
  • @ManoMini Static libraries are only collections (or archives, that's what the `.a` suffix stands for) of object files. You can't *link* a static library, to create one that contains itself *and* all its dependencies. That's not how static libraries work. If your static library depends on another library, the user of your library have to link with your dependencies as well. You simply can't create a stand-alone static library. – Some programmer dude Dec 27 '16 at 19:28
  • If what you call "lib" is a static library, then yes, you can extract elements (.o files) from your, say, lib.a this way: ar -x lib.a... then you will have all those .o in your directory. Then you can do the same above, now including the list of .o files extracted from lib.a with ar -x. You can use *.o if they are too many. – zsram Dec 27 '16 at 19:31
  • @zsram no. it's like `cmath` when you have to use `-lm` in gcc – Mano Mini Dec 27 '16 at 19:34
  • For example in linux, libm exists both in static and dynamic versions: $ find /usr/lib -name libm.* that command results in libm.a and libm.so in my system. Maybe your library has also a static version. – zsram Dec 27 '16 at 19:37