gpt4 book ai didi

c++ - 将 CMake 子项目与另一个集成

转载 作者:行者123 更新时间:2023-11-30 02:31:44 25 4
gpt4 key购买 nike

我写了一个 C++ 库 MyLib,我想将它与另一个项目 ExternPro 集成。所以在 ExternPro 中,我这样写 CMakeLists.txt:

add_subdirectory(MyLib)
ADD_EXECUTABLE(test test.cpp)
include_directories(${MyLib_INCLUDE_DIRS})
target_link_libraries(test ${MyLib_LIBRARIES})

要设置变量 MyLib_LIBRARIESMyLib_INCLUDE_DIRS 我写道:

set(MyLib_LIBRARIES ${PROJECT_SOURCE_DIR}/src/MyLib.a CACHE INTERNAL "")
set(MyLib_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "")

但是出了点问题“没有规则来创建测试所需的目标 MyLib/src/MyLib.a。停止。”

所以我的问题是,我应该如何正确编写 CMakeLists.txt 以便 cmake 可以帮助我先构建 MyLib 然后再处理ExternPro 的依赖项?

最佳答案

如果这是两个独立的项目,我通常使用“CMake 查找脚本”从另一个库引用一个库:http://www.vtk.org/Wiki/CMake:How_To_Find_Libraries#Writing_find_modules

但我通常使用与此处描述的略有不同的查找脚本 (FindMyLibrary.cmake):

# Find MyLibrary installation
#
# This module needs following variables specified (e.g. through cmake -Dvar=)
# MyLibrary_ROOT_DIR - root directory of the library installation
#
# This module defines the following variables:
# MyLibrary_INCLUDE_DIRS - Where to find the public headers
# MyLibrary_LIBRARIES - List of mandatory and optional libraries
# MyLibrary_FOUND - True if an installation was found
#
# Configuration variables for tis module:
# MyLibrary_USE_STATIC_LIBS - Set to ON to force the use of the static
# libraries. Default is OFF.

# If MyLibrary_ROOT_DIR was defined in the environment, use it.
if(NOT MyLibrary_ROOT_DIR AND NOT $ENV{MyLibrary_ROOT_DIR} STREQUAL "")
set(MyLibrary_ROOT_DIR $ENV{MyLibrary_ROOT_DIR})
endif()

if(NOT MyLibrary_ROOT_DIR)
set(MyLibrary_ROOT_DIR /usr)
endif()

message(STATUS "Using MyLibrary_ROOT_DIR: ${MyLibrary_ROOT_DIR}")

find_path(MyLibrary_INCLUDE_DIRS
NAMES mylib/mylib.hpp
PATHS ${MyLibrary_ROOT_DIR}
PATH_SUFFIXES include)

# Here we set the default components
if(NOT MyLibrary_FIND_COMPONENTS)
set(MyLibrary_FIND_COMPONENTS mylibrary)
endif()

# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
if(MyLibrary_USE_STATIC_LIBS)
set(_mylib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
if(WIN32)
set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
endif()
endif()

foreach(COMPONENT ${MyLibrary_FIND_COMPONENTS})
find_library(MyLibrary_${COMPONENT}_LIBRARY
NAMES ${COMPONENT}
HINTS ${MyLibrary_ROOT_DIR}
PATH_SUFFIXES lib64 lib
NO_DEFAULT_PATH)
set(MyLibrary_LIBRARIES ${MyLibrary_LIBRARIES} ${MyLibrary_${COMPONENT}_LIBRARY})
endforeach()

# Restore the original find library ordering
if(MyLibrary_USE_STATIC_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_mylib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
endif()

# handle the QUIETLY and REQUIRED arguments and set MyLibrary_FOUND to
# TRUE if all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
MyLibrary "Could NOT find MyLibrary: set MyLibrary_ROOT_DIR to a proper location"
MyLibrary_LIBRARIES
MyLibrary_INCLUDE_DIRS)

mark_as_advanced(MyLibrary_INCLUDE_DIRS MyLibrary_LIBRARIES)

然后像这样使用:

find_package(MyLibrary REQUIRED)
include_directories(SYSTEM ${MyLibrary_INCLUDE_DIRS})

target_link_libraries(${TARGET}
${MyLibrary_LIBRARIES}
)

基本上,它是这样的:

  1. 库已构建并安装(make install)到默认位置(例如/usr)或其他一些位置(通常在开发中)。
  2. FindMyLibrary.cmake 是库安装的一部分(对于 RPM 库开发包),也会安装(例如 ${instdir}/share/cmake/Modules)。
  3. 然后,依赖项目将路径添加到 CMAKE_MODULE_PATH,并使用查找脚本查找已安装的公共(public) header 和库。

优点是这样你可以在开发过程中使用它(当你有库源并构建库时),或者也没有库源(只安装了库和头文件 - 开发包)在系统中)。

Boost 等通常使用类似的技术(CMake 已经提供了查找脚本)。

关于c++ - 将 CMake 子项目与另一个集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37292768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com