CMake: can't find header files
Asked Answered
C

3

4

I have a directory main, which has the following subdirectories: A, B, C, D, Test.

In Test, I have a CMakeLists file with the following content:

cmake_minimum_required(VERSION 2.8)
enable_testing()
set(TEST_EXE_NAME test)
add_executable(${TEST_EXE_NAME} test.cpp)
add_test(NAME "testDatabase" COMMAND ${TEST_EXE_NAME})
target_include_directories(Test PUBLIC ./)
target_include_directories(Test A B C D)
target_link_libraries(Test A B C D)

In Test, I have an executable which #includes several header files from A, B, C, and D.

However, after doing make, I get the message that cmake cannot find these header files from A, B, C, and D.

How can I make this go away?

Cadent answered 23/1, 2017 at 19:49 Comment(3)
A, B, C, & D appear in your question as directory names and library names, but your question asks about headers.Derange
I don't see anywhere where you include the headers for A, B, C, or D.Anthologize
@JoelCornett Sorry, I meant files within A, like A.h, B.h, etc.Cadent
G
5

From your question, it is hard to see exactly what is going wrong. This is why I am going to describe how I would tackle the whole problem.

In your "directory main"

It is necessary to have a CMakeLists.txt here to be able to use the CMake targets for A-D in Test. It would look like this:

cmake_minimum_required(VERSION 2.8)
enable_testing()
add_subdirectory(A)
add_subdirectory(B)
add_subdirectory(C)
add_subdirectory(D)
add_subdirectory(Test)

Note that we call enable_testing() here. This will enable you to call make test in your root build directory directly later on.

In the folders A-D

There, you create libraries for A-D. For A, for instance, you would write:

add_library(A STATIC [... source files for A ...]) # or SHARED instead of STATIC
target_include_directories(A PUBLIC ./)

Note that by using target_include_directories, you tell CMake to include the directories for the libraries automatically later on. This will be useful below.

In the folder Test

Now this becomes quite easy:

set(TEST_EXE_NAME test)
add_executable(${TEST_EXE_NAME} test.cpp)
target_include_directories(${TEST_EXE_NAME} PUBLIC ./)
target_link_libraries(${TEST_EXE_NAME} A B C D)
add_test(NAME "testDatabase" COMMAND ${TEST_EXE_NAME})

Note that there is no need to set the include directories for A-D here, since CMake already knows from before that they are needed!

Glomeration answered 23/1, 2017 at 22:14 Comment(0)
T
3

First argument od target_include_directories is CMake target, not directory, thus you should use following code (with assumption that ${TEST_EXE_NAME} is the target that requires headers from A, B, C, D):

target_include_directories(${TEST_EXE_NAME} PUBLIC ./)
target_include_directories(${TEST_EXE_NAME} PUBLIC A B C D)
Twannatwattle answered 23/1, 2017 at 20:44 Comment(2)
Thanks for your response! When I do that, I get the message that "target_include_directories called with invalid arguments." Do you have any suggestions for that?Cadent
Try adding PUBLIC before A in second line.Twannatwattle
D
-1

Try including A, B, C, and D in the target_include_directories() line. As was mentioned, the target_link_libraries() line should be specifying the libraries, not the headers involved.

Decorticate answered 23/1, 2017 at 20:16 Comment(1)
I just tried that and updated the question to reflect that, but got the message that "target_include_directories called with invalid arguments". Do you have any other suggestions? Thanks!Cadent

© 2022 - 2024 — McMap. All rights reserved.