The CMake example results in a broken module (when adding extra libraries). This is split of from #387 since I suspect there is a larger issue than just lack of documentation.
I am trying to create a module that wraps a C++ library, using CMake. My first attempt based on #387 was to use target_link_libraries with PRIVATE. This broke with:
Target "stylist" of type MODULE_LIBRARY may not be linked into another
target. One may link only to STATIC or SHARED libraries, or to executables
with the ENABLE_EXPORTS property set.
I ended up with this:
find_package(Stylist REQUIRED)
include_directories(SYSTEM ${Stylist_INCLUDE_DIRS})
link_directories(${Stylist_LIBRARY_DIRS})
add_subdirectory(pybind11)
pybind11_add_module(_stylist SHARED ${SOURCES})
target_link_libraries(_stylist PRIVATE ${Stylist_LIBRARIES})
Which still fails:
>>> import _stylist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit__stylist)
If I search for that error this appears to generally be caused by people compiling using Python 2 while trying to use the result with Python 3. That is not the case for me: Python 3 is used for the compilation as well.
This was my mistake: there was a mismatch between the library name in CMakeLists.txt and the module name I used in PYBIND11_MODULE.
Thanks a lot for sharing this. Had the same issue.
Just ran into the same issue, and it was _not_ a naming issue. In my case, I had to add "PRIVATE" to the target_link_library command. Ie,
pybind11_add_module(mypy mypy/mypy.cpp)
target_link_libraries(mypy my_other_cpp_library)
did NOT work, but
pybind11_add_module(mypy mypy/mypy.cpp)
target_link_libraries(mypy PRIVATE my_other_cpp_library)
did
Most helpful comment
This was my mistake: there was a mismatch between the library name in CMakeLists.txt and the module name I used in PYBIND11_MODULE.