为 NetCDF C++ 创建 CMake
Create CMake for NetCDF C++
我正在尝试为我使用 NetCDF 的项目创建一个 CMake 文件。我是 CMake 的新手(这是我的第一次尝试),所以如果其中一些内容很明显,我深表歉意。
为了安装 NetCDF,我按照 git 指南中显示的步骤进行操作 here
我相信我的步骤是正确的。我正在尝试使用 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 指南中的以下行:
Make sure that either nc-config
is in your PATH
, or that the location of netCDFConfig.cmake
is in CMAKE_PREFIX_PATH
.
我已经尝试将 nc-config
添加到我的 PATH
,运行ning:
export PATH="nc-config:$PATH"
但这并不能解决问题...
此外,我试图在我的电脑中找到一个 netCDFConfig.cmake
文件,但它似乎不存在。我确实有一个 netCDFCxxConfig.cmake
,但我不确定这是否相同,或者我如何才能在这里使用它。
有没有人遇到过这个问题,知道如何解决?任何帮助将不胜感激,如果过去提供过这样的解决方案,我深表歉意。
编辑:
我能够让 CMake 最终找到该文件,但现在它在寻找另一个文件时迷路了...这是它在我的原始文件中找不到的文件 post:
# 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
.
时出现问题的地方
我刚刚post编辑的文件在目录中:netcd-cxx4/build
netcdf-cxx4Targets.cmake
在目录中:
netcd-cxx4/build/CMakeFiles/Export/lib/cmake/netCDF
所以第一个应该能找到另一个?
netCDFCxxConfig.cmake
文件就是您要使用的文件。通过快速查看 GitHub repository,有一个用于此文件 (netCDFCxxConfig.cmake.in
) 的模板,但没有用于 netCDFConfig.cmake.in
的模板。因此,我的结论是维护者可能在某个时候更改了配置文件名,并且忘记将他们的 README 文档更新为 netCDFCxxConfig.cmake
.
您可以将 netCDFCxxConfig.cmake
文件的位置添加到 CMake 文件中的 CMAKE_PREFIX_PATH
列表变量。然后,link到netCDFCxxConfig.cmake
文件中定义的导入目标netCDF::netcdf
。
...
# ##############################################################################
# Don't do this.
<s>find_file(CMAKE_PREFIX_PATH netCDFCxxConfig.cmake)</s>
# 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.
<s>include_directories(${NETCDF_INCLUDES})</s>
# 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)
</pre>
我正在尝试为我使用 NetCDF 的项目创建一个 CMake 文件。我是 CMake 的新手(这是我的第一次尝试),所以如果其中一些内容很明显,我深表歉意。
为了安装 NetCDF,我按照 git 指南中显示的步骤进行操作 here
我相信我的步骤是正确的。我正在尝试使用 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 指南中的以下行:
Make sure that either
nc-config
is in yourPATH
, or that the location ofnetCDFConfig.cmake
is inCMAKE_PREFIX_PATH
.
我已经尝试将 nc-config
添加到我的 PATH
,运行ning:
export PATH="nc-config:$PATH"
但这并不能解决问题...
此外,我试图在我的电脑中找到一个 netCDFConfig.cmake
文件,但它似乎不存在。我确实有一个 netCDFCxxConfig.cmake
,但我不确定这是否相同,或者我如何才能在这里使用它。
有没有人遇到过这个问题,知道如何解决?任何帮助将不胜感激,如果过去提供过这样的解决方案,我深表歉意。
编辑:
我能够让 CMake 最终找到该文件,但现在它在寻找另一个文件时迷路了...这是它在我的原始文件中找不到的文件 post:
# 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
.
我刚刚post编辑的文件在目录中:netcd-cxx4/build
netcdf-cxx4Targets.cmake
在目录中:
netcd-cxx4/build/CMakeFiles/Export/lib/cmake/netCDF
所以第一个应该能找到另一个?
netCDFCxxConfig.cmake
文件就是您要使用的文件。通过快速查看 GitHub repository,有一个用于此文件 (netCDFCxxConfig.cmake.in
) 的模板,但没有用于 netCDFConfig.cmake.in
的模板。因此,我的结论是维护者可能在某个时候更改了配置文件名,并且忘记将他们的 README 文档更新为 netCDFCxxConfig.cmake
.
您可以将 netCDFCxxConfig.cmake
文件的位置添加到 CMake 文件中的 CMAKE_PREFIX_PATH
列表变量。然后,link到netCDFCxxConfig.cmake
文件中定义的导入目标netCDF::netcdf
。
...
# ##############################################################################
# Don't do this.
<s>find_file(CMAKE_PREFIX_PATH netCDFCxxConfig.cmake)</s>
# 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.
<s>include_directories(${NETCDF_INCLUDES})</s>
# 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)
</pre>