如何在ubuntu中静态链接netcdf程序

hba*_*osa 5 ubuntu static g++ build netcdf

我正在尝试使用netcdf库和g ++静态链接一个非常简单的程序。该程序如下所示:

#include "netcdf.h"
int main(void)
{
  int ncid, ok;
  ok=nc_create("testing.nc", NC_NOCLOBBER, &ncid);
  ok=nc_close(ncid);
  return(0);
}
Run Code Online (Sandbox Code Playgroud)

如果我像这样编译它,效果很好:

> g++ test.cpp -lnetcdf
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试静态编译它,那么一切都会中断:

> g++ -static test.cpp -lnetcdf
/usr/lib64/libnetcdf.a(nc4file.o): In function `sync_netcdf4_file':
(.text+0x5a): undefined reference to `H5Fflush'
/usr/lib64/libnetcdf.a(nc4file.o): In function `close_netcdf4_file':
(.text+0x11b): undefined reference to `H5Fclose'
/usr/lib64/libnetcdf.a(nc4file.o): In function `get_netcdf_type':
(.text+0x18b): undefined reference to `H5Tget_class'
/usr/lib64/libnetcdf.a(nc4file.o): In function `get_netcdf_type':
(.text+0x1e9): undefined reference to `H5Tis_variable_str'
(...)
Run Code Online (Sandbox Code Playgroud)

许多抱怨,但第一个是针对hdf5。在寻找答案时,我 unidata上找到了该页面该页面解释了应该与hdf5和其他库链接的地方。所以我尝试了:

> g++ -static test.cpp -lnetcdf -lhdf5_hl -lhdf5 -lz -lm 
/usr/lib64/libnetcdf.a(liboc_la-ocinternal.o): In function `ocinitialize':
(.text+0x17e): undefined reference to `curl_version_info'
/usr/lib64/libnetcdf.a(liboc_la-http.o): In function `ocsetcurlproperties':
(.text+0xc8): undefined reference to `curl_easy_strerror'
(...)
/usr/lib64/libhdf5.a(H5.o): In function `H5_term_library':
(.text+0x383): undefined reference to `pthread_once'
/usr/lib64/libhdf5.a(H5.o): In function `H5dont_atexit':
(.text+0x11ac): undefined reference to `pthread_once'
(...)
Run Code Online (Sandbox Code Playgroud)

抱怨少了,第一个是来自libcurl?然后hdf5吹牛pthread。因此,我使用package_config尝试找出所有依赖项:

> pkg-config netcdf --libs --static
-lnetcdf
> pkg-config libcurl --libs --static
-lcurl -lidn -llber -lldap -lrt -lgssapi_krb5 -lssl -lcrypto -lz 
Run Code Online (Sandbox Code Playgroud)

最后尝试:

> g++ -pthread -static testing.cpp -lnetcdf -lhdf5_hl -lhdf5 -lz  -lm \
-lcurl -lidn -llber -lldap -lrt -lgssapi_krb5 -lssl -lcrypto -lz
/usr/bin/ld: cannot find -lgssapi_krb5
/usr/lib64/libldap.a(os-ip.o): In function `ldap_pvt_is_socket_ready':
(...)
Run Code Online (Sandbox Code Playgroud)

仍然抱怨找不到gssapi_krb5 ...现在我被卡住了,因为安装libgssapi-krb5-2之后,我唯一得到的就是共享库!

有人知道如何静态链接netcdf程序吗?

比这要简单得多!这是一个包含三行代码的程序,我确实有静态的netcdf库...如果它们已经是静态的,我为什么还需要其他东西?

顺便说一句,我在64位Intel机器上使用Ubuntu Natty 11.04,并且从Ubuntu存储库安装了netcdf和hdf5:

> dpkg --get-selections | grep netcdf
libnetcdf-dev                   install
libnetcdf6                  install
netcdf-bin                  install
netcdf-dbg                  install

> dpkg --get-selections | grep hdf
hdf4-tools                  install
hdf5-tools                  install
hdfview                     install
libhdf4-0                   install
libhdf5-serial-1.8.4                install
libhdf5-serial-dev              install
libjhdf4-java                   install
libjhdf4-jni                    install
libjhdf5-java                   install
libjhdf5-jni                    install
Run Code Online (Sandbox Code Playgroud)