CMake find_package无法找到fCWT的config.cmake配置文件求助
问题背景
已按fCWT官方说明完成安装,Ubuntu系统下/usr/local/share/fcwt/cmake路径存在fCWT-config.cmake和fCWT-config-release.cmake配置文件,但在QT Creator中通过CMakeLists.txt执行find_package(fCWT REQUIRED)时,CMake报错找不到对应配置文件。
当前CMakeLists.txt内容
cmake_minimum_required(VERSION 3.22) project(fft LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(GSL REQUIRED) find_package(fCWT REQUIRED) include_directories(${GSL_INCLUDE_DIR}) include_directories(${fCWT_INCLUDE_DIR}) include_directories(/lib/include) include_directories(/usr/local/include) add_executable(fft main.cpp) target_link_libraries(fft PRIVATE ${GSL_LIBRARY}) target_link_libraries(fft PRIVATE ${fCWT_LIBRARY})
尝试过的无效配置
set (CMAKE_MODULE_PATH fCWT-config.cmake REQUIRED) set (CMAKE_PREFIX_PATH /usr/local/share/fcwt/cmake/fCWT-config.cmake REQUIRED) set (fCWT-config.cmake_DIR = /usr/local/share/fcwt/cmake/ REQUIRED) list(APPEND CMAKE_SYSTEM_PREFIX_PATH /usr/local/share/fcwt/cmake)
CMake报错信息
CMakeLists.txt:14: error: By not providing "FindfCWT.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "fCWT", but CMake did not find one. Could not find a package configuration file provided by "fCWT" with any of the following names:
fCWTConfig.cmake
fcwt-config.cmakeAdd the installation prefix of "fCWT" to CMAKE_PREFIX_PATH or set "fCWT_DIR" to a directory containing one of the above files. If "fCWT" provides a separate development package or SDK, be sure it has been installed.
解决建议
1. 正确指定fCWT配置文件目录
在find_package(fCWT REQUIRED)前添加以下配置,直接指向配置文件所在目录:
set(fCWT_DIR "/usr/local/share/fcwt/cmake" CACHE PATH "Path to fCWT configuration files")
或者在QT Creator的CMake配置界面(Projects > Build Settings > CMake > CMake options)添加参数:
-DfCWT_DIR=/usr/local/share/fcwt/cmake
2. 强制使用CONFIG模式查找
CMake默认优先查找Find<Package>.cmake模块,改为明确使用配置文件模式查找:
find_package(fCWT REQUIRED CONFIG)
3. 匹配CMake期望的配置文件名
报错显示CMake期望的文件名是fCWTConfig.cmake或fcwt-config.cmake,而当前文件是fCWT-config.cmake,可通过创建软链接解决:
sudo ln -s /usr/local/share/fcwt/cmake/fCWT-config.cmake /usr/local/share/fcwt/cmake/fcwt-config.cmake
4. 改用现代CMake写法优化链接
避免手动设置包含目录和库路径,直接使用fCWT导出的目标(配置文件通常会生成fCWT::fCWT目标):
# 替换原include_directories和target_link_libraries相关代码 target_link_libraries(fft PRIVATE GSL::GSL fCWT::fCWT)
内容的提问来源于stack exchange,提问作者Walrus

