所以我试图将netCDF与C ++一起使用。安装是正确的(我知道,因为我在集群上运行,并且我知道它对那里的其他人都有效)。甚至netCDF网页上的示例代码也无法正常工作...
我正在使用g ++ -std = c ++ 11进行编译。每当我尝试编译使用netCDF的几个示例代码之一时,都会遇到很多错误,而且我不知道发生了什么……
#include <vector>
#include <netcdf>
using namespace netCDF;
int main() {
int nx = 6, ny = 12;
int dataOut[nx][ny];
for(int i = 0; i < nx; i++)
for(int j = 0; j < ny; j++)
dataOut[i][j] = i * ny + j;
// Create the netCDF file.
NcFile dataFile("1st.netCDF.nc",NcFile::replace);
// Create the two dimensions.
NcDim xDim = dataFile.addDim("x",nx);
NcDim yDim = dataFile.addDim("y",ny);
std::vector<NcDim> dims(2);
dims[0] = xDim;
dims[1] = yDim;
// Create the data variable.
NcVar data = dataFile.addVar("data", ncInt, dims);
// Put the data in the file.
data.putVar(&dataOut);
// Add an attribute.
dataFile.putAtt("Creation date:",
"12 Dec 2014");
return 0;
}
预期结果是对代码的正确编译,并且一旦运行它以获取记录有dataOut的.nc文件。这是我尝试编译时的实际输出:
/tmp/ccyuchst.o:在函数main':
rarray_2_netcdf.cc:(.text+0x1c6): undefined reference to
中的netCDF :: NcFile :: NcFile(std :: __ cxx11 :: basic_string,std :: allocator> const&,netCDF :: NcFile :: FileMode))
rarray_2_netcdf.cc:(.text+0x234):对netCDF::NcGroup::addDim(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long) const'
rarray_2_netcdf.cc:(.text+0x2a2): undefined reference to
netCDF :: NcGroup :: addDim(std :: __ cxx11 :: basic_string,std :: allocator> const&,unsigned long)的未定义引用const'
rarray_2_netcdf.cc:(.text+0x322):对netCDF::NcDim::operator=(netCDF::NcDim const&)'
rarray_2_netcdf.cc:(.text+0x34b): undefined reference to
netCDF :: NcDim :: operator =(netCDF :: NcDim const&)的未定义引用
rarray_2_netcdf.cc:(.text+0x399):对netCDF::ncInt'
rarray_2_netcdf.cc:(.text+0x3a1): undefined reference to
netCDF :: NcGroup :: addVar(std :: __ cxx11 :: basic_string,std :: allocator> const&,netCDF :: NcType const&,std的未定义引用: :vector> const&)const'
rarray_2_netcdf.cc:(.text+0x3d5):对netCDF::NcVar::putVar(void const*) const'
rarray_2_netcdf.cc:(.text+0x441): undefined reference to
netCDF :: NcGroup :: putAtt(std :: __ cxx11 :: basic_string,std :: allocator> const&,std :: __ cxx11 :: basic_string,的未定义引用, std :: allocator> const&)const'
rarray_2_netcdf.cc:(.text+0x4d6):对netCDF::NcFile::~NcFile()'
rarray_2_netcdf.cc:(.text+0x63e): undefined reference to
netCDF :: NcFile ::〜NcFile()'的未定义引用
/tmp/ccyuchst.o:在函数void std::_Construct<netCDF::NcDim>(netCDF::NcDim*)':
rarray_2_netcdf.cc:(.text._ZSt10_ConstructIN6netCDF5NcDimEJEEvPT_DpOT0_[_ZSt10_ConstructIN6netCDF5NcDimEJEEvPT_DpOT0_]+0x2e): undefined reference to
netCDF :: NcDim :: NcDim()中
/tmp/ccyuchst.o:在函数netCDF::NcGroupAtt::~NcGroupAtt()':
rarray_2_netcdf.cc:(.text._ZN6netCDF10NcGroupAttD2Ev[_ZN6netCDF10NcGroupAttD5Ev]+0x20): undefined reference to
netCDF :: NcAtt ::〜NcAtt()'中
collect2:错误:ld返回1退出状态