踩坑日志:CMAKE里的ADD_SUBDIRECTORIES要在INCLUDE_DIRECTORIES后面

背景:

我想include一个第三方库。我已经把库放到third_party这个文件夹里了,也在CMakeLists里设置了INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/third_party)。但是编译器死活找不到这个库。

错误:

project(LabelAndStore)
cmake_minimum_required(VERSION 2.8)
 
SET(CMAKE_CONFIGURATION_TYPES Debug)

SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

ADD_SUBDIRECTORY(src)

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/third_party)
正确:
project(LabelAndStore)
cmake_minimum_required(VERSION 2.8)
 
SET(CMAKE_CONFIGURATION_TYPES Debug)

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/third_party)

SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

ADD_SUBDIRECTORY(src)

总结:

ADD_SUBDIRECTORIES要在INCLUDE_DIRECTORIES后面。或者说,在ADD_EXECUTABLE之前要先INCLUDE_DIRECTORIES。

你可能感兴趣的:(Ubuntu,CMAKE,C++)