Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find a header with 'include_directories' in CMake

Tags:

c++

include

cmake

Here is the folder hierarchy of a c++ project:

 1. FolderA
    1. file1.h/cpp
    2. CMakeLists.txt (CmakeA)
 2. FolderB
    1. file2.h/cpp
    2. CMakeLists.txt (CmakeB)
 3. main.cpp
 4. CMakeLists.txt (CmakeAll)

CmakeA:

file(GLOB FolderA_FILES *.cpp *.h *.hpp)

# add component
add_library(FolderA ${FolderA_FILES})

include_directories(../FolderB)
target_link_libraries(FolderA FolderB)

CMakeB:

file(GLOB FolderB_FILES *.cpp *.h *.hpp)

# add component
add_library(FolderB ${FolderB_FILES})

CMakeAll:

cmake_minimum_required(VERSION 2.8)

#add smirk dependency
set(Smirk_DIR /usr/local CACHE PATH "Directory where Smirk has been installed (e.g. /usr/local).")
include(${Smirk_DIR}/cmake/smirk.cmake)
smirk_project(operatingTableProject)
file(GLOB smirk_operatingTableProject_FILES *.cpp *.h *.hpp)
add_executable(smirk_operatingTableProject ${smirk_operatingTableProject_FILES})

# install directives
install(
    TARGETS smirk_operatingTableProject
    RUNTIME DESTINATION bin
)

#add FolderB dependency
add_subdirectory(FolderB)
target_link_libraries(smirk_operatingTableProject FolderB)

#add FolderA dependency
add_subdirectory(FolderA)
target_link_libraries(smirk_operatingTableProject FolderA)

In the file1.h I include file2.h. The problem is wherever I include in the main.cpp FolderA/file1.h I got a compilation issue:

file2.h: No such file or directory (in file1.h)

But if I comment out the include in the main.cpp, it works. Also, If I include file2.h in file1.cpp it works so the main problem is wherever I include a header file from a subdirectory in another header file from other subdirectory and one of them is included in the main file.

Any help would be appreciated.

like image 615
Maystro Avatar asked Dec 15 '16 12:12

Maystro


1 Answers

Command include_directories affects only on local scope: being issued in FolderA it affects only on the compilation of FolderA library. You need to issue that command in top-level CMakeLists.txt for compile main.cpp.

Alternatively, you may use command target_include_directories with PUBLIC option in FolderA:

target_include_directories(FolderA PUBLIC ../FolderB)

so include directories will be propagated to any target which is linked with that library.

like image 100
Tsyvarev Avatar answered Sep 28 '22 07:09

Tsyvarev