如何正确安装包含GSL的标头(指南支持库)

时间:2017-10-12 18:12:44

标签: c++ c++11 cmake cpp-core-guidelines guideline-support-library

// 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...

make install我安装了我的库和标题后

/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}")

客户端将使用这样的库:

# 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;
}

可能的解决方案

  1. 创建include/MyLib/gsl/并将其作为单独的包含目录添加到CMakeLists.txtMyLibConfig.cmake中以防止#include <gsl/...>失败
  2. 我尝试sudo apt install libgsl-devfind_package(GSL REQUIRED),但那是https://www.gnu.org/software/gsl/(名称冲突)
  3. 我在github repository上找不到与此相关的任何问题,因此应该有一个我错过的简单解决方案(对其他人来说很明显)。

0 个答案:

没有答案