I have the following code structure:
--src
|--common--include--common---datatype--a_h_file.hpp
| |
| --src
|
|--main_lib
| |--------include-----one---one.hpp
| |
| |---src--------one----one.cpp
| CMakeLists.txt
|---main.cpp
CMakeLists.txt
main.cpp
uses one.hpp
without problem.
My CMakeLists.txt
files are like this
Upper level
cmake_minimum_required(VERSION 3.0.0)
project(MyProject VERSION 1.0.0)
add_subdirectory(main_lib)
add_executable(myproj main.cpp)
target_link_libraries(myproj PUBLIC mainpub)
and the other
add_library(mainpub
src/one/one.cpp)
target_include_directories(mainpub PUBLIC include)
With this I can use one.hpp
My problem is that by design it has been decided that one.hpp
should include a_h_file.hpp
like this
(one.hpp)
#pragma once
float addition(float,float);
#include "common/data_type/a_h_file.hpp" //<---THIS!
class whatever{
public:
int number=1;
};
So, my question is how do I modify the CMake files to include the path /src/common/include
to the paths that are going to be considered in order to be able to use a_h_file.hpp
?
EDIT: I tried
cmake_minimum_required(VERSION 3.0.0)
project(MyProject VERSION 1.0.0)
add_library(Common INTERFACE)
target_include_directories(Common INTERFACE common/include)
add_subdirectory(main_lib)
add_executable(myproj main.cpp)
target_link_libraries(myproj PUBLIC mainpub)
and in the other
add_library(mainpub
src/one/one.cpp)
#target_include_directories(mainpub PUBLIC include ../common/include)
#target_include_directories(mainpub PUBLIC Common)
#target_include_directories(mainpub PUBLIC include)
target_include_directories(mainpub PUBLIC Common include)
but it did not work :(
fatal error: common/data_type/a_h_file.hpp: No such file or directory
#include "common/data_type/a_h_file.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~~~
EDIT2:
Modified the second Cmake file to
add_library(mainpub
src/one/one.cpp)
target_link_libraries(mainpub PUBLIC Common)
target_include_directories(mainpub PUBLIC include)
It did not work either.
CodePudding user response:
First, define an INTERFACE
target for your common directory in the top-level CMakeLists.txt:
add_library(Common INTERFACE)
target_include_directories(Common INTERFACE common/include)
Then just link against it in your targets, which will propagate the include directories:
target_link_libraries(mainpub PUBLIC Common)