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? `

1
  • The term you are looking for is static library. Search for how to create one in e.g. Linux and you will get many hits. Dec 27, 2016 at 19:12

1 Answer 1

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

5
  • 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, 2016 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. Dec 27, 2016 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, 2016 at 19:31
  • @zsram no. it's like cmath when you have to use -lm in gcc
    – Mano Mini
    Dec 27, 2016 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, 2016 at 19:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.