I have a C++ project with a folder structure like this:
src/
CMakelists.txt
pynative/
CMakelists.txt
include/
pynative.cpp
include/
foo.h
// more headers
lib.cpp
// more source files
The pynative.cpp
file has a PYBIND11_MODULE
definition to export some functions which use code from the parent src/include/
. I want to compile and export the pybind11 module with all the required source code from src/include/
and lib.cpp
. Here's my src/CMakelists.txt
:
cmake_minimum_required(VERSION 3.5.1)
set(CMAKE_CXX_COMPILER "CLANG")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(mainproj VERSION 0.1.0 LANGUAGES CXX)
find_package(LLVM REQUIRED CONFIG)
include_directories(include)
add_library(mainproj lib.cpp)
add_subdirectory(pynative)
target_link_libraries(pynative PUBLIC mainproj)
And here's my pynative/CMakelists.txt
:
cmake_minimum_required(VERSION 3.5.1)
project(pynative VERSION 0.1.0 LANGUAGES CXX)
list(APPEND CMAKE_PREFIX_PATH C:/Users/somepath/)
find_package(pybind11 REQUIRED)
pybind11_add_module(pynative pynative.cpp)
This is the first time I'm working with Cmake and its hard to understand why I'm getting this error:
Attempt to add link library "mainproj" to target "pynative"
which is not built in this directory.
This is allowed only when policy CMP0079 is set to NEW
The mainproj
is just a set of functions no main so I don't want to compile it to an executable, I want it as a library which links with pynative
to give me a Python module I can use in my code. I know mainproj
is being built outside of pynative
but not sure what can I do to fix this. Please help.
Also, is my current project structure a good way to organize this type of code? Should I move mainproj
to a sibling directory?