C++项目集成Apache ZooKeeper库遇CMake查找包错误,求排查
问题描述
我有一个C应用,使用Apache ZooKeeper官方C库进行连接,集成时遇到CMake配置问题。
我的CMakeLists.txt配置如下:
cmake_minimum_required(VERSION 3.22) project(meta-flare-cpp) set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/lib/zookeeper") add_executable(meta-flare src/main.cpp src/ZooKeeperConnector/ZooKeeperConnector.cpp) # Link against ZooKeeper library find_package(ZooKeeper REQUIRED) target_link_libraries(meta-flare ZooKeeper::ZooKeeper) # Include directories target_include_directories(meta-flare PRIVATE ${ZooKeeper_INCLUDE_DIRS})
项目目录结构:
--- Folder Structure --- CMakeCache.txt CMakeLists.txt [cmake] └── [Modules] └── FindZooKeeper.cmake [src] ├── [ZooKeeperConnector] ├── ZooKeeperConnector.cpp └── ZooKeeperConnector.h └── main.cpp [lib] └── [zookeeper] ├── zookeeper_github_clone
执行cmake .时出现如下错误:
CMake Error at CMakeLists.txt:10 (find_package): By not providing "FindZooKeeper.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "ZooKeeper", but CMake did not find one. Could not find a package configuration file provided by "ZooKeeper" with any of the following names: ZooKeeperConfig.cmake zookeeper-config.cmake Add the installation prefix of "ZooKeeper" to CMAKE_PREFIX_PATH or set "ZooKeeper_DIR" to a directory containing one of the above files. If "ZooKeeper" provides a separate development package or SDK, be sure it has been installed.
请问我哪里操作有误?
问题分析与解决
核心问题
CMake找不到ZooKeeper的配置文件,原因有两点:
- 你自定义了
FindZooKeeper.cmake,但未将其所在目录添加到CMAKE_MODULE_PATH,导致CMake默认去系统路径查找官方提供的ZooKeeperConfig.cmake(而你克隆的源码里没有生成这个文件)。 CMAKE_PREFIX_PATH指向的路径不正确,你指向了lib/zookeeper,但实际源码在lib/zookeeper/zookeeper_github_clone目录下。
解决步骤
- 添加自定义模块路径
在CMakeLists.txt开头添加代码,让CMake能识别你写的FindZooKeeper.cmake:
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules" ${CMAKE_MODULE_PATH})
- 修正CMAKE_PREFIX_PATH路径
将路径指向ZooKeeper源码的实际目录:
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/lib/zookeeper/zookeeper_github_clone")
- 确保FindZooKeeper.cmake配置正确
你的自定义查找模块需要准确定位头文件和库文件,示例配置如下:
# 查找ZooKeeper头文件路径 find_path(ZooKeeper_INCLUDE_DIRS zookeeper/zookeeper.h PATHS ${CMAKE_PREFIX_PATH}/include) # 查找ZooKeeper库文件(根据编译版本选择库名,比如zookeeper_mt是多线程版本) find_library(ZooKeeper_LIBRARIES zookeeper_mt PATHS ${CMAKE_PREFIX_PATH}/lib) # 将库导入为CMake目标,供后续链接使用 add_library(ZooKeeper::ZooKeeper UNKNOWN IMPORTED) set_target_properties(ZooKeeper::ZooKeeper PROPERTIES IMPORTED_LOCATION ${ZooKeeper_LIBRARIES} INTERFACE_INCLUDE_DIRECTORIES ${ZooKeeper_INCLUDE_DIRS})
- 调整后的完整CMakeLists.txt
cmake_minimum_required(VERSION 3.22) project(meta-flare-cpp) # 添加自定义模块路径 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules" ${CMAKE_MODULE_PATH}) # 设置ZooKeeper源码路径 set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/lib/zookeeper/zookeeper_github_clone") add_executable(meta-flare src/main.cpp src/ZooKeeperConnector/ZooKeeperConnector.cpp) # 使用自定义Find模块查找ZooKeeper find_package(ZooKeeper REQUIRED) target_link_libraries(meta-flare ZooKeeper::ZooKeeper) target_include_directories(meta-flare PRIVATE ${ZooKeeper_INCLUDE_DIRS})
额外注意
如果是直接克隆的ZooKeeper源码,需要先编译源码生成库文件,否则find_library会找不到对应的.lib或.so文件。
内容的提问来源于stack exchange,提问作者Mani Kant Tiwari
相关产品推荐
相关产品推荐

