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 ?
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)
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.
Most helpful comment
If you have a your module in
my_module.cpp, you should use that instead ofmain.cppTry this:
alternative layouts
Using
add_libraryandpybind11_add_modulein the same cmakelists.txt is a little unorthedox, but works.If
funis not a standalone library, then no need to useadd_library, just do:If
funis a standalone library, then usually it would be in its own directory with its own CMakeLists.txt, then my_module would add it withadd_subdirectory, just like how pybind11 gets added.