cmake简单配置文件示例

时间:2018-04-16 12:39:12

标签: c++ cmake

现在我有了一个我想要在另一个CMake c ++项目中使用的lib。它存在于我的电脑中。

${MY_LIB_PATH}\include
${MY_LIB_PATH}\lib\x86\debug\lib-files
${MY_LIB_PATH}\lib\x86\release\lib-files
${MY_LIB_PATH}\lib\x64\debug\lib-files
${MY_LIB_PATH}\lib\x64\release\lib-files

哪个基本配置文件会让CMake find_package知道这些?我预计它会非常简单,因为它没有提供太多信息。 But this page just make my head hurt.

抱歉,我决定复制源代码,所以我不知道应该接受哪个答案。

3 个答案:

答案 0 :(得分:4)

不要自己写配置;使用CMake的export命令。它的内容太宽泛而无法完整覆盖,但这是我的一个项目的修改示例:

install(TARGETS
        your_target
    EXPORT YourPackageConfig
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
export(TARGETS
        your_target
    NAMESPACE YourPackage::
    FILE "${CMAKE_CURRENT_BINARY_DIR}/YourPackageConfig.cmake"
)
install(EXPORT
        YourPackageConfig
    DESTINATION "${CMAKE_INSTALL_DATADIR}/YourPackage/cmake"
    NAMESPACE YourPackage::
)

这将为您创建配置文件,因此其他项目可以通过find_package使用它。

find_package(YourPackage REQUIRED)
target_link_libraries(foo YouprPackage::your_target)

它会自动处理IMPORTED目标,还允许您嵌入编译器标志,包含路径,库依赖项,甚至哪些文件是您的界面的一部分(基本上,任何属于INTERFACE的内容属性)。

答案 1 :(得分:1)

在库的根目录中放置“$ {libname} -config.cmake”。

然后在该文件中添加IMPORTED目标。

libprotobuf有一个例子。

add_library(libprotobuf STATIC IMPORTED GLOBAL)

set_target_properties(libprotobuf PROPERTIES 
    IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/prebuilt/android/${ANDROID_ABI}/libprotobuf.a"
    IMPORTED_LINK_INTERFACE_LIBRARIES "${ZLIB_LIBRARIES};${CMAKE_THREAD_LIBS_INIT}"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/src")

将Env或CMake变量“$ {libname} _DIR”设置为“$ {MY_LIB_PATH}”

使用它。

find_package(${libname})
#.......
target_link_libraries(main ${libname})

答案 2 :(得分:0)

也许这个older doc可能会更轻巧。此外还有tutorialthis other one。最后一个也许是最简单的。

希望不会增加更多的痛苦: - )

按照文档提供的内容应大致如下(假设您的库为mylib):

MyLib中/ MyLibConfig.cmake.in

# - Config file for the MyLib package
# It defines the following variables
#  MYLIB_INCLUDE_DIRS - include directories for MyLib
#  MYLIB_LIBRARIES    - libraries to link against
#  MYLIB_EXECUTABLE   - the bar executable

# Compute paths
get_filename_component(MYLIB_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(MYLIB_INCLUDE_DIRS "@CONF_INCLUDE_DIRS@")

# Our library dependencies (contains definitions for IMPORTED targets)
if(NOT TARGET mylib AND NOT MyLib_BINARY_DIR)
  include("${MYLIB_CMAKE_DIR}/MyLibTargets.cmake")
endif()

# These are IMPORTED targets created by MyLibTargets.cmake
set(MYLIB_LIBRARIES mylib)

MyLib中/ MyLibConfigVersion.cmake.in

set(PACKAGE_VERSION "@MYLIB_VERSION@")

# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
  set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
  set(PACKAGE_VERSION_COMPATIBLE TRUE)
  if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
    set(PACKAGE_VERSION_EXACT TRUE)
  endif()
endif()

主要MyLib / CMakeLists.txt

...

set(MYLIB_MAJOR_VERSION 0)
set(MYLIB_MINOR_VERSION 1)
set(MYLIB_PATCH_VERSION 0)
set(MYLIB_VERSION
  ${MYLIB_MAJOR_VERSION}.${MYLIB_MINOR_VERSION}.${MYLIB_PATCH_VERSION})

...

add_library(mylib SHARED mylib.c ...)

...

install(TARGETS mylib
  # IMPORTANT: Add the mylib library to the "export-set"
  EXPORT MyLibTargets
  RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT bin
  LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT shlib
  PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}/mylib"
    COMPONENT dev)

...

# The interesting stuff goes here
# ===============================

# Add all targets to the build-tree export set
export(TARGETS mylib
  FILE "${PROJECT_BINARY_DIR}/MyLibTargets.cmake")

# Export the package for use from the build-tree
# (this registers the build-tree with a global CMake-registry)
export(PACKAGE MyLib)

# Create the MyLibConfig.cmake and MyLibConfigVersion files
file(RELATIVE_PATH REL_INCLUDE_DIR "${INSTALL_CMAKE_DIR}"
   "${INSTALL_INCLUDE_DIR}")
# ... for the build tree
set(CONF_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}")
configure_file(MyLibConfig.cmake.in
  "${PROJECT_BINARY_DIR}/MyLibConfig.cmake" @ONLY)
# ... for the install tree
set(CONF_INCLUDE_DIRS "\${MYLIB_CMAKE_DIR}/${REL_INCLUDE_DIR}")
configure_file(MyLibConfig.cmake.in
  "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/MyLibConfig.cmake" @ONLY)
# ... for both
configure_file(MyLibConfigVersion.cmake.in
  "${PROJECT_BINARY_DIR}/MyLibConfigVersion.cmake" @ONLY)

# Install the MyLibConfig.cmake and MyLibConfigVersion.cmake
install(FILES
  "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/MyLibConfig.cmake"
  "${PROJECT_BINARY_DIR}/MyLibConfigVersion.cmake"
  DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev)

# Install the export set for use with the install-tree
install(EXPORT MyLibTargets DESTINATION
  "${INSTALL_CMAKE_DIR}" COMPONENT dev)