update build system and netcdf.cpp

This commit is contained in:
张壹
2021-08-01 14:37:01 +08:00
parent 7428b5a6f0
commit 0c6e4bdad7
5 changed files with 53 additions and 16 deletions

View File

@@ -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/)

View File

@@ -1,4 +1,4 @@
# netcdfcxx_win
# netcdfcxx_legacy
#### 介绍
一个windows下netcdf c++接口的编译项目注意使用的是4.2版本的老接口。

16
config.sh Executable file
View File

@@ -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

View File

@@ -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)

View File

@@ -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