0

I'm making a bunch of libraries to use in my programs and some of my libraries depend on other libraries, so I compile them using the -l option of gcc to link the dependencies and it works fine but there is one case where I have a problem :

  • Let's say I make a library called lib1 with some basic functions.
  • I then make lib2 with functions that sometimes call lib1 functions.
  • Finally, I make a program that uses a function from lib2 which depends on a lib1 function (let's say I have a custom strcpy in lib2 that needs a custom malloc from lib1)

Having done that, I try to compile my program with gcc -llib2 (assuming lib2.a contains lib1 functions if I understood how static libraries work) but it doesn't work because the function from lib2 needs a function from lib1 that the compiler doesn't find. I guess I actually need to do gcc -llib1 -llib2 but it seems useless since lib2 already should contain lib1 functions.

Is there any way I could compile my program without specifying every library in the gcc command line ? (assuming I will have more than 2 libraries and multiple layers of dependencies)

I found this answer https://stackoverflow.com/a/41323069/12136455 that seems to shed some light on my problem but I can't quite understand what he means.

sbedene
  • 1
  • 1
  • 1
    `since lib2 already should contain lib1 functions` - No. That library "call"s doesn't mean it "contain"s. How did you compile lib2? – KamilCuk Sep 28 '19 at 22:02
  • I did `gcc -llib1 -c` on the .c files to generate the .o files, then `ar rc lib2.a *.o` and `ranlib lib2.a` – sbedene Sep 28 '19 at 22:07
  • `-llib1` links lib1 dynamically, not statically. Also `lib2` is a static library (it's just an archive of .o files...), you shouldn't link with it (well, unless you really want to), but compile _with it_. You could unpack the library `lib1.a` to .o files and then include those .o files with lib2.a – KamilCuk Sep 28 '19 at 22:23
  • This makes sense, I will try that thanks a lot – sbedene Sep 28 '19 at 22:38

0 Answers0