5

I am writing project in c++, and I have problem with importing libraries in cmake. The third party library, which is importing, does not propagate include directories.

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

Example of code /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;

}

The Error List is:

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"

Why nana_lib target does not propagate include directories for camera_calib_exe target? How to solve that issue?

Thank you in advance.

qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
tsybaya
  • 117
  • 1
  • 7
  • `add_subdirectory(nana)` is slightly cleaner than `add_subdirectory(${PROJECT_SOURCE_DIR}/nana)`; paths are relative anyway – Nibor Jan 20 '18 at 17:32

1 Answers1

6

The scope for imported target's is important. Try to add the GLOBAL option to 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."

Reference

Florian
  • 39,996
  • 9
  • 133
  • 149
  • Isn't this kinda wrong to pollute global project config? Shouldn't `find_package` be used instead? – user7860670 Jan 20 '18 at 12:26
  • @VTT But even `find_package()` would add something like a `nana_lib` target to your main scope. So no, I don't see a difference there. And from the shown `nana/CMakeLists.txt` it's not meant to be found by CMake (no `nanaConfig.cmake`). I admit I didn't check [`nana` C++ library](https://github.com/cnjinhao/nana) to see what they recommend in respect of "how to include their library in an existing project". But it looks like the shown `nana/CMakeLists.txt` is nothing official. – Florian Jan 20 '18 at 12:37
  • @VTT `find_package` only works if someone else already took the time to generate a cmake file that defines all the target properties of a library. When there is no cmake file then the obvious course of action is to define targets ourselves. – RAM Aug 23 '18 at 09:16