Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake including h files from other directories

Tags:

c++

cmake

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?

like image 319
Fantastic Mr Fox Avatar asked Apr 02 '12 23:04

Fantastic Mr Fox


People also ask

How do I include a .h file in CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .

What does Include_directories do in CMake?

Add include directories to the build. Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory.

How add include path CMake?

First, you use include_directories() to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable() or add_library() call.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

How do I add a directory to CMake?

First, you use include_directories () to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable () or add_library () call.

How do I add headers to a project using Cmake?

First, you use include_directories () to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable () or add_library () call. As an example, if your project's sources are in src, and you need headers from include, you could do it like this:

What is the cmakelists file?

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

Why doesn't CMake track the headers of a code blocks project?

At least, when generating a Code::Blocks project the header files do not appear within the project (the source files do). It therefore seems to me that CMake consider those headers to be externalto the project, and does not track them in the depends.


1 Answers

#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(../../)
like image 164
tmpearce Avatar answered Sep 27 '22 02:09

tmpearce