在 Visual Studio 上的 C++ 项目中将 NetCDF(通过 vcpkg 安装)与 CMake 一起使用时未解析的外部符号
Unresolved External Symbols When Using NetCDF (Installed via vcpkg) with CMake in a C++ Project on Visual Studio
该问题自最初发布以来已更新。我保留了所有内容以供将来人们使用,但最新的信息和问题在底部。
我发现一些 similar questions 以前发布过有关相关主题的帖子,但其中 none 似乎对我正在做的事情有一个清晰、简洁的解决方案。我希望通过问这个问题,我能得到答案,并且在互联网上也能为以后遇到类似问题的人提供更明确的答案。
我正在尝试在 Visual Studio 2019 年使用 NetCDF 创建一个简单的示例 C++ 代码,并将 CMake 作为构建系统。我使用 vcpkg
安装了 NetCDF,特别是命令
vcpkg install netcdf-cxx:x64-windows
安装了所有先决条件包和 netcdf-cxx:x64-windows 到 C:\Program Files\vcpkg\packages
.
在项目本身中,我有两个主要文件:main.cpp
和 CMakeLists.txt
。目录结构如下:
- C:\
- Users\
- Owner\
- Education and Research\
- Personal Projects\
- learning_coding\
- C++\
- cmake_netcdf\
- CMakeLists.txt
- main.cpp
文件main.cpp
是
#include <iostream>
#include <netcdf>
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
而 CMakeLists.txt
是
cmake_minimum_required (VERSION 3.17)
project(cmake_netcdf)
# Add source to this project's executable.
add_executable(cmake_netcdf main.cpp)
# Find and include the netcdf package
find_package(netcdf CONFIG REQUIRED)
target_link_libraries(cmake_netcdf
PRIVATE netcdf
)
Visual Studio可以成功刷新C++ IntelliSense信息,
1> CMake generation started for configuration: 'x64-Debug'.
1> Found and using vcpkg toolchain file (C:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake).
1> The toolchain file has changed (CMAKE_TOOLCHAIN_FILE).
1> Command line: "cmd.exe" /c ""C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO19\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\out\install\x64-Debug" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe" -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe" -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO19\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" -DCMAKE_TOOLCHAIN_FILE="C:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake" "C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf" 2>&1"
1> Working directory: C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug
1> [CMake] -- The C compiler identification is MSVC 19.27.29111.0
1> [CMake] -- The CXX compiler identification is MSVC 19.27.29111.0
1> [CMake] -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
1> [CMake] -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - works
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
1> [CMake] -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - works
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] -- Found ZLIB: optimized;C:/Program Files/vcpkg/installed/x64-windows/lib/zlib.lib;debug;C:/Program Files/vcpkg/installed/x64-windows/debug/lib/zlibd.lib (found version "1.2.11")
1> [CMake] -- Found ZLIB: optimized;C:/Program Files/vcpkg/installed/x64-windows/lib/zlib.lib;debug;C:/Program Files/vcpkg/installed/x64-windows/debug/lib/zlibd.lib (found suitable version "1.2.11", minimum required is "1")
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: C:/Users/Owner/Education and Research/Personal Projects/learning_coding/C++/cmake_netcdf/build/x64-Debug
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> Extracted includes paths.
1> CMake generation finished.
但项目无法构建并给出错误消息 cannot open source file "netcdf"
和 Cannot open include file: 'netcdf': No such file or directory
。我该如何解决这个错误?
编辑 1:跟进评论:
- “您能否在解决方案属性->包含目录中检查是否设置了 netcdf 包含的路径?” – 用户 3389943
此代码未保存为解决方案,但您确实提示我查看 CMake 变量。 netcdf 的目录变量是 netcdf_DIR = C:/Program Files/vcpkg/installed/x64-windows/share/netcdf-c
,看起来像
- C:/Program Files/vcpkg/installed/x64-windows/share/netcdf-c/
- copyright
- netCDFConfig.cmake
- netCDFConfigVersion.cmake
- netCDFTargets.cmake
- netCDFTargets-debug.cmake
- netCDFTargets-release.cmake
- usage
- vcpkg_abi_info.txt
netcdf_INCLUDES
或任何类似的变量都没有。它正在寻找 C++ 程序的 netcdf-c 是一个问题吗?我如何让 CMake 寻找合适的包含目录?
- “NetCDF 包中没有名为 netcdf 的 header。它应该读作#include “netcdf.h”。” – vre
进行该更改并不能修复错误,它会在将 'netcdf'
替换为 "netcdf.h"
时出现相同的错误。我认为正确的 include
是 netcdf
,除非 tutorials 是 out-of-date.
编辑 2:跟进评论:
- ““我认为正确的包含文件是 netcdf”:包含文件肯定被命名为 netcdf.h。您的 find_package 调用应该尝试找到 netCDF,此处区分大小写。在 netCDFTargets-debug.cmake 有一个用 add_library(<> IMPORTED ...) 定义的目标。将此目标名称用于您的 target_link_libraries 命令。” – vre
感谢您告诉我有关此文件的信息!文件 netCDFTargets-debug.cmake
读取
#----------------------------------------------------------------
# Generated CMake target import file for configuration "DEBUG".
#----------------------------------------------------------------
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Import target "netCDF::netcdf" for configuration "DEBUG"
set_property(TARGET netCDF::netcdf APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(netCDF::netcdf PROPERTIES
IMPORTED_IMPLIB_DEBUG "${_IMPORT_PREFIX}/debug/lib/netcdf.lib"
IMPORTED_LOCATION_DEBUG "${_IMPORT_PREFIX}/debug/bin/netcdf.dll"
)
list(APPEND _IMPORT_CHECK_TARGETS netCDF::netcdf )
list(APPEND _IMPORT_CHECK_FILES_FOR_netCDF::netcdf "${_IMPORT_PREFIX}/debug/lib/netcdf.lib" "${_IMPORT_PREFIX}/debug/bin/netcdf.dll" )
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
所以我将 CMakeLists.txt
更改为
# Unit test for building a program with netCDF using CMake in visual studio.
cmake_minimum_required (VERSION 3.17)
project(cmake_netcdf)
# Add source to this project's executable.
add_executable(cmake_netcdf main.cpp)
# Find and include the netcdf package
find_package(netCDF CONFIG REQUIRED)
target_link_libraries(cmake_netcdf
PRIVATE netCDF::netcdf
)
现在 main.cpp
构建!它使用 #include <netcdf>
和 #include "netcdf.h"
.
构建
从这里开始,我决定将 main.cpp
更改为 NCAR 网站上的示例文件,因此 main.cpp
现在是
/* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is a very simple example which writes a 2D array of
sample data. To handle this in netCDF we create two shared
dimensions, "x" and "y", and a netCDF variable, called "data".
This example is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C++ API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
$Id: simple_xy_wr.cpp,v 1.5 2010/02/11 22:36:43 russ Exp $
*/
#include <iostream>
#include <netcdf>
#include <vector>
using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;
// We are writing 2D data, a 6 x 12 grid.
static const int NX = 6;
static const int NY = 12;
// Return this in event of a problem.
static const int NC_ERR = 2;
int main()
{
// This is the data array we will write. It will just be filled
// with a progression of numbers for this example.
int dataOut[NX][NY];
// Create some pretend data. If this wasn't an example program, we
// would have some real data to write, for example, model output.
for (int i = 0; i < NX; i++)
for (int j = 0; j < NY; j++)
dataOut[i][j] = i * NY + j;
// The default behavior of the C++ API is to throw an exception i
// an error occurs. A try catch block is necessary.
try
{
// Create the file. The Replace parameter tells netCDF to overwrite
// this file, if it already exists.
NcFile dataFile("simple_xy.nc", NcFile::replace);
// Create netCDF dimensions
NcDim xDim = dataFile.addDim("x", NX);
NcDim yDim = dataFile.addDim("y", NY);
// Define the variable. The type of the variable in this case is
// ncInt (32-bit integer).
vector<NcDim> dims;
dims.push_back(xDim);
dims.push_back(yDim);
NcVar data = dataFile.addVar("data", ncInt, dims);
// Write the data to the file. Although netCDF supports
// reading and writing subsets of data, in this case we write all
// the data in one operation.
data.putVar(dataOut);
// The file will be automatically close when the NcFile object goes
// out of scope. This frees up any internal netCDF resources
// associated with the file, and flushes any buffers.
//cout << "*** SUCCESS writing example file simple_xy.nc!" << endl;
return 0;
}
catch (NcException& e)
{
e.what();
return NC_ERR;
}
}
尝试构建此示例文件时,我遇到了一堆链接器错误
>------ Build All started: Project: cmake_netcdf, Configuration: x64-Debug ------
[1/2] Building CXX object CMakeFiles\cmake_netcdf.dir\main.cpp.obj
[2/2] Linking CXX executable cmake_netcdf.exe
FAILED: cmake_netcdf.exe
cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmake_netcdf.dir --rc=C:\PROGRA~2\WI3CF2~1\bin0183~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\bin0183~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MICROS~119\COMMUN~1\VC\Tools\MSVC27~1.291\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmake_netcdf.dir\main.cpp.obj /out:cmake_netcdf.exe /implib:cmake_netcdf.lib /pdb:cmake_netcdf.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\netcdf.lib" "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\hdf5_hl_D.lib" "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\hdf5_D.lib" "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\libcurl-d.lib" "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\zlibd.lib" wldap32.lib winmm.lib ws2_32.lib advapi32.lib crypt32.lib advapi32.lib crypt32.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D "C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug" && powershell -noprofile -executionpolicy Bypass -file "C:/Program Files/vcpkg/scripts/buildsystems/msbuild/applocal.ps1" -targetBinary "C:/Users/Owner/Education and Research/Personal Projects/learning_coding/C++/cmake_netcdf/build/x64-Debug/cmake_netcdf.exe" -installedDir "C:/Program Files/vcpkg/installed/x64-windows/debug/bin" -OutVariable out""
LINK Pass 1: command "C:\PROGRA~2\MICROS~119\COMMUN~1\VC\Tools\MSVC27~1.291\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmake_netcdf.dir\main.cpp.obj /out:cmake_netcdf.exe /implib:cmake_netcdf.lib /pdb:cmake_netcdf.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console C:\Program Files\vcpkg\installed\x64-windows\debug\lib\netcdf.lib C:\Program Files\vcpkg\installed\x64-windows\debug\lib\hdf5_hl_D.lib C:\Program Files\vcpkg\installed\x64-windows\debug\lib\hdf5_D.lib C:\Program Files\vcpkg\installed\x64-windows\debug\lib\libcurl-d.lib C:\Program Files\vcpkg\installed\x64-windows\debug\lib\zlibd.lib wldap32.lib winmm.lib ws2_32.lib advapi32.lib crypt32.lib advapi32.lib crypt32.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmake_netcdf.dir/intermediate.manifest CMakeFiles\cmake_netcdf.dir/manifest.res" failed (exit code 1120) with the following output:
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl netCDF::NcDim::NcDim(class netCDF::NcDim const &)" (??0NcDim@netCDF@@QEAA@AEBV01@@Z) referenced in function "public: static void __cdecl std::_Default_allocator_traits<class std::allocator<class netCDF::NcDim> >::construct<class netCDF::NcDim,class netCDF::NcDim &>(class std::allocator<class netCDF::NcDim> &,class netCDF::NcDim * const,class netCDF::NcDim &)" (??$construct@VNcDim@netCDF@@AEAV12@@?$_Default_allocator_traits@V?$allocator@VNcDim@netCDF@@@std@@@std@@SAXAEAV?$allocator@VNcDim@netCDF@@@1@QEAVNcDim@netCDF@@AEAV34@@Z)
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: class netCDF::NcVar __cdecl netCDF::NcGroup::addVar(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class netCDF::NcType const &,class std::vector<class netCDF::NcDim,class std::allocator<class netCDF::NcDim> > const &)const " (?addVar@NcGroup@netCDF@@QEBA?AVNcVar@2@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBVNcType@2@AEBV?$vector@VNcDim@netCDF@@V?$allocator@VNcDim@netCDF@@@std@@@5@@Z) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: class netCDF::NcDim __cdecl netCDF::NcGroup::addDim(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,unsigned __int64)const " (?addDim@NcGroup@netCDF@@QEBA?AVNcDim@2@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_K@Z) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl netCDF::NcFile::NcFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,enum netCDF::NcFile::FileMode)" (??0NcFile@netCDF@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@W4FileMode@01@@Z) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl netCDF::NcFile::~NcFile(void)" (??1NcFile@netCDF@@UEAA@XZ) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: void __cdecl netCDF::NcVar::putVar(void const *)const " (?putVar@NcVar@netCDF@@QEBAXPEBX@Z) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2001: unresolved external symbol "class netCDF::NcInt netCDF::ncInt" (?ncInt@netCDF@@3VNcInt@1@A)
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\cmake_netcdf.exe : fatal error LNK1120: 7 unresolved externals
ninja: build stopped: subcommand failed.
Build All failed.
我尝试用 #include "netcdf.h"
替换 #include <netcdf>
但我得到了更多错误:
[1/2] Building CXX object CMakeFiles\cmake_netcdf.dir\main.cpp.obj
FAILED: CMakeFiles/cmake_netcdf.dir/main.cpp.obj
C:\PROGRA~2\MICROS~119\COMMUN~1\VC\Tools\MSVC27~1.291\bin\Hostx64\x64\cl.exe /nologo /TP -DDEBUG -DH5_BUILT_AS_DYNAMIC_LIB -I"C:\Program Files\vcpkg\installed\x64-windows\include" /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\cmake_netcdf.dir\main.cpp.obj /FdCMakeFiles\cmake_netcdf.dir\ /FS -c ..\..\main.cpp
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(22): error C2871: 'netCDF': a namespace with this name does not exist
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(23): error C2653: 'netCDF': is not a class or namespace name
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(23): error C2871: 'exceptions': a namespace with this name does not exist
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C2065: 'NcFile': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C2146: syntax error: missing ';' before identifier 'dataFile'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C2653: 'NcFile': is not a class or namespace name
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C2065: 'replace': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C3861: 'dataFile': identifier not found
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(56): error C2065: 'NcDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(56): error C2146: syntax error: missing ';' before identifier 'xDim'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(56): error C2065: 'xDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(56): error C2065: 'dataFile': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(57): error C2065: 'NcDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(57): error C2146: syntax error: missing ';' before identifier 'yDim'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(57): error C2065: 'yDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(57): error C2065: 'dataFile': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2065: 'NcDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2923: 'std::vector': 'NcDim' is not a valid template type argument for parameter '_Ty'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2976: 'std::vector': too few template arguments
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.27.29110\include\vector(413): note: see declaration of 'std::vector'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2133: 'dims': unknown size
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2512: 'std::vector': no appropriate default constructor available
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.27.29110\include\vector(413): note: see declaration of 'std::vector'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(62): error C2065: 'xDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(63): error C2065: 'yDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(64): error C2065: 'NcVar': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(64): error C2146: syntax error: missing ';' before identifier 'data'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(64): error C2065: 'dataFile': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(64): error C2065: 'ncInt': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(78): error C2061: syntax error: identifier 'NcException'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(78): error C2310: catch handlers must specify one type
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(80): error C2065: 'e': undeclared identifier
ninja: build stopped: subcommand failed.
Build All failed.
对这些新问题有什么见解吗? Google 搜索将我带到 this page,这表明 CMake 正在链接库的 32 位版本(而我的系统是 64 位),但我只有 64 位版本的已安装库。
此外,关于 #include <netcdf>
与 #include "netcdf.h"
,我在 C:\Program Files\vcpkg\installed\x64-windows\include
中都有这两个文件(以及一堆与 HDF5 和东西相关的 header 文件) .似乎 <netcdf>
是全部,而 netcdf.h
只是其中的一部分?
- "netcdf_DIR 是 find_package 搜索 header 和库的地方。你能像@vre 提到的那样交叉检查目标名称吗?" – 用户 3389943
我当然可以! netcdf_DIR
是C:/Program Files/vcpkg/installed/x64-windows/share/netcdf-c
,我在Edit 1.
中提到的内容
netCDFTargets.cmake
文件有行 add_library(netCDF::netcdf SHARED IMPORTED)
,我将其用于最新版本的 target_link_libraries
命令。
编辑 3:跟进评论:
- “那么 netCDFTargets.cmake 中还定义了哪些其他目标” – user3389943
我不太确定如何解析netCDFTargets.cmake
,所以这里是内容:
# Generated by CMake
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.5)
message(FATAL_ERROR "CMake >= 2.6.0 required")
endif()
cmake_policy(PUSH)
cmake_policy(VERSION 2.6)
#----------------------------------------------------------------
# Generated CMake target import file.
#----------------------------------------------------------------
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Protect against multiple inclusion, which would fail when already imported targets are added once more.
set(_targetsDefined)
set(_targetsNotDefined)
set(_expectedTargets)
foreach(_expectedTarget netCDF::netcdf)
list(APPEND _expectedTargets ${_expectedTarget})
if(NOT TARGET ${_expectedTarget})
list(APPEND _targetsNotDefined ${_expectedTarget})
endif()
if(TARGET ${_expectedTarget})
list(APPEND _targetsDefined ${_expectedTarget})
endif()
endforeach()
if("${_targetsDefined}" STREQUAL "${_expectedTargets}")
unset(_targetsDefined)
unset(_targetsNotDefined)
unset(_expectedTargets)
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)
return()
endif()
if(NOT "${_targetsDefined}" STREQUAL "")
message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\nTargets Defined: ${_targetsDefined}\nTargets not yet defined: ${_targetsNotDefined}\n")
endif()
unset(_targetsDefined)
unset(_targetsNotDefined)
unset(_expectedTargets)
# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
# Create imported target netCDF::netcdf
add_library(netCDF::netcdf SHARED IMPORTED)
set_target_properties(netCDF::netcdf PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include;${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "ZLIB::ZLIB;hdf5::hdf5-shared;hdf5::hdf5_hl-shared;CURL::libcurl"
)
if(CMAKE_VERSION VERSION_LESS 2.8.12)
message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.")
endif()
# Load information for each installed configuration.
get_filename_component(_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
file(GLOB CONFIG_FILES "${_DIR}/netCDFTargets-*.cmake")
foreach(f ${CONFIG_FILES})
include(${f})
endforeach()
# Cleanup temporary variables.
set(_IMPORT_PREFIX)
# Loop over all imported files and verify that they actually exist
foreach(target ${_IMPORT_CHECK_TARGETS} )
foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )
if(NOT EXISTS "${file}" )
message(FATAL_ERROR "The imported target \"${target}\" references the file
\"${file}\"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
\"${CMAKE_CURRENT_LIST_FILE}\"
but not all the files it references.
")
endif()
endforeach()
unset(_IMPORT_CHECK_FILES_FOR_${target})
endforeach()
unset(_IMPORT_CHECK_TARGETS)
# This file does not depend on other imported targets which have
# been exported from the same project but in a separate export set.
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)
有帮助吗?
好的,我找到了解决办法!它肯定不是一个完美的解决方案,但它是一个解决方案。
不幸的是,我无法让它在 Visual Studio 和 Windows 上运行。我可能会在某个时候再试一次,但我认为 netCDF-cxx4
的版本在 vcpkg
或其他什么地方已经过时了。我目前不在我的主要工作站,所以我将不得不在那里再试一次,看看我是否可以在 Visual Studio 中找到解决方案。无论如何,这就是我所做的似乎有效的事情:
- 我通过 Windows Subsystem for Linux (WSL). I'm not sure if this will work for older or newer versions of Ubuntu, but I assume that it will. I also am using the Windows Terminal that they mention on that page and set up my colors with the
vimrc
example file found here 下载了 Ubuntu 20.04.1。
这通过在 C:\
之上添加两层 /mnt/
来稍微改变文件系统的工作方式。您可以通过转到 /mnt/c/Users/
导航到 Windows 上的文件,通过转到 /
导航到 Ubuntu 子系统。我将我的 Ubuntu 帐户命名为 usr
,因此 'desktop' Ubuntu 文件保存在 /home/usr
中。对于所有后续步骤,我在 Windows 终端应用程序中使用 Ubuntu 终端 window。
启用 WSL 后通过 vcpkg
安装 zlib
也出现问题,但我不知道如何解决它,也没有在任何地方找到解决方案。
我通过 运行 sudo apt install build-essential
下载了一个 C++ 编译器,并通过 sudo apt install cmake libnetcdf-dev
.
安装了 CMake 和 netCDF-C
我通过将 GitHub repository 克隆到我的主目录 /home/usr
并使用命令
安装了 netCDF-cxx4
mkdir build
cd build
cmake ..
make
ctest
sudo make install
如果您没有sudo
,您将收到权限错误。
- 我在目录
/usr/local/lib/cmake/netcdf
中找到了netCDFCxxConfig.cmake
文件,并对CMakeLists.txt
文件做了一些修改
cmake_minimum_required(VERSION 3.8)
# Set project name and version
project(cmake_netcdfcxx4)
# Find the netcdf-cxx4 library
list(APPEND CMAKE_PREFIX_PATH "/usr/local/lib/cmake/netCDF")
find_package(netCDFCxx REQUIRED)
# Add source to this project's executable.
add_executable(cmake_netcdfcxx4 main.cpp)
# Link the netcdf-cxx4 library to the executable
target_link_libraries(cmake_netcdfcxx4
PRIVATE
netCDF::netcdf-cxx4
)
将此目录添加到 CMAKE_PREFIX_PATH
变量。我仍在研究如何自动将目录 /usr/local/lib/cmake/
添加到 CMake 搜索路径,这样用户就不必编辑 CMakeLists.txt
文件来正确编译它,所以我想这需要一些正在更新。
我希望这对以后的人有所帮助!
该问题自最初发布以来已更新。我保留了所有内容以供将来人们使用,但最新的信息和问题在底部。
我发现一些 similar questions 以前发布过有关相关主题的帖子,但其中 none 似乎对我正在做的事情有一个清晰、简洁的解决方案。我希望通过问这个问题,我能得到答案,并且在互联网上也能为以后遇到类似问题的人提供更明确的答案。
我正在尝试在 Visual Studio 2019 年使用 NetCDF 创建一个简单的示例 C++ 代码,并将 CMake 作为构建系统。我使用 vcpkg
安装了 NetCDF,特别是命令
vcpkg install netcdf-cxx:x64-windows
安装了所有先决条件包和 netcdf-cxx:x64-windows 到 C:\Program Files\vcpkg\packages
.
在项目本身中,我有两个主要文件:main.cpp
和 CMakeLists.txt
。目录结构如下:
- C:\
- Users\
- Owner\
- Education and Research\
- Personal Projects\
- learning_coding\
- C++\
- cmake_netcdf\
- CMakeLists.txt
- main.cpp
文件main.cpp
是
#include <iostream>
#include <netcdf>
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
而 CMakeLists.txt
是
cmake_minimum_required (VERSION 3.17)
project(cmake_netcdf)
# Add source to this project's executable.
add_executable(cmake_netcdf main.cpp)
# Find and include the netcdf package
find_package(netcdf CONFIG REQUIRED)
target_link_libraries(cmake_netcdf
PRIVATE netcdf
)
Visual Studio可以成功刷新C++ IntelliSense信息,
1> CMake generation started for configuration: 'x64-Debug'.
1> Found and using vcpkg toolchain file (C:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake).
1> The toolchain file has changed (CMAKE_TOOLCHAIN_FILE).
1> Command line: "cmd.exe" /c ""C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO19\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\out\install\x64-Debug" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe" -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe" -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO19\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" -DCMAKE_TOOLCHAIN_FILE="C:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake" "C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf" 2>&1"
1> Working directory: C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug
1> [CMake] -- The C compiler identification is MSVC 19.27.29111.0
1> [CMake] -- The CXX compiler identification is MSVC 19.27.29111.0
1> [CMake] -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
1> [CMake] -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - works
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
1> [CMake] -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - works
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] -- Found ZLIB: optimized;C:/Program Files/vcpkg/installed/x64-windows/lib/zlib.lib;debug;C:/Program Files/vcpkg/installed/x64-windows/debug/lib/zlibd.lib (found version "1.2.11")
1> [CMake] -- Found ZLIB: optimized;C:/Program Files/vcpkg/installed/x64-windows/lib/zlib.lib;debug;C:/Program Files/vcpkg/installed/x64-windows/debug/lib/zlibd.lib (found suitable version "1.2.11", minimum required is "1")
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: C:/Users/Owner/Education and Research/Personal Projects/learning_coding/C++/cmake_netcdf/build/x64-Debug
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> Extracted includes paths.
1> CMake generation finished.
但项目无法构建并给出错误消息 cannot open source file "netcdf"
和 Cannot open include file: 'netcdf': No such file or directory
。我该如何解决这个错误?
编辑 1:跟进评论:
- “您能否在解决方案属性->包含目录中检查是否设置了 netcdf 包含的路径?” – 用户 3389943
此代码未保存为解决方案,但您确实提示我查看 CMake 变量。 netcdf 的目录变量是 netcdf_DIR = C:/Program Files/vcpkg/installed/x64-windows/share/netcdf-c
,看起来像
- C:/Program Files/vcpkg/installed/x64-windows/share/netcdf-c/
- copyright
- netCDFConfig.cmake
- netCDFConfigVersion.cmake
- netCDFTargets.cmake
- netCDFTargets-debug.cmake
- netCDFTargets-release.cmake
- usage
- vcpkg_abi_info.txt
netcdf_INCLUDES
或任何类似的变量都没有。它正在寻找 C++ 程序的 netcdf-c 是一个问题吗?我如何让 CMake 寻找合适的包含目录?
- “NetCDF 包中没有名为 netcdf 的 header。它应该读作#include “netcdf.h”。” – vre
进行该更改并不能修复错误,它会在将 'netcdf'
替换为 "netcdf.h"
时出现相同的错误。我认为正确的 include
是 netcdf
,除非 tutorials 是 out-of-date.
编辑 2:跟进评论:
- ““我认为正确的包含文件是 netcdf”:包含文件肯定被命名为 netcdf.h。您的 find_package 调用应该尝试找到 netCDF,此处区分大小写。在 netCDFTargets-debug.cmake 有一个用 add_library(<> IMPORTED ...) 定义的目标。将此目标名称用于您的 target_link_libraries 命令。” – vre
感谢您告诉我有关此文件的信息!文件 netCDFTargets-debug.cmake
读取
#----------------------------------------------------------------
# Generated CMake target import file for configuration "DEBUG".
#----------------------------------------------------------------
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Import target "netCDF::netcdf" for configuration "DEBUG"
set_property(TARGET netCDF::netcdf APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(netCDF::netcdf PROPERTIES
IMPORTED_IMPLIB_DEBUG "${_IMPORT_PREFIX}/debug/lib/netcdf.lib"
IMPORTED_LOCATION_DEBUG "${_IMPORT_PREFIX}/debug/bin/netcdf.dll"
)
list(APPEND _IMPORT_CHECK_TARGETS netCDF::netcdf )
list(APPEND _IMPORT_CHECK_FILES_FOR_netCDF::netcdf "${_IMPORT_PREFIX}/debug/lib/netcdf.lib" "${_IMPORT_PREFIX}/debug/bin/netcdf.dll" )
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
所以我将 CMakeLists.txt
更改为
# Unit test for building a program with netCDF using CMake in visual studio.
cmake_minimum_required (VERSION 3.17)
project(cmake_netcdf)
# Add source to this project's executable.
add_executable(cmake_netcdf main.cpp)
# Find and include the netcdf package
find_package(netCDF CONFIG REQUIRED)
target_link_libraries(cmake_netcdf
PRIVATE netCDF::netcdf
)
现在 main.cpp
构建!它使用 #include <netcdf>
和 #include "netcdf.h"
.
从这里开始,我决定将 main.cpp
更改为 NCAR 网站上的示例文件,因此 main.cpp
现在是
/* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is a very simple example which writes a 2D array of
sample data. To handle this in netCDF we create two shared
dimensions, "x" and "y", and a netCDF variable, called "data".
This example is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C++ API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
$Id: simple_xy_wr.cpp,v 1.5 2010/02/11 22:36:43 russ Exp $
*/
#include <iostream>
#include <netcdf>
#include <vector>
using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;
// We are writing 2D data, a 6 x 12 grid.
static const int NX = 6;
static const int NY = 12;
// Return this in event of a problem.
static const int NC_ERR = 2;
int main()
{
// This is the data array we will write. It will just be filled
// with a progression of numbers for this example.
int dataOut[NX][NY];
// Create some pretend data. If this wasn't an example program, we
// would have some real data to write, for example, model output.
for (int i = 0; i < NX; i++)
for (int j = 0; j < NY; j++)
dataOut[i][j] = i * NY + j;
// The default behavior of the C++ API is to throw an exception i
// an error occurs. A try catch block is necessary.
try
{
// Create the file. The Replace parameter tells netCDF to overwrite
// this file, if it already exists.
NcFile dataFile("simple_xy.nc", NcFile::replace);
// Create netCDF dimensions
NcDim xDim = dataFile.addDim("x", NX);
NcDim yDim = dataFile.addDim("y", NY);
// Define the variable. The type of the variable in this case is
// ncInt (32-bit integer).
vector<NcDim> dims;
dims.push_back(xDim);
dims.push_back(yDim);
NcVar data = dataFile.addVar("data", ncInt, dims);
// Write the data to the file. Although netCDF supports
// reading and writing subsets of data, in this case we write all
// the data in one operation.
data.putVar(dataOut);
// The file will be automatically close when the NcFile object goes
// out of scope. This frees up any internal netCDF resources
// associated with the file, and flushes any buffers.
//cout << "*** SUCCESS writing example file simple_xy.nc!" << endl;
return 0;
}
catch (NcException& e)
{
e.what();
return NC_ERR;
}
}
尝试构建此示例文件时,我遇到了一堆链接器错误
>------ Build All started: Project: cmake_netcdf, Configuration: x64-Debug ------
[1/2] Building CXX object CMakeFiles\cmake_netcdf.dir\main.cpp.obj
[2/2] Linking CXX executable cmake_netcdf.exe
FAILED: cmake_netcdf.exe
cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmake_netcdf.dir --rc=C:\PROGRA~2\WI3CF2~1\bin0183~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\bin0183~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MICROS~119\COMMUN~1\VC\Tools\MSVC27~1.291\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmake_netcdf.dir\main.cpp.obj /out:cmake_netcdf.exe /implib:cmake_netcdf.lib /pdb:cmake_netcdf.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\netcdf.lib" "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\hdf5_hl_D.lib" "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\hdf5_D.lib" "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\libcurl-d.lib" "C:\Program Files\vcpkg\installed\x64-windows\debug\lib\zlibd.lib" wldap32.lib winmm.lib ws2_32.lib advapi32.lib crypt32.lib advapi32.lib crypt32.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D "C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug" && powershell -noprofile -executionpolicy Bypass -file "C:/Program Files/vcpkg/scripts/buildsystems/msbuild/applocal.ps1" -targetBinary "C:/Users/Owner/Education and Research/Personal Projects/learning_coding/C++/cmake_netcdf/build/x64-Debug/cmake_netcdf.exe" -installedDir "C:/Program Files/vcpkg/installed/x64-windows/debug/bin" -OutVariable out""
LINK Pass 1: command "C:\PROGRA~2\MICROS~119\COMMUN~1\VC\Tools\MSVC27~1.291\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmake_netcdf.dir\main.cpp.obj /out:cmake_netcdf.exe /implib:cmake_netcdf.lib /pdb:cmake_netcdf.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console C:\Program Files\vcpkg\installed\x64-windows\debug\lib\netcdf.lib C:\Program Files\vcpkg\installed\x64-windows\debug\lib\hdf5_hl_D.lib C:\Program Files\vcpkg\installed\x64-windows\debug\lib\hdf5_D.lib C:\Program Files\vcpkg\installed\x64-windows\debug\lib\libcurl-d.lib C:\Program Files\vcpkg\installed\x64-windows\debug\lib\zlibd.lib wldap32.lib winmm.lib ws2_32.lib advapi32.lib crypt32.lib advapi32.lib crypt32.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmake_netcdf.dir/intermediate.manifest CMakeFiles\cmake_netcdf.dir/manifest.res" failed (exit code 1120) with the following output:
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl netCDF::NcDim::NcDim(class netCDF::NcDim const &)" (??0NcDim@netCDF@@QEAA@AEBV01@@Z) referenced in function "public: static void __cdecl std::_Default_allocator_traits<class std::allocator<class netCDF::NcDim> >::construct<class netCDF::NcDim,class netCDF::NcDim &>(class std::allocator<class netCDF::NcDim> &,class netCDF::NcDim * const,class netCDF::NcDim &)" (??$construct@VNcDim@netCDF@@AEAV12@@?$_Default_allocator_traits@V?$allocator@VNcDim@netCDF@@@std@@@std@@SAXAEAV?$allocator@VNcDim@netCDF@@@1@QEAVNcDim@netCDF@@AEAV34@@Z)
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: class netCDF::NcVar __cdecl netCDF::NcGroup::addVar(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class netCDF::NcType const &,class std::vector<class netCDF::NcDim,class std::allocator<class netCDF::NcDim> > const &)const " (?addVar@NcGroup@netCDF@@QEBA?AVNcVar@2@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBVNcType@2@AEBV?$vector@VNcDim@netCDF@@V?$allocator@VNcDim@netCDF@@@std@@@5@@Z) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: class netCDF::NcDim __cdecl netCDF::NcGroup::addDim(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,unsigned __int64)const " (?addDim@NcGroup@netCDF@@QEBA?AVNcDim@2@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_K@Z) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl netCDF::NcFile::NcFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,enum netCDF::NcFile::FileMode)" (??0NcFile@netCDF@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@W4FileMode@01@@Z) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl netCDF::NcFile::~NcFile(void)" (??1NcFile@netCDF@@UEAA@XZ) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: void __cdecl netCDF::NcVar::putVar(void const *)const " (?putVar@NcVar@netCDF@@QEBAXPEBX@Z) referenced in function main
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\main.cpp.obj : error LNK2001: unresolved external symbol "class netCDF::NcInt netCDF::ncInt" (?ncInt@netCDF@@3VNcInt@1@A)
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\build\x64-Debug\cmake_netcdf.exe : fatal error LNK1120: 7 unresolved externals
ninja: build stopped: subcommand failed.
Build All failed.
我尝试用 #include "netcdf.h"
替换 #include <netcdf>
但我得到了更多错误:
[1/2] Building CXX object CMakeFiles\cmake_netcdf.dir\main.cpp.obj
FAILED: CMakeFiles/cmake_netcdf.dir/main.cpp.obj
C:\PROGRA~2\MICROS~119\COMMUN~1\VC\Tools\MSVC27~1.291\bin\Hostx64\x64\cl.exe /nologo /TP -DDEBUG -DH5_BUILT_AS_DYNAMIC_LIB -I"C:\Program Files\vcpkg\installed\x64-windows\include" /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\cmake_netcdf.dir\main.cpp.obj /FdCMakeFiles\cmake_netcdf.dir\ /FS -c ..\..\main.cpp
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(22): error C2871: 'netCDF': a namespace with this name does not exist
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(23): error C2653: 'netCDF': is not a class or namespace name
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(23): error C2871: 'exceptions': a namespace with this name does not exist
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C2065: 'NcFile': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C2146: syntax error: missing ';' before identifier 'dataFile'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C2653: 'NcFile': is not a class or namespace name
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C2065: 'replace': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(53): error C3861: 'dataFile': identifier not found
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(56): error C2065: 'NcDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(56): error C2146: syntax error: missing ';' before identifier 'xDim'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(56): error C2065: 'xDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(56): error C2065: 'dataFile': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(57): error C2065: 'NcDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(57): error C2146: syntax error: missing ';' before identifier 'yDim'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(57): error C2065: 'yDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(57): error C2065: 'dataFile': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2065: 'NcDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2923: 'std::vector': 'NcDim' is not a valid template type argument for parameter '_Ty'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2976: 'std::vector': too few template arguments
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.27.29110\include\vector(413): note: see declaration of 'std::vector'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2133: 'dims': unknown size
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(61): error C2512: 'std::vector': no appropriate default constructor available
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.27.29110\include\vector(413): note: see declaration of 'std::vector'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(62): error C2065: 'xDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(63): error C2065: 'yDim': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(64): error C2065: 'NcVar': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(64): error C2146: syntax error: missing ';' before identifier 'data'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(64): error C2065: 'dataFile': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(64): error C2065: 'ncInt': undeclared identifier
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(78): error C2061: syntax error: identifier 'NcException'
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(78): error C2310: catch handlers must specify one type
C:\Users\Owner\Education and Research\Personal Projects\learning_coding\C++\cmake_netcdf\main.cpp(80): error C2065: 'e': undeclared identifier
ninja: build stopped: subcommand failed.
Build All failed.
对这些新问题有什么见解吗? Google 搜索将我带到 this page,这表明 CMake 正在链接库的 32 位版本(而我的系统是 64 位),但我只有 64 位版本的已安装库。
此外,关于 #include <netcdf>
与 #include "netcdf.h"
,我在 C:\Program Files\vcpkg\installed\x64-windows\include
中都有这两个文件(以及一堆与 HDF5 和东西相关的 header 文件) .似乎 <netcdf>
是全部,而 netcdf.h
只是其中的一部分?
- "netcdf_DIR 是 find_package 搜索 header 和库的地方。你能像@vre 提到的那样交叉检查目标名称吗?" – 用户 3389943
我当然可以! netcdf_DIR
是C:/Program Files/vcpkg/installed/x64-windows/share/netcdf-c
,我在Edit 1.
netCDFTargets.cmake
文件有行 add_library(netCDF::netcdf SHARED IMPORTED)
,我将其用于最新版本的 target_link_libraries
命令。
编辑 3:跟进评论:
- “那么 netCDFTargets.cmake 中还定义了哪些其他目标” – user3389943
我不太确定如何解析netCDFTargets.cmake
,所以这里是内容:
# Generated by CMake
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.5)
message(FATAL_ERROR "CMake >= 2.6.0 required")
endif()
cmake_policy(PUSH)
cmake_policy(VERSION 2.6)
#----------------------------------------------------------------
# Generated CMake target import file.
#----------------------------------------------------------------
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Protect against multiple inclusion, which would fail when already imported targets are added once more.
set(_targetsDefined)
set(_targetsNotDefined)
set(_expectedTargets)
foreach(_expectedTarget netCDF::netcdf)
list(APPEND _expectedTargets ${_expectedTarget})
if(NOT TARGET ${_expectedTarget})
list(APPEND _targetsNotDefined ${_expectedTarget})
endif()
if(TARGET ${_expectedTarget})
list(APPEND _targetsDefined ${_expectedTarget})
endif()
endforeach()
if("${_targetsDefined}" STREQUAL "${_expectedTargets}")
unset(_targetsDefined)
unset(_targetsNotDefined)
unset(_expectedTargets)
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)
return()
endif()
if(NOT "${_targetsDefined}" STREQUAL "")
message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\nTargets Defined: ${_targetsDefined}\nTargets not yet defined: ${_targetsNotDefined}\n")
endif()
unset(_targetsDefined)
unset(_targetsNotDefined)
unset(_expectedTargets)
# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
# Create imported target netCDF::netcdf
add_library(netCDF::netcdf SHARED IMPORTED)
set_target_properties(netCDF::netcdf PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include;${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "ZLIB::ZLIB;hdf5::hdf5-shared;hdf5::hdf5_hl-shared;CURL::libcurl"
)
if(CMAKE_VERSION VERSION_LESS 2.8.12)
message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.")
endif()
# Load information for each installed configuration.
get_filename_component(_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
file(GLOB CONFIG_FILES "${_DIR}/netCDFTargets-*.cmake")
foreach(f ${CONFIG_FILES})
include(${f})
endforeach()
# Cleanup temporary variables.
set(_IMPORT_PREFIX)
# Loop over all imported files and verify that they actually exist
foreach(target ${_IMPORT_CHECK_TARGETS} )
foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )
if(NOT EXISTS "${file}" )
message(FATAL_ERROR "The imported target \"${target}\" references the file
\"${file}\"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
\"${CMAKE_CURRENT_LIST_FILE}\"
but not all the files it references.
")
endif()
endforeach()
unset(_IMPORT_CHECK_FILES_FOR_${target})
endforeach()
unset(_IMPORT_CHECK_TARGETS)
# This file does not depend on other imported targets which have
# been exported from the same project but in a separate export set.
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)
有帮助吗?
好的,我找到了解决办法!它肯定不是一个完美的解决方案,但它是一个解决方案。
不幸的是,我无法让它在 Visual Studio 和 Windows 上运行。我可能会在某个时候再试一次,但我认为 netCDF-cxx4
的版本在 vcpkg
或其他什么地方已经过时了。我目前不在我的主要工作站,所以我将不得不在那里再试一次,看看我是否可以在 Visual Studio 中找到解决方案。无论如何,这就是我所做的似乎有效的事情:
- 我通过 Windows Subsystem for Linux (WSL). I'm not sure if this will work for older or newer versions of Ubuntu, but I assume that it will. I also am using the Windows Terminal that they mention on that page and set up my colors with the
vimrc
example file found here 下载了 Ubuntu 20.04.1。
这通过在 C:\
之上添加两层 /mnt/
来稍微改变文件系统的工作方式。您可以通过转到 /mnt/c/Users/
导航到 Windows 上的文件,通过转到 /
导航到 Ubuntu 子系统。我将我的 Ubuntu 帐户命名为 usr
,因此 'desktop' Ubuntu 文件保存在 /home/usr
中。对于所有后续步骤,我在 Windows 终端应用程序中使用 Ubuntu 终端 window。
启用 WSL 后通过 vcpkg
安装 zlib
也出现问题,但我不知道如何解决它,也没有在任何地方找到解决方案。
我通过 运行
安装了 CMake 和 netCDF-Csudo apt install build-essential
下载了一个 C++ 编译器,并通过sudo apt install cmake libnetcdf-dev
.我通过将 GitHub repository 克隆到我的主目录
安装了/home/usr
并使用命令netCDF-cxx4
mkdir build
cd build
cmake ..
make
ctest
sudo make install
如果您没有sudo
,您将收到权限错误。
- 我在目录
/usr/local/lib/cmake/netcdf
中找到了netCDFCxxConfig.cmake
文件,并对CMakeLists.txt
文件做了一些修改
cmake_minimum_required(VERSION 3.8)
# Set project name and version
project(cmake_netcdfcxx4)
# Find the netcdf-cxx4 library
list(APPEND CMAKE_PREFIX_PATH "/usr/local/lib/cmake/netCDF")
find_package(netCDFCxx REQUIRED)
# Add source to this project's executable.
add_executable(cmake_netcdfcxx4 main.cpp)
# Link the netcdf-cxx4 library to the executable
target_link_libraries(cmake_netcdfcxx4
PRIVATE
netCDF::netcdf-cxx4
)
将此目录添加到 CMAKE_PREFIX_PATH
变量。我仍在研究如何自动将目录 /usr/local/lib/cmake/
添加到 CMake 搜索路径,这样用户就不必编辑 CMakeLists.txt
文件来正确编译它,所以我想这需要一些正在更新。
我希望这对以后的人有所帮助!