Pybind11: How could I compile with cmake and have other dependencies

Created on 16 Sep 2018  路  3Comments  路  Source: pybind/pybind11

I would like to write an extension named my_module, I have the my_module.cpp which calls some function written in the fun.cpp/fun.h files. Then I write the CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.12)
project(example)

add_library(fun fun.cpp)
include_directories("${PROJECT_SOURCE_DIR}/")
link_libraries("${PROJECT_SOURCE_DIR}/")

add_subdirectory(pybind11)
pybind11_add_module(main main.cpp)

When I tried to generate Makefile with the comman cmake ., I come over the errors:

CMake Error at CMakeLists.txt:10 (target_link_libraries):
  The keyword signature for target_link_libraries has already been used with
  the target "main".  All uses of target_link_libraries with a target must be
  either all-keyword or all-plain.

  The uses of the keyword signature are here:

   * pybind11/tools/pybind11Tools.cmake:107 (target_link_libraries)

How could I write the correct CMakeLists.txt such that I could make my project work ?

Most helpful comment

If you have a your module in my_module.cpp, you should use that instead of main.cpp

Try this:

cmake_minimum_required(VERSION 2.8.12)
project(my_module)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_library(fun fun.cpp)
include_directories("${PROJECT_SOURCE_DIR}/")

add_subdirectory(pybind11)
pybind11_add_module(my_module my_module.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE fun)

alternative layouts

Using add_library and pybind11_add_module in the same cmakelists.txt is a little unorthedox, but works.
If fun is not a standalone library, then no need to use add_library, just do:

cmake_minimum_required(VERSION 2.8.12)
project(my_module)
include_directories("${PROJECT_SOURCE_DIR}/") # for fun.h

add_subdirectory(pybind11)
pybind11_add_module(my_module my_module.cpp fun.cpp)

If fun is a standalone library, then usually it would be in its own directory with its own CMakeLists.txt, then my_module would add it with add_subdirectory, just like how pybind11 gets added.

All 3 comments

If you have a your module in my_module.cpp, you should use that instead of main.cpp

Try this:

cmake_minimum_required(VERSION 2.8.12)
project(my_module)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_library(fun fun.cpp)
include_directories("${PROJECT_SOURCE_DIR}/")

add_subdirectory(pybind11)
pybind11_add_module(my_module my_module.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE fun)

alternative layouts

Using add_library and pybind11_add_module in the same cmakelists.txt is a little unorthedox, but works.
If fun is not a standalone library, then no need to use add_library, just do:

cmake_minimum_required(VERSION 2.8.12)
project(my_module)
include_directories("${PROJECT_SOURCE_DIR}/") # for fun.h

add_subdirectory(pybind11)
pybind11_add_module(my_module my_module.cpp fun.cpp)

If fun is a standalone library, then usually it would be in its own directory with its own CMakeLists.txt, then my_module would add it with add_subdirectory, just like how pybind11 gets added.

Got it, thanks

This has been resolved.

Was this page helpful?
0 / 5 - 0 ratings