cmake including h files from other directories

14,669
#include <heightQuadGrid/heightQuadGrid.h>

This syntax indicates that one of the "include directories" for the project should be the directory above the heightQuadGrid dir. In the cmakelists.txt file for the heightQuadTreeTest executable, you need to go up two directories, and add that as an include directory:

include_directories(../../)
Share:
14,669
Fantastic Mr Fox
Author by

Fantastic Mr Fox

I am a software engineer and roboticist. Building robots is my thing!

Updated on June 17, 2022

Comments

  • Fantastic Mr Fox
    Fantastic Mr Fox almost 2 years

    I am having trouble including a test under a cmake project. My project is set out like this:

                                           TerrainMap
                                            /     \
                             PointAccumulator     heightQuadGrid
                                                    \
                                                     Test
    

    In the TerrainMap Directory the CMakeLists.txt file simply outlines the cmake version the project name and includes the two sub directories.

    In heightQuadGrid the CMakeLists.txt looks like this:

    cmake_minimum_required(VERSION 2.8)
    
    find_package(PCL 1.2 REQUIRED)
    find_package(OpenCV REQUIRED)
    
    include_directories(${PCL_INCLUDE_DIRS})
    link_directories(${PCL_LIBRARY_DIRS})
    add_definitions(${PCL_DEFINITIONS})
    
    add_library(heightQuadGrid heightQuadGrid.cpp)
    
    add_subdirectory(Test)
    

    which as I understand makes a library called heightQuadGrid. The CMakeLists.txt in Test looks like this:

    FIND_PACKAGE(PCL 1.2 REQUIRED)
    FIND_PACKAGE(OpenCV REQUIRED)
    FIND_PACKAGE(Boost COMPONENTS unit_test_framework REQUIRED)
    
    include_directories(${PCL_INCLUDE_DIRS})
    link_directories(${PCL_LIBRARY_DIRS})
    add_definitions(${PCL_DEFINITIONS} )
    
    link_libraries(heightQuadGrid)
    
    add_executable(heightQuadTreeTest heightQuadGridTest.cpp)
    target_link_libraries (heightQuadTreeTest heightQuadGrid ${PCL_LIBRARIES} ${OpenCV_LIBS} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
    

    And finally the cpp file heightQuadGridTest.cpp has this include:

    #include <heightQuadGrid/heightQuadGrid.h>
    

    The cmake works correctly but when i go to make the project it tells me that it cannot find heightQuadGrid/heightQuadGrid.h

    Whats the deal as I have seen a very similar approach in another working project?

  • Fantastic Mr Fox
    Fantastic Mr Fox about 12 years
    No I don't think so, to me this indicates i am trying to get heightQuadGrid.h from the library heightQuadGrid which i have linked appropriately.
  • tmpearce
    tmpearce about 12 years
    Correct me if I'm wrong (I don't have your directory structure on my machine) but "linking appropriately" tells the linker where to look... the include directories tell the compiler where to look.
  • Fantastic Mr Fox
    Fantastic Mr Fox about 12 years
    Ok I think I understand what you mean, but would i need to include 2 directories up or just one to where heightQuadGrid.h is? And then how do i include it in my cpp file, the way I have done?
  • Fantastic Mr Fox
    Fantastic Mr Fox about 12 years
    My apologies this worked. Im not sure I really understand why though. But thankyou. A more neat looking solution to this for anyone is to do include_directories(${CMAKE_SOURCE_DIR})
  • tmpearce
    tmpearce about 12 years
    Glad it worked. When you #include something, the compiler looks at the "include directories", then looks for the file relative to those (including subdirectories you have in that #include). In your case, heightQuadGrid is the subdir and heightQuadGrid.h is the file. So you need the directory above that subdir so when the whole thing gets appended, the full path is valid. include_dir/subdir/file in other words. As you note, ${CMAKE_SOURCE_DIR} works too - that's because in this case, it is the same directory as ../../ in your directory structure.