如何将 RapidXml 添加到 CMake 项目?
How to add RapidXml to a CMake Project?
我想将 RapidXML 添加到我的 CMake 项目中,以便我可以使用它。
我的项目结构如下所示:
Root
|
|_inc (for my own Headers)
| |_main.h
| |_CMakeList.txt
|_src (for my own Sources)
| |_main.cpp
| |_CMakeList.txt
|_libs (for third party libs)
| |_rapidxml
| |_CMakeList.txt
|_CMakeList.txt
有人可以帮助我吗?
提前致谢!
我自己找到了解决方案。 :)
在 /Root/libs/CMakeList.txt 中,我添加了以下行:
add_subdirectory(rapidxml)
add_library(rapidxml INTERFACE)
target_include_directories(rapidxml INTERFACE rapidxml/ )
如果有更好的解决方案,我很高兴听到!
RapidXML 实际上是一个 header-only 库,因此您 不需要 将它添加到 CMake - 只需 #include <rapidxml/rapidxml.hpp>
。但是,您可以使用 CMake 的 find_package()
函数以可移植的方式“正确地”执行此操作。但这并不容易!
首先,在项目目录的某处创建一个 cmake
文件夹 - 这也可用于其他没有 built-in CMake 模块的库。通过将此行放在 CMakeLists.txt
:
顶部附近的某个位置,将其添加到模块路径中
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
在该文件夹中,创建一个名为 FindRapidXML.cmake
的文件并为其提供以下内容:
#
# Search and find the RapidXML library
#
# Search for the include path
find_path(
RapidXML_INCLUDE_DIR
NAMES
rapidxml.hpp
rapidxml_utils.hpp
rapidxml_iterators.hpp
rapidxml_print.hpp
HINTS "${RapidXML_ROOT_DIR}"
PATH_SUFFIXES include rapidxml
)
# If we find the library, add it as a target
if (RapidXML_INCLUDE_DIR)
set(RapidXML_FOUND TRUE)
# Pull the version number out of the header
message(DEBUG "Pulling version from ${RapidXML_INCLUDE_DIR}/rapidxml.hpp")
file(STRINGS "${RapidXML_INCLUDE_DIR}/rapidxml.hpp"
RapidXML_VERSION REGEX "^//[ ]*Version[ ]+[0-9]+\.[0-9]+[ ]*$")
string(REGEX REPLACE "^//[ ]*Version[ ]+([0-9]+)\.([0-9]+)[ ]*$" "\1\.\2" RapidXML_VERSION "${RapidXML_VERSION}")
# Print a message that we found it
if (NOT RapidXML_FIND_QUIETLY)
message(STATUS "Found RapidXML (found version \"${RapidXML_VERSION}\")")
endif (NOT RapidXML_FIND_QUIETLY)
# Create a target for it, if there isn't one already created
if (NOT TARGET RapidXML::RapidXML)
add_library(RapidXML::RapidXML INTERFACE IMPORTED)
set_target_properties(RapidXML::RapidXML PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${RapidXML_INCLUDE_DIR}")
endif (NOT TARGET RapidXML::RapidXML)
else ()
# If we don't find the library, fail as appropriate
if (RapidXML_FIND_REQUIRED)
message(FATAL_ERROR "RapidXML was NOT found")
elseif (NOT RapidXML_FIND_QUIETLY)
message(STATUS "RapidXML was NOT found")
endif (RapidXML_FIND_REQUIRED)
endif (RapidXML_INCLUDE_DIR)
# Hide the variables that we set
mark_as_advanced(RapidXML_INCLUDE_DIR)
然后,您可以通过一行简单的代码将 RapidXML 导入到您的项目中:
find_package(RapidXML REQUIRED)
您可以像这样将其包含在目标中:
target_link_libraries(<target> PRIVATE RapidXML::RapidXML)
这样做的美妙之处在于,您可以使用 find_package()
中的选项来决定是否需要它,因此如果找不到它,您会提前失败。 FindRapidXML.cmake
文件也可以在其他项目中重复使用,找到它的方法不在你的主 cmake 文件中,如果它安装在 non-standard 位置,你可以提示它在哪里使用 HINTS <path>
就像使用任何其他包一样。
我想将 RapidXML 添加到我的 CMake 项目中,以便我可以使用它。 我的项目结构如下所示:
Root
|
|_inc (for my own Headers)
| |_main.h
| |_CMakeList.txt
|_src (for my own Sources)
| |_main.cpp
| |_CMakeList.txt
|_libs (for third party libs)
| |_rapidxml
| |_CMakeList.txt
|_CMakeList.txt
有人可以帮助我吗? 提前致谢!
我自己找到了解决方案。 :) 在 /Root/libs/CMakeList.txt 中,我添加了以下行:
add_subdirectory(rapidxml)
add_library(rapidxml INTERFACE)
target_include_directories(rapidxml INTERFACE rapidxml/ )
如果有更好的解决方案,我很高兴听到!
RapidXML 实际上是一个 header-only 库,因此您 不需要 将它添加到 CMake - 只需 #include <rapidxml/rapidxml.hpp>
。但是,您可以使用 CMake 的 find_package()
函数以可移植的方式“正确地”执行此操作。但这并不容易!
首先,在项目目录的某处创建一个 cmake
文件夹 - 这也可用于其他没有 built-in CMake 模块的库。通过将此行放在 CMakeLists.txt
:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
在该文件夹中,创建一个名为 FindRapidXML.cmake
的文件并为其提供以下内容:
#
# Search and find the RapidXML library
#
# Search for the include path
find_path(
RapidXML_INCLUDE_DIR
NAMES
rapidxml.hpp
rapidxml_utils.hpp
rapidxml_iterators.hpp
rapidxml_print.hpp
HINTS "${RapidXML_ROOT_DIR}"
PATH_SUFFIXES include rapidxml
)
# If we find the library, add it as a target
if (RapidXML_INCLUDE_DIR)
set(RapidXML_FOUND TRUE)
# Pull the version number out of the header
message(DEBUG "Pulling version from ${RapidXML_INCLUDE_DIR}/rapidxml.hpp")
file(STRINGS "${RapidXML_INCLUDE_DIR}/rapidxml.hpp"
RapidXML_VERSION REGEX "^//[ ]*Version[ ]+[0-9]+\.[0-9]+[ ]*$")
string(REGEX REPLACE "^//[ ]*Version[ ]+([0-9]+)\.([0-9]+)[ ]*$" "\1\.\2" RapidXML_VERSION "${RapidXML_VERSION}")
# Print a message that we found it
if (NOT RapidXML_FIND_QUIETLY)
message(STATUS "Found RapidXML (found version \"${RapidXML_VERSION}\")")
endif (NOT RapidXML_FIND_QUIETLY)
# Create a target for it, if there isn't one already created
if (NOT TARGET RapidXML::RapidXML)
add_library(RapidXML::RapidXML INTERFACE IMPORTED)
set_target_properties(RapidXML::RapidXML PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${RapidXML_INCLUDE_DIR}")
endif (NOT TARGET RapidXML::RapidXML)
else ()
# If we don't find the library, fail as appropriate
if (RapidXML_FIND_REQUIRED)
message(FATAL_ERROR "RapidXML was NOT found")
elseif (NOT RapidXML_FIND_QUIETLY)
message(STATUS "RapidXML was NOT found")
endif (RapidXML_FIND_REQUIRED)
endif (RapidXML_INCLUDE_DIR)
# Hide the variables that we set
mark_as_advanced(RapidXML_INCLUDE_DIR)
然后,您可以通过一行简单的代码将 RapidXML 导入到您的项目中:
find_package(RapidXML REQUIRED)
您可以像这样将其包含在目标中:
target_link_libraries(<target> PRIVATE RapidXML::RapidXML)
这样做的美妙之处在于,您可以使用 find_package()
中的选项来决定是否需要它,因此如果找不到它,您会提前失败。 FindRapidXML.cmake
文件也可以在其他项目中重复使用,找到它的方法不在你的主 cmake 文件中,如果它安装在 non-standard 位置,你可以提示它在哪里使用 HINTS <path>
就像使用任何其他包一样。