简体   繁体   中英

Problem in loading dynamic shared c++ library from Python

I've built a project SharedProj that is linked to .so. It links 4 more projects, that are built to static libraries: libA.a, libB.a, libC.a, libD.a . These projects are set as references in SharedProj.so and linked with -l flag.

When I try to load SharedProj.so from Python with cdll.LoadLibrary , it fails with exception: "cannot find libA.so - no such file". Yes, it's really true: there's no libA.so and not supposed to be. There's libA.a , that is linked statically.

What is the problem here? How do I build SharedProj.so properly, so Python will load it correctly?

I've tried linking SharedProj.so with -shared or -shared-libgcc flags. Tried to build the static libraries with -static or -static-libgcc flags. Tried to add or remove -fPIC flag.

I've solved the problem in Eclipse IDE. Apparently, the problem was that my static libs libA.a, libB.a, libC.a have used another libs, like math lib, and I haven't specified the linker where it must search for them. In the final link directive I pass with -L flag the folders of my static libs AND the /lib folder. With -l flag I specify the names of the static libs: libA, libB, libC . Also the following flags are included: -shared -shared-gcclib -m64 Also, a directive to the linker to pull all the symbols into the .so must be included: -Wl,--whole-archive all_the_static_libs.a -Wl,--no-whole-archive. So, finally, the linkage line looks like this:

g++ -L"%workspace/libA/Debug" -L"%workspace/libB/Debug" -L"%workspace/libC/Debug" -L/lib -Wl,--whole-archive -llibA -llibB -llibC -Wl,--no-whole-archive -m64 -shared-libgcc -shared -o "SharedProj.so" ./SharedProj.o

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