问题
I'm trying to include/link the NetCDF C library (https://www.unidata.ucar.edu/software/netcdf/docs/index.html) into my C++/CUDA project with cmake. I seem to be running into issues, though.
I am on Windows 10 and have installed the NetCDF C libraries for Windows, version 4.7. Including the NetCDF libraries seems to go without a hitch, the problem is in the linking step.
My code that uses the NetCDF library:
netcdf_writer.cpp
#ifndef __CUDACC__
//only actually include the NetCDF libraries if we aren't in CUDA
#include <netcdf.h>
#endif
void write_full_netcdf_file(string filename, Configuration cfg) {
int ncid, x_dimid;
int retval;
if ((retval = nc_create(filename.c_str(), NC_CLOBBER, &ncid))) {
ERR(retval);
}
/* Define the dimensions. NetCDF will hand back an ID for each. */
if ((retval = nc_def_dim(ncid, "x", cfg.nx, &x_dimid)))
ERR(retval);
}
Note that this code is appropriately split into header/cpp files, I've just consolidated here for simplicity.
A truncated version of my CMakeLists.txt file is below. Note that I use VTK's FindNetCDF.cmake: https://github.com/Kitware/VTK/blob/master/CMake/FindNetCDF.cmake to do the finding.
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(cmake_and_cuda LANGUAGES CXX CUDA)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
include(CTest)
#add in all CUDA and C++ files along with their associated headers.
file(GLOB_RECURSE sources src/*.cu src/*.cpp)
file(GLOB_RECURSE headers "src/*.h" "src/*.hpp" "src/*.cuh")
find_package(NetCDF 4.7 REQUIRED)
INCLUDE_DIRECTORIES(${NetCDF_INCLUDE_DIRS})
set(LIBS ${LIBS} ${NetCDF_LIBRARIES})
set (Foo_INCLUDE_DIRS "")
foreach (_headerFile ${headers})
get_filename_component(_dir ${_headerFile} PATH)
list (APPEND Foo_INCLUDE_DIRS ${_dir})
endforeach()
list(REMOVE_DUPLICATES Foo_INCLUDE_DIRS)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -g")
# We need to explicitly state that we need all CUDA files in the
# particle library to be built with -dc as the member functions
# could be called by other libraries and executables
add_executable(foo ${sources})
set_property(TARGET foo
PROPERTY CUDA_SEPARABLE_COMPILATION ON)
target_include_directories(foo PRIVATE ${Foo_INCLUDE_DIRS})
target_link_libraries(foo PRIVATE ${LIBS} )
target_link_libraries(foo PUBLIC NetCDF::NetCDF)
When I run cmake, I get the following:
CMake Error at CMakeLists.txt:45 (add_executable):
Target "foo" links to target "hdf5::hdf5-shared" but the target was not
found. Perhaps a find_package() call is missing for an IMPORTED target, or
an ALIAS target is missing?
CMake Error at CMakeLists.txt:45 (add_executable):
Target "foo" links to target "hdf5::hdf5_hl-shared" but the target was not
found. Perhaps a find_package() call is missing for an IMPORTED target, or
an ALIAS target is missing?
...
What I've tried:
- I've tried including/linking the HDF5 library explicitly (although NetCDF should provide this already). This results in the same error.
- I've tried explicitly linking the two HDF5 libraries:
target_link_libraries(mac PUBLIC hdf5::hdf5-shared hdf5::hdf5_hl-shared)
. Again, this results in the same error.
来源:https://stackoverflow.com/questions/61232966/netcdf-library-linkage-on-windows-with-cmake