我试图使用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);
}
如果我像这样编译它,那就完美了:
> g++ test.cpp -lnetcdf
但是,如果我尝试静态编译它,那么一切都会破坏:
> 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'
(...)
很多抱怨,但第一个是hdf5。在寻找答案时,我发现此页面at 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'
(...)
投诉少,第一批来自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
最后尝试了:
> 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':
(...)
仍然抱怨没有找到gssapi_krb5 ...现在我被困了,因为在安装了libgssapi-krb5-2之后我唯一得到的就是共享库!!
有没有人知道如何静态链接netcdf程序?
它必须比这更简单!这是一个包含3行代码的程序,我有静态的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