无法在 CMAKE 项目中使用 XercesC

Failing to use XercesC in a CMAKE project

我正在尝试创建一个链接到 Windows 上的 xerces-c 的 CMAKE 项目。 Xerces-C 使用 cmake 构建并安装在一个文件夹中。这是安装的布局:

xercesc/3.2.2
  |
  |-bin
  |  |-xerces-c_3_2.dll
  |  |-xerces-c_3_2D.dll
  |  |-(many executables)
  |
  |-cmake
  |  |-XercesCConfig.cmake
  |  |-XercesCConfigInterna.cmale
  |  |-(other .cmake)
  |
  |-include
  |  |-xercesc
  |     |-dom
  |     |-framework
  |     |-internal
  |     |-parsers
  |     |-util
  |     |-(other folders)
  |
  |-lib
  |  |-xerces-c_3.lib
  |  |-xerces-c_3D.lib
  |
  |-share
     |-doc
        |-(documentation

这是我的CMakeFile.txt

cmake_minimum_required (VERSION 3.10.0)

project (myproject)

set (CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} ${XERCES_ROOT})
message (STATUS "CMAKE_INCLUDE_PATH is ${CMAKE_INCLUDE_PATH}")
find_package (XercesC REQUIRED)

set (CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/..)
include_directories (${XercesC_INCLUDE_DIR})

set (PROJECT_SRC
  Dummy.cpp
  )

add_library (${PROJECT_NAME} SHARED ${PROJECT_SRC})

XERCES_ROOT是我master里定义的变量CMakeLists.txt

set (XERCES_ROOT "" CACHE FILEPATH "Root directory of the Xerces-C installation")

我使用以下命令执行 cmake:

 cmake -DBOOST_ROOT=D:\lib\Boost.69.0\ -DXERCES_ROOT=d:\lib\xercesc.2.2\ -G "Visual Studio 15 2017 Win64" ../

当我执行cmake时,我打印CMAKE_INCLUDE_PATH的内容:

-- CMAKE_INCLUDE_PATH is D:/lib/xercesc/3.2.2

这是正确的位置。我在 find_package(XercesC REQUIRED) 行之前设置了这个变量。

但是找不到包。这是错误消息:

CMake Error at C:/Program Files/CMake/share/cmake-3.13/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Failed to find XercesC (missing: XercesC_LIBRARY XercesC_INCLUDE_DIR
  XercesC_VERSION)
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.13/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files/CMake/share/cmake-3.13/Modules/FindXercesC.cmake:98 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  src/myproject/CMakeLists.txt:7 (find_package)


-- Configuring incomplete, errors occurred!

我做错了什么?我该怎么做才能告诉 CMAKE 在哪里可以找到 Xerces?也许 CMAKE_INCLUDE_PATH 用法有误?

对于在您的项目中使用的关于外部包根目录的提示 CMake,可以在 CMAKE_PREFIX_PATH 变量中提示根目录:

cmake -DCMAKE_PREFIX_PATH=d:\lib\xercesc.2.2\ <other-options>

这样,您的 CMakeLists.txt 根本不需要理会这些提示:它只使用 find_package() 并期望一切正常。

另请参阅 ,了解使用 CMAKE_PREFIX_PATH 变量的其他方式以及该变量的其他功能。