我有一个小项目,如下所示:
/
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
我做错了什么?
答案 0 :(得分:3)
问题在于src/CMakeLists.txt
你有:
target_link_libraries(my_app ${MY_LIB})
但是,在此范围内,变量MY_LIB
未定义,因此为空。
用
替换该行target_link_libraries(my_app httpOnThreads)
解决了这个问题。
我建议明确库名。