对于仅位于github上的典型C++标头库,at:https://github.com/username/library_name,
具有一个include/library_name文件夹的目录结构,如下所示:
include/library_name包含所有的库源。这通常是用户在Linux:/usr/local/include/library_name下安装的。
我需要一个cmake脚本,以便在可移植的外部项目中使用库(跨Linux、MacOs、BSD、Windows)。
它应:
用CMake实现这一目标的正确方法是什么?
发布于 2015-02-20 14:22:59
我终于让它工作了,这个FindLibraryName.cmake试图找到这个库,如果它找不到它,就从github获取它,并将它设置为一个外部项目:
# Find the Library_Name include directory
# The following variables are set if Library_Name is found.
# Library_Name_FOUND - True when the Library_Name include directory is found.
# Library_Name_INCLUDE_DIRS - The path to where the poco include files are.
# If Library_Name is not found, Library_Name_FOUND is set to false.
find_package(PkgConfig)
# Allow the user can specify the include directory manually:
if(NOT EXISTS "${LIBRARY_NAME_INCLUDE_DIR}")
find_path(LIBRARY_NAME_INCLUDE_DIR
NAMES library_name/library_name.hpp
DOC "Library_Name library header files"
)
endif()
if(EXISTS "${LIBRARY_NAME_INCLUDE_DIR}")
include(FindPackageHandleStandardArgs)
mark_as_advanced(LIBRARY_NAME_INCLUDE_DIR)
else()
include(ExternalProject)
ExternalProject_Add(library_name
GIT_REPOSITORY https://github.com/username/library_name.git
TIMEOUT 5
CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
INSTALL_COMMAND "" # Disable install step, is a header only lib!
)
# Specify include dir
ExternalProject_Get_Property(library_name source_dir)
set(LIBRARY_NAME_INCLUDE_DIRS ${source_dir}/include)
endif()
if(EXISTS "${LIBRARY_NAME_INCLUDE_DIR}")
set(Library_Name_FOUND 1)
else()
set(Library_Name_FOUND 0)
endif()然后在项目CMakeLists.txt中添加:
find_package(Library_Name)
include_directories(${LIBRARY_NAME_INCLUDE_DIRS})您可以用上面的SVN替换GIT,并提供svn的URL,它也会工作。其他版本控制系统也可用。
https://stackoverflow.com/questions/28629084
复制相似问题