5
// include/MyLib/MyModel.h

#include <memory>
#include <string>
#include "myEntity.h"
#include <gsl/gsl>

class MyModel {
    public:
        std::unique_ptr<MyEntity> load(std::string id);
        bool store(gsl::not_null<MyEntity*> entity); // <---
}

 

# CMakeLists.txt

# `git submodule add https://github.com/Microsoft/GSL.git dependency/gsl`
add_subdirectory(dependency/gsl EXCLUDE_FROM_ALL)
target_link_libraries(${PROJECT_NAME} INTERFACE GSL)

include(GNUInstallDirs)
install(
    DIRECTORY ${CMAKE_SOURCE_DIR}/include/
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
# other install and CMake package stuff...

After make install I have my library and headers installed

/usr/local/lib/libMyLib.so.1.0.0
/usr/local/lib/libMyLib.so.1
/usr/local/lib/libMyLib.so
/usr/local/include/MyLib/MyModel.h
/usr/local/lib/cmake/MyLib/MyLib.cmake
/usr/local/lib/cmake/MyLib/MyLib-noconfig.cmake
/usr/local/lib/cmake/MyLib/MyLibConfig.cmake
/usr/local/lib/cmake/MyLib/MyLibConfigVersion.cmake

 

# /usr/local/lib/cmake/MyLib/MyLibConfig.cmake

get_filename_component(MyLib_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)

set( MyLib_INCLUDE_DIR  "/usr/local/include" )
set( MyLib_INCLUDE_DIRS  "/usr/local/include" )

message(STATUS "MyLib found. Headers: ${MyLib_INCLUDE_DIRS}")

Clients will use the library like this:

# CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(MyLibTest)

add_executable(${PROJECT_NAME} main.cpp)
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_14)

find_package(MyLib 1.0 REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE MyLib)

 

// main.cpp

#include <iostream>
#include <MyLib/MyModel.h> // <-- not found <gsl/gsl>

using namespace std;

int main() {
    auto entity = MyModel::load("someUniqueId");
    // ...
    return 0;
}

Possible solutions

  1. Create include/MyLib/gsl/ and add it as separate include directory in CMakeLists.txt and in MyLibConfig.cmake to prevent fails of #include <gsl/...>
  2. I tried sudo apt install libgsl-dev and find_package(GSL REQUIRED) but that is https://www.gnu.org/software/gsl/ (name collision)

I can't find any questions related to this on github repository, so there should be an easy solution (obvious to others) that I have missed.

Oleg
  • 486
  • 6
  • 12
  • 1
    Client needs to set up include path for gsl if he wants to use your library. If you've used for example boost in `MyModel.h` then client would also need to set up include path for boost. It may be not the most convenient way to utilize third-party libraries (being C++ dev is suffering), but there is nothing wrong with it. – user7860670 Oct 12 '17 at 18:48
  • If I correctly understand the problem, you expect `GSL` target, created by the subproject, to add its **installation** directory to include directories property. But I see no command in its [CMakeLists.txt](https://github.com/Microsoft/GSL/blob/master/CMakeLists.txt) which setups this. The script adds *BUILD_INTERFACE* to its include directories, but not *INSTALL_INTERFACE*. – Tsyvarev Oct 12 '17 at 20:11

0 Answers0