Mac系统下在自己代码中使用netCDF库进行数据输出

netCDF是一个非常通用的数据格式,大量的软件都支持此格式的读和写,比如paraview, tecplot, surfer, gmt,matlab等等。所以如果自己写的计算程序将输出文件格式可选 为netcdf那将是很方便使用的。

安装netCDF

Mac系统下直接使用 brew install netcdf就可以安装最新版的netcdf,默认安装目录为 /usr/local/Cellar/netcdf/4.6.3_1,该目录下有include, lib文件夹,会编程序的都知道这意味着什么!

代码中使用netCDF

程序源码

比如一个简单的测试程序,来自netcdf源代码中的例子,这段C++代码是写一个简单的nc文件

#include 
#include 
#include 
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()
{
  int dataOut[NX][NY];
  for(int i = 0; i < NX; I++)
    for(int j = 0; j < NY; j++)
      dataOut[i][j] = i * NY + j;
  try
    {  
      NcFile dataFile("simple_xy.nc", NcFile::replace);
      NcDim xDim = dataFile.addDim("x", NX);
      NcDim yDim = dataFile.addDim("y", NY);
      vector dims;
      dims.push_back(xDim);
      dims.push_back(yDim);
      NcVar data = dataFile.addVar("data", ncInt, dims);
      data.putVar(dataOut);
      return 0; 
    }
  catch(NcException& e)
    {e.what();
      return NC_ERR;
    }
}

CMake文件

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)

# 项目信息
project (testnc)

# 添加头文件目录和链接库目录
include_directories(/usr/local/Cellar/netcdf/4.6.3_1/include )
link_directories(/usr/local/Cellar/netcdf/4.6.3_1/lib)

# 指定生成目标
add_executable(testnc main.cpp)

# 添加链接库
target_link_libraries(testnc netcdf-cxx4)

编译生成

创建一个build目录,然后进入build目录,使用命令 cmake ..生成makefile;最后用命令 make 就可以生成testnc可执行文件,这个可执行文件可以直接运行会生成一个simple_xy.nc的nc数据文件。

雷区

netcdf的lib文件夹下共有四个.a文件,我们在c++代码中要使用的是libnetcdf-cxx4.a这个文件

netcdf的链接库

如果设置为target_link_libraries(testnc netcdf)target_link_libraries(testnc netcdf_c++)都会导致如下的编译错误

[ 50%] Building CXX object CMakeFiles/testnc.dir/main.cpp.o
[100%] Linking CXX executable testnc
Undefined symbols for architecture x86_64:
  "netCDF::NcDim::NcDim(netCDF::NcDim const&)", referenced from:
      void std::__1::allocator::construct(netCDF::NcDim*, netCDF::NcDim const&) in main.cpp.o
  "netCDF::ncInt", referenced from:
      _main in main.cpp.o
  "netCDF::NcFile::NcFile(std::__1::basic_string, std::__1::allocator > const&, netCDF::Nc
File::FileMode)", referenced from:
      _main in main.cpp.o
  "netCDF::NcFile::~NcFile()", referenced from:
      _main in main.cpp.o
  "netCDF::NcVar::putVar(void const*) const", referenced from:
      _main in main.cpp.o
  "netCDF::NcGroup::addDim(std::__1::basic_string, std::__1::allocator > const&, unsigned 
long) const", referenced from:
      _main in main.cpp.o
  "netCDF::NcGroup::addVar(std::__1::basic_string, std::__1::allocator > const&, netCDF::NcType const&, std::__1::vector > const&) const", referenced from:
      _main in main.cpp.o
  "typeinfo for netCDF::exceptions::NcException", referenced from:
      GCC_except_table0 in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [testnc] Error 1
make[1]: *** [CMakeFiles/testnc.dir/all] Error 2
make: *** [all] Error 2

你可能感兴趣的:(Mac系统下在自己代码中使用netCDF库进行数据输出)