CMake target_include_directory不提供include

时间:2018-01-24 10:21:21

标签: cmake

我有一个小项目,如下所示:

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

CMakeLists.txt文件如下所示:

/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)

我希望target_link_libraries(my_app ${MY_LIB})将提供包含信息。但是,当我尝试构建项目时,我遇到致命错误:No such file or directory

我做错了什么?

1 个答案:

答案 0 :(得分:3)

问题在于src/CMakeLists.txt你有:

target_link_libraries(my_app ${MY_LIB})    

但是,在此范围内,变量MY_LIB未定义,因此为空。

替换该行
target_link_libraries(my_app httpOnThreads)

解决了这个问题。

我建议明确库名。