added subdirectory to CMakeLists.txt isn't detected by main.cpp

396 views Asked by At

I'm working on creating a python binding to a c++ code using Pybind11. My project architecture is the following:

-- MyProject
   | -- lib
         | -- pybind11
              | -- CMakeLists.txt
              | -- include
         | -- myCPPCode
              | -- CMakeLists.txt
              | -- MyCodeDefinitions.h
              | -- MyCode.cpp
   | -- src
         | -- MyProject
              | -- MyModule.cpp
   | -- CMakeLists.txt
   | -- setup.py

what I want to do, is to include #include <myCPPCode/MyCodeDefinitions.h> into MyModule.cpp.

In order to do that, I added into the global CMakeLists.txt the following command: add_subdirectory(lib/myCPPCode), But it's not working, as I don't know what to put in lib/myCPPCode/CMakeLists.txt, and I'm getting the following error:

path/MyProject/src/MyProject/MyModule.cpp:2:10: fatal error: myCPPCode/MyCodeDefinitions.h: No such file or directory
    2 | #include <myCPPCode/MyCodeDefinitions.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~

The content of lib/myCPPCode/CMakeLists.txt is the following:

cmake_minimum_required(VERSION 3.13)
project(myCPPCode)


add_library(myCPPCode MyCode.cpp)

How should I configure my CMakeLists.txt and lib/myCPPCode/CMakeLists.txt?

Thank you in advance!

1

There are 1 answers

2
Sylvain Chaugny On BEST ANSWER

The add_subdirectory just tells cmake to find a CMakeLists.txt in the directory given as argument.

You need to change your add_subdirectory(lib/myCPPCode) into include_directories(lib/myCPPCode) and it should work better.