voltento
voltento

Reputation: 887

CMake target_include_directory doesn't provide include

I have the small project that looks like shown below:

/
 CMakeLists.txt
 src/
     CMakeLists.txt
     sources...
 libs/
      foo/
          CMakeLists.txt
          src/
              sources...
          include/
                  headers...

The CMakeLists.txt files look like this:

/CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
add_subdirectory(libs/foo)
add_subdirectory(src)

/src/CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
set(CMAKE_CXX_STANDARD 14)
add_executable(my_app
        main.cpp
)
target_link_libraries(my_app ${MY_LIB})    

/libs/foo/CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)    
set(CMAKE_CXX_STANDARD 14)

set(MY_LIB httpOnThreads)
set(MY_LIB_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include)

add_library(${MY_LIB} STATIC
        include/foo.h src/foo.cpp
)

target_include_directories (${MY_LIB} PUBLIC
        ${MY_LIB_INCLUDE} )
target_include_directories(${MY_LIB} PRIVATE src)

I expect target_link_libraries(my_app ${MY_LIB}) will be provide include information. But, when I try to build the project, I have fatal error: No such file or directory

What do I do wrong?

Upvotes: 0

Views: 2053

Answers (1)

Unapiedra
Unapiedra

Reputation: 16218

The problem is that in src/CMakeLists.txt you have:

target_link_libraries(my_app ${MY_LIB})    

However, in this scope, the variable MY_LIB is not defined and thus empty.

Replacing that line with

target_link_libraries(my_app httpOnThreads)

solves the issue.

I would recommend to be explicit on the library name.

Upvotes: 3

Related Questions