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