简体   繁体   中英

Compiling/Linking multiple c++ libraries in linux

I have 3 separate libraries with no dependencies, exclusive to themselves, individually compiled using g++ -c and then individually added to separate archives using ar rvs *.o .

I now have 3 separate archives.

I now have another 4th library that depends on all 3 of the previous libraries. How can I compile my 4th library to include the 3 other exclusive libraries, to use all 4(referencing only the 4th library) in a main.cpp program?

I am a g++/linux/makefile COMPLETE BEGINNER.

Is the fourth library a static library or a dynamic library?

The three first libraries are static libraries, they are nothing more than archives of object files. If the fourth library is that, then there's nothing you do with it, just create the archive.

The problem is when linking with the fourth library. Since static libraries are nothing more than archives of object files, you nee to link with all four libraries. You also need to put the libraries correct on the command line for the linker: If library A depends on library B, then A needs to come before B on the linker command line.

On the other hand if the fourth library is a shared library, then it is linked much like an executable program, and when doing that you link with all libraries needed as usual. Then when creating a program using the shared library, you don't need to link with the dependencies.


As for actual command line, the simplest way is to just add the libraries when linking:

$ g++ main.o -o my_program lib4.a lib3.a lib2.a lib1.a

The above command is for linking with static libraries, and after creating an object file of the main.cpp source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM