问题描述
我正在尝试为使用NetCDF的项目创建CMake文件。我是CMake的新手(这是我的第一次尝试),因此对于其中的某些内容显而易见,我深表歉意。
要安装NetCDF,我按照here显示的git指南中的步骤进行操作
我相信我正确地执行了这些步骤。我正在尝试使用NetCDF C ++库,但出于编译目的,我还必须安装C库。我是这样使用的:
sudo apt install libnetcdf-dev
运行以下命令时,我会收到适当的输出(根据git指南):
nc-config --has-nc4
到目前为止很好...我希望。这是我的CMakeLists.Txt:
cmake_minimum_required(VERSION 3.16)
project(orbit LANGUAGES CXX)
# ##############################################################################
# Conan Packages
# ##############################################################################
set(CONAN_EXTRA_REQUIRES "") set(CONAN_EXTRA_OPTIONS "")
list(APPEND CONAN_EXTRA_REQUIRES boost/1.73.0) list(APPEND CONAN_EXTRA_REQUIRES eigen/3.3.7)
if(BUILD_SHARED_LIBS) list(APPEND CONAN_EXTRA_OPTIONS "Pkg:shared=True") endif()
include(cmake/Conan.cmake) run_conan()
# ##############################################################################
find_file(CMAKE_PREFIX_PATH netCDFCxxConfig.cmake)
find_package (NetCDF required) include_directories(${NETCDF_INCLUDES})
# set(SOURCE test.cpp player.cpp player.hpp)
# include_directories(include)
# Search all .cpp files
file(GLOB_RECURSE SOURCE_FILES "*.cpp")
add_executable(test ${SOURCE_FILES})
target_link_libraries(test
PRIVATE
CONAN_PKG::boost
CONAN_PKG::eigen
${NETCDF_LIBRARIES_CXX})
CMake Error at CMakeLists.txt:24 (find_package):
By not providing "FindNetCDF.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "NetCDF",but
CMake did not find one.
Could not find a package configuration file provided by "NetCDF" with any
of the following names:
NetCDFConfig.cmake
netcdf-config.cmake
Add the installation prefix of "NetCDF" to CMAKE_PREFIX_PATH or set
"NetCDF_DIR" to a directory containing one of the above files. If "NetCDF"
provides a separate development package or SDK,be sure it has been
installed.
我的理解是,此错误是由于我没有正确执行git指南中的以下内容而引起的:
请确保
nc-config
在您的PATH
中,或netCDFConfig.cmake
的位置在CMAKE_PREFIX_PATH
中。
我尝试通过运行以下命令将nc-config
添加到我的PATH
:
export PATH="nc-config:$PATH"
但这不能解决问题...
此外,我尝试在计算机中找到一个netCDFConfig.cmake
文件,但是该文件似乎不存在。我确实有一个netCDFCxxConfig.cmake
,但不确定是否相同,或者在这里如何使用它。
有人对此问题有任何经验,并且知道如何解决吗?任何帮助将不胜感激,如果过去提供了这样的解决方案,我深表歉意。
编辑:
我能够让CMake最终找到该文件,但是现在丢失了寻找另一个文件...这是我在原始帖子中找不到的文件:
# NetCDF CXX Configuration Summary
####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was netCDFCxxConfig.cmake.in ########
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
macro(set_and_check _var _file)
set(${_var} "${_file}")
if(NOT EXISTS "${_file}")
message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
endif()
endmacro()
macro(check_required_components _NAME)
foreach(comp ${${_NAME}_FIND_COMPONENTS})
if(NOT ${_NAME}_${comp}_FOUND)
if(${_NAME}_FIND_required_${comp})
set(${_NAME}_FOUND FALSE)
endif()
endif()
endforeach()
endmacro()
####################################################################################
include(CMakeFindDependencyMacro)
if (1)
if(EXISTS "")
set(netCDF_ROOT "")
endif()
if(EXISTS "/usr/lib/x86_64-linux-gnu/cmake/netCDF")
set(netCDF_DIR "/usr/lib/x86_64-linux-gnu/cmake/netCDF")
endif()
find_dependency(netCDF)
set(NETCDF_C_LIBRARY ${netCDF_LIBRARIES})
set(NETCDF_C_INCLUDE_DIR ${netCDF_INCLUDE_DIR})
else()
set(NETCDF_C_LIBRARY "netcdf")
set(NETCDF_C_INCLUDE_DIR "/usr/include")
endif()
if (NOT TARGET netCDF::netcdf)
add_library(netCDF::netcdf UNKNowN IMPORTED)
set_target_properties(netCDF::netcdf PROPERTIES
IMPORTED_LOCATION "${NETCDF_C_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${NETCDF_C_INCLUDE_DIR}"
)
endif()
include("${CMAKE_CURRENT_LIST_DIR}/netcdf-cxx4Targets.cmake")
最后一行是所有错误所在。在这里找到netcdf-cxx4Targets.cmake
会遇到问题。
我刚发布的文件位于目录netcd-cxx4/build
中
netcdf-cxx4Targets.cmake
在目录中:
netcd-cxx4/build/CMakeFiles/Export/lib/cmake/netCDF
解决方法
您要使用netCDFCxxConfig.cmake
文件。通过快速查看GitHub repository,可以找到该文件的模板(netCDFCxxConfig.cmake.in
,但没有netCDFConfig.cmake.in
的模板。因此,我的结论是维护人员可能在某个时候更改了配置文件名,而忘记将其自述文件更新为netCDFCxxConfig.cmake
。
您可以将netCDFCxxConfig.cmake
文件的位置添加到CMake文件中的CMAKE_PREFIX_PATH
列表变量中。然后,链接到netCDFCxxConfig.cmake
文件中定义的导入目标netCDF::netcdf
。
...
# ##############################################################################
# Don't do this.
find_file(CMAKE_PREFIX_PATH netCDFCxxConfig.cmake)
# Instead,append the path to the config file using the 'list' command.
list(APPEND CMAKE_PREFIX_PATH "/path/to/installed/netcdf")
# Look for the 'netCDFCxx' package,since that is what the config file is called.
find_package (netCDFCxx REQUIRED)
# Probably don't need this if we use the imported target below.
include_directories(${NETCDF_INCLUDES})
# set(SOURCE test.cpp player.cpp player.hpp)
# include_directories(include)
# Search all .cpp files
file(GLOB_RECURSE SOURCE_FILES "*.cpp")
add_executable(test ${SOURCE_FILES})
# Link to the imported target 'netCDF::netcdf' here instead.
target_link_libraries(test
PRIVATE
CONAN_PKG::boost
CONAN_PKG::eigen
netCDF::netcdf)