2021年3月6日星期六

Combining archives into shared object before using them

I have the following five source code files:

  • foo.cpp & foo.hpp
  • bar.cpp & bar.cpp
  • main.cpp (includes foo.hpp and bar.hpp)

After compiling foo.cpp and bar.cpp into object-files and packing them into archives, I can use them to compile main.cpp:

g++ -c -fPIC foo.cpp  # foo.o  g++ -c -fPIC bar.cpp  # bar.o    ar r libfoo.a foo.o  # libfoo.a  ar r libbar.a bar.o  # libbar.a    g++ -o main.out main.cpp -L. -lfoo -lbar  # main.out  

This works fine, however if I now try to combine the archives into a shared object and compile main.cpp with said object:

g++ -shared -o libbaz.so -L. -lfoo -lbar  # libbaz.so  g++ -o main.out -L. -lbaz  # failure!   

Now the linker starts complaining about undefined references. How would one do this properly?

https://stackoverflow.com/questions/66512560/combining-archives-into-shared-object-before-using-them March 07, 2021 at 10:05AM

没有评论:

发表评论