From 0c6e4bdad73a0b047835e0426ad1fc124ec01fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=A3=B9?= Date: Sun, 1 Aug 2021 14:37:01 +0800 Subject: [PATCH] update build system and netcdf.cpp --- CMakeLists.txt | 7 ++++++- README.md | 2 +- config.sh | 16 ++++++++++++++++ src/CMakeLists.txt | 28 ++++++++++++++-------------- src/lib/netcdf.cpp | 16 ++++++++++++++++ 5 files changed, 53 insertions(+), 16 deletions(-) create mode 100755 config.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ffe34e..6db9fcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,12 @@ cmake_minimum_required(VERSION 3.15.2) project(NETCDF_CXX) message(STATUS "Platform: " ${CMAKE_HOST_SYSTEM_NAME}) -set(CMAKE_INSTALL_PREFIX "D:/Library") +if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") + #set(CMAKE_C_COMPILER gcc) + #set(CMAKE_CXX_COMPILER g++) + set(CMAKE_INSTALL_PREFIX D:/Library) +endif() +message(STATUS "Install prefix: " ${CMAKE_INSTALL_PREFIX}) # 添加源文件地址 add_subdirectory(src/) \ No newline at end of file diff --git a/README.md b/README.md index 2eca0af..0618c20 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# netcdfcxx_win +# netcdfcxx_legacy #### 介绍 一个windows下netcdf c++接口的编译项目,注意使用的是4.2版本的老接口。 diff --git a/config.sh b/config.sh new file mode 100755 index 0000000..19e4c6d --- /dev/null +++ b/config.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +cmd=${1} +package=netcdfcxx_legacy +address=/opt + +if [[ ${cmd} == "configure" && ! -d "build/" ]]; then + mkdir build && cd build && cmake .. -DCMAKE_INSTALL_PREFIX=${address}/stow/${package} -DCMAKE_BUILD_TYPE=Release +elif [[ ${cmd} == "configure" ]]; then + cd build && rm -rf * && cmake .. -DCMAKE_INSTALL_PREFIX=${address}/stow/${package} -DCMAKE_BUILD_TYPE=Release +elif [[ ${cmd} == "build" ]]; then + cd build && make +elif [[ ${cmd} == "install" ]]; then + cd build && sudo make install + sudo stow --dir=${address}/stow --target=${address} ${package} +fi \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5eb7632..9de8ce1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,8 @@ # 设定源文件文件夹 aux_source_directory(lib/ NETCDF_SRC) # 设置netcdf c接口的库文件地址 -set(LIB_NETCDF_INC C:/Program\ Files/netCDF\ 4.8.0/include) -set(LIB_NETCDF_LIB C:/Program\ Files/netCDF\ 4.8.0/lib) +set(LIB_NETCDF_INC /opt/homebrew/include) +set(LIB_NETCDF_LIB /opt/homebrew/lib) # netcdf安装后的动态库与静态库的地址不一样 需要手动在cmake生成的VS工程内添加下面的库地址 # 方法如下:gctl->属性->链接器->附件库目录(选择编辑,并选择netcdf的可执行文件地址) #set(LIB_NETCDF_BIN C:/Program\ Files/netCDF\ 4.8.0/bin) @@ -17,24 +17,24 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) # 以下部分为库的编译 # 注意目标名必须唯一 所以不能直接生成相同名称的动态库与静态库 # 注意此处不必为目标名称添加lib前缀和相应后缀,cmake会自行添加 -add_library(netcdf_c++ SHARED ${NETCDF_SRC}) +add_library(netcdfcxx_legacy SHARED ${NETCDF_SRC}) # 首先添加静态库的生成命令 -add_library(netcdf_c++_static STATIC ${NETCDF_SRC}) +add_library(netcdfcxx_legacy_static STATIC ${NETCDF_SRC}) # 设置静态库的输出名称从而获得与动态库名称相同的静态库 -set_target_properties(netcdf_c++_static PROPERTIES OUTPUT_NAME "netcdf_c++") +set_target_properties(netcdfcxx_legacy_static PROPERTIES OUTPUT_NAME "netcdfcxx_legacy") # 设置输出目标属性以同时输出动态库与静态库 -set_target_properties(netcdf_c++ PROPERTIES CLEAN_DIRECT_OUTPUT 1) -set_target_properties(netcdf_c++_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) +set_target_properties(netcdfcxx_legacy PROPERTIES CLEAN_DIRECT_OUTPUT 1) +set_target_properties(netcdfcxx_legacy_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) # 设置动态库的版本号 -set_target_properties(netcdf_c++ PROPERTIES VERSION 1.0 SOVERSION 1.0) +set_target_properties(netcdfcxx_legacy PROPERTIES VERSION 1.0 SOVERSION 1.0) find_library(NETCDF_LIBRARY netcdf ${LIB_NETCDF_LIB}) -target_link_libraries(netcdf_c++ PUBLIC ${NETCDF_LIBRARY}) -target_link_libraries(netcdf_c++_static ${NETCDF_LIBRARY}) +target_link_libraries(netcdfcxx_legacy PUBLIC ${NETCDF_LIBRARY}) +target_link_libraries(netcdfcxx_legacy_static ${NETCDF_LIBRARY}) # 库的安装命令 -install(TARGETS netcdf_c++ DESTINATION lib) -install(TARGETS netcdf_c++_static DESTINATION lib) +install(TARGETS netcdfcxx_legacy DESTINATION lib) +install(TARGETS netcdfcxx_legacy_static DESTINATION lib) # 头文件安装命令 install(FILES lib/netcdfcpp.h DESTINATION include) install(FILES lib/ncvalues.h DESTINATION include) @@ -48,9 +48,9 @@ macro(add_sample name) # 添加可执行文件 命令行 add_executable(${name} example/${name}.cpp) # 为安装文件添加动态库的搜索地址 在Windows下并没有什么用 直接忽略 - set_target_properties(${name} PROPERTIES INSTALL_RPATH D:/Library/lib) + set_target_properties(${name} PROPERTIES INSTALL_RPATH /opt/lib) # 链接动态库 - target_link_libraries(${name} PUBLIC netcdf_c++) + target_link_libraries(${name} PUBLIC netcdfcxx_legacy) endmacro() add_sample(pres_temp_4D_rd) diff --git a/src/lib/netcdf.cpp b/src/lib/netcdf.cpp index de5c67f..a2fb4cc 100644 --- a/src/lib/netcdf.cpp +++ b/src/lib/netcdf.cpp @@ -648,6 +648,7 @@ long* NcVar::edges( void ) const // edge lengths (dimension sizes) int NcVar::num_atts( void ) const // handles variable and global atts { + /* int natt = 0; if (the_file->is_valid()) if (the_id == ncGlobal) @@ -657,6 +658,21 @@ int NcVar::num_atts( void ) const // handles variable and global atts nc_inq_varnatts(the_file->id(), the_id, &natt) ); return natt; + */ + + int natt = 0; + if (the_file->is_valid()) + { + if (the_id == ncGlobal) + { + natt = the_file->num_atts(); + } + else + { + NcError::set_err(nc_inq_varnatts(the_file->id(), the_id, &natt)); + } + } + return natt; } NcAtt* NcVar::get_att( NcToken aname ) const