10

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Maystro
  • 2,907
  • 8
  • 36
  • 71

1 Answers1

14

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153