1

I am trying to create a shared library (really a Python module) that links against a static library. Both libraries are part of the same project and built using cmake.

Now, the shared library is built like this:


add_library(MyLibPython SHARED ${PYTHON_WRAPPERS_SRC})
set_target_properties(MyLibPython PROPERTIES PREFIX "")
target_link_libraries(MyLibPython MyLibStatic ${LIBS})

This builds without error, but when I try to import the Python module, I get:

ImportError:
lib/python/MyLibPython.so: undefined symbol: _Zone_of_my_MyLibStatic_functions

I also have a number of executables (unit tests) that are built in a similar way, and they work perfectly.

I should add, this is using gcc on Linux.

2
  • Are you trying to link to Boost::Python statically? Or are you linking to the wrapped library statically? If the former: see my answer here: stackoverflow.com/questions/4120169/… Commented Nov 19, 2010 at 17:04
  • It's the latter. But thank you for the link, it was interesting to read that statically linking to boost python does not always work as well... Commented Nov 20, 2010 at 11:32

1 Answer 1

2

Check your linker command line. Is it passing something like -Wl,--as-needed? If so, it might not be including everything required by the static library.

I don't think your technique is portable in general. Can you get a shared library to link against? I think that there are some platforms where everything that goes into a shared library needs to be compiled as PIC.

Anyway, to link an entire archive with GNU ld (look up man ld):

gcc -o foo foo.o bar.o baz.o -Wl,--whole-archive libfoo.a -Wl,--no-whole-archive [rest-of-linker-args]
Sign up to request clarification or add additional context in comments.

4 Comments

I do not have a shred library unfortunately. I could probably link agains the object files directly, but that requires a lot of rewriting of the build scripts, and I was hoping to avoid that.
Linking the whole archive works. The problem seems to be that when linking .so files, static libs are not included even if the code in the static lib refers to it. It seems ld assumes the executable (python in this case) will be linked to the same static libs as the .so.
@Krumelur: If that's the problem, you may have some luck linking via gcc ... -Wl,-Bstatic -lfoo -Wl,-Bdynamic ...
Thanks! Will try that, once I figure out how to put that into cmake :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.