include_directories 用于导入的库

include_directories for imported library

我正在用 C++ 编写项目,但在 cmake 中导入库时遇到问题。正在导入的第三方库不传播包含目录。

根/CMakeLists.txt

# Top level CMakeLists.txt

cmake_minimum_required(VERSION 3.9.2 FATAL_ERROR)
project(camera_calib)

add_subdirectory(${PROJECT_SOURCE_DIR}/nana)

file(GLOB SRC "${PROJECT_SOURCE_DIR}/src/*.cpp")

add_executable(camera_calib_exe ${SRC})

target_include_directories(camera_calib_exe PRIVATE ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(camera_calib_exe nana_lib)

/nana/CMakeLists.txt

# nana c++ gui CMakeLists.txt

cmake_minimum_required(VERSION 3.9.2 FATAL_ERROR)
project(nana VERSION 1.5.6 LANGUAGES CXX)

add_library(nana_lib STATIC IMPORTED)
set_property(TARGET nana_lib PROPERTY IMPORTED_IMPLIB_DEBUG ${PROJECT_SOURCE_DIR}/lib/nana_v141_Debug_x64.lib)
set_property(TARGET nana_lib PROPERTY IMPORTED_IMPLIB_RELEASE ${PROJECT_SOURCE_DIR}/lib/nana_v141_Release_x64.lib)
set_property(TARGET nana_lib PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/include)
#set_property(TARGET nana_lib PROPERTY INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/include)

代码示例 /src/main.cpp

#include <nana/gui.hpp>
#include <nana/gui/widgets/label.hpp>
#include <nana/gui/widgets/button.hpp>

int main(int argc, char** argv)
{
    nana::form fm(nana::rectangle(0, 0, 1900, 1000));
    fm.caption("Camera Calibration");

    nana::button btn_openCam(fm, nana::rectangle(1770, 20, 100, 30));
    btn_openCam.caption("Open cameras");
    btn_openCam.events().click(nana::API::exit_all);

    nana::button btn_closeCam(fm, nana::rectangle(1770, 20 + 30 + 20, 100, 30));
    btn_closeCam.enabled(false);
    btn_closeCam.caption("Close cameras");
    btn_closeCam.events().click(nana::API::exit_all);

    nana::button btn_exit(fm, nana::rectangle(1770, 900, 100, 30));
    btn_exit.caption("Exit");
    btn_exit.events().click(nana::API::exit_all);
    fm.show();
    nana::exec();

    std::cout << "lala";
    return 0;

}

错误列表是:

E1696   cannot open source file "nana/gui.hpp"
E1696   cannot open source file "nana/gui/widgets/label.hpp"
E1696   cannot open source file "nana/gui/widgets/button.hpp"

为什么 nana_lib 目标不传播 camera_calib_exe 目标的包含目录? 如何解决这个问题?

提前致谢。

导入目标的范围很重要。尝试将 GLOBAL 选项添加到 add_library():

add_library(nana_lib STATIC IMPORTED GLOBAL)

"The target name has scope in the directory in which it is created and below, but the GLOBAL option extends visibility."

参考