Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
10 Views

Fortran compiler with option -static and NetCDF library

Hello,

I have a difficulty to compile the Fortran code with option -static when the NetCDF library is required.  I explain the issue in detail as following.

We have two Red Hat Enterprise Linux 7.4 64-bit system machines.  Due to security consideration, one of the machines (called machine 1 later) is only installed OS.  The other machine (called machine 2) is installed Intel compiler and NetCDF library.  Now we want to compile our Fortran code on machine 2 to generate an executable file and run this executable file on machine 1.  In order to run it on machine 1, we need to use option -static on machine 2 to compile the Fortran code.

First I compiled a simple Fortran code (without using NetCDF library) with and without option -static on machine 2.  The generated executable files can be run on machine 1 to get the same results.  Next I can successfully compile a Fortran with using NetCDF library without option -static on machine 2.  The generated executable can be run on machine 2, but cannot be run on machine 1 because this executable file loads the Intel and NetCDF libraries which are not installed on machine 1.  When I compile the Fortran code with option -static on machine 2, the compilation fails and the error message are such as:

/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_put_att_text_':
fort-attio.c:(.text+0x70): undefined reference to `nc_put_att_text'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_get_att_text_':
fort-attio.c:(.text+0x175): undefined reference to `nc_get_att_text'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_put_att_int1_':
fort-attio.c:(.text+0x287): undefined reference to `nc_put_att_schar'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_get_att_int1_':
fort-attio.c:(.text+0x395): undefined reference to `nc_get_att_schar'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_put_att_int2_':
fort-attio.c:(.text+0x4a7): undefined reference to `nc_put_att_short'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_get_att_int2_':   and more

From these error messages, it is obviously that the NetCDF library is not imported when the option -static is used (note: if the option -static is not requested, the compilation goes through successfully).  

Have anyone in this forum come across the same issue and if yes, how to solve it?  Your comments are greatly appreciated.

Thanks,

Lianyuan

 

0 Kudos
10 Replies
Highlighted
Black Belt
10 Views

Please state the linking

Please state the linking command line that produced the "undefined reference" messages. I suspect that when you linked with the static library you specified only the Fortran library libnetcdff.a and left out the C library libnetcdf.a.

0 Kudos
Highlighted
10 Views

Hello, mecej4,

Hello, mecej4,

Thank you for your reply.  So far I have used the following three compilation commands to compile to frtran code called convert2.f (the NetCDF package is installed at /usr/local/):

1)  ifort -o convert2 convert2.f -I/usr/local/include -L/usr/local/lib -lnetcdff

The compilation goes through and the executable file "convert2" is successfully generated.  On machine 2, we can run "convert2" to get the expected results.  However, on machine 1, we can not successfully run "convert2" because running "convert2" requires to load the Intel and NetCDF libraries that are not allowed to install on machine 1.

2)  ifort -static -o convert2 convert2.f -I/usr/local/include -L/usr/local/lib -lnetcdff

The compilation fails and the following error messages are shown:

/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_put_att_text_':
fort-attio.c:(.text+0x70): undefined reference to `nc_put_att_text'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_get_att_text_':
fort-attio.c:(.text+0x175): undefined reference to `nc_get_att_text'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_put_att_int1_':
fort-attio.c:(.text+0x287): undefined reference to `nc_put_att_schar'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_get_att_int1_':
fort-attio.c:(.text+0x395): undefined reference to `nc_get_att_schar'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_put_att_int2_':
fort-attio.c:(.text+0x4a7): undefined reference to `nc_put_att_short'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_get_att_int2_':
fort-attio.c:(.text+0x5b5): undefined reference to `nc_get_att_short'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_put_att_int_':
fort-attio.c:(.text+0x6c7): undefined reference to `nc_put_att_int'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_get_att_int_':
fort-attio.c:(.text+0x7d5): undefined reference to `nc_get_att_int'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_put_att_real_':
fort-attio.c:(.text+0x8e7): undefined reference to `nc_put_att_float'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_get_att_real_':
fort-attio.c:(.text+0x9f5): undefined reference to `nc_get_att_float'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_put_att_double_':
fort-attio.c:(.text+0xb07): undefined reference to `nc_put_att_double'
/usr/local/lib/libnetcdff.a(fort-attio.o): In function `nf_get_att_double_':
fort-attio.c:(.text+0xc15): undefined reference to `nc_get_att_double'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_create_':
fort-control.c:(.text+0x5c): undefined reference to `nc_create'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf__create_':
fort-control.c:(.text+0x17d): undefined reference to `nc__create'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_open_':
fort-control.c:(.text+0x28c): undefined reference to `nc_open'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf__open_':
fort-control.c:(.text+0x39e): undefined reference to `nc__open'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_set_fill_':
fort-control.c:(.text+0x461): undefined reference to `nc_set_fill'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_redef_':
fort-control.c:(.text+0x473): undefined reference to `nc_redef'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_enddef_':
fort-control.c:(.text+0x483): undefined reference to `nc_enddef'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf__enddef_':
fort-control.c:(.text+0x49f): undefined reference to `nc__enddef'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_sync_':
fort-control.c:(.text+0x4b3): undefined reference to `nc_sync'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_close_':
fort-control.c:(.text+0x4c3): undefined reference to `nc_close'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_delete_':
fort-control.c:(.text+0x50a): undefined reference to `nc_delete'
fort-control.c:(.text+0x52a): undefined reference to `nc_delete'
fort-control.c:(.text+0x591): undefined reference to `nc_delete'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf__create_mp_':
fort-control.c:(.text+0x651): undefined reference to `nc__create_mp'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf__open_mp_':
fort-control.c:(.text+0x77d): undefined reference to `nc__open_mp'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_delete_mp_':
fort-control.c:(.text+0x870): undefined reference to `nc_delete_mp'
fort-control.c:(.text+0x897): undefined reference to `nc_delete_mp'
fort-control.c:(.text+0x901): undefined reference to `nc_delete_mp'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_set_base_pe_':
fort-control.c:(.text+0x945): undefined reference to `nc_set_base_pe'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_inq_base_pe_':
fort-control.c:(.text+0x95f): undefined reference to `nc_inq_base_pe'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_abort_':
fort-control.c:(.text+0x973): undefined reference to `nc_abort'
/usr/local/lib/libnetcdff.a(fort-control.o): In function `nf_set_default_format_':
fort-control.c:(.text+0x98f): undefined reference to `nc_set_default_format'
/usr/local/lib/libnetcdff.a(fort-dim.o): In function `nf_def_dim_':
fort-dim.c:(.text+0x6f): undefined reference to `nc_def_dim'
/usr/local/lib/libnetcdff.a(fort-dim.o): In function `nf_inq_dimid_':
fort-dim.c:(.text+0x193): undefined reference to `nc_inq_dimid'
/usr/local/lib/libnetcdff.a(fort-dim.o): In function `nf_inq_dim_':
fort-dim.c:(.text+0x2d3): undefined reference to `nc_inq_dim'
/usr/local/lib/libnetcdff.a(fort-dim.o): In function `nf_inq_dimname_':
fort-dim.c:(.text+0x427): undefined reference to `nc_inq_dimname'
/usr/local/lib/libnetcdff.a(fort-dim.o): In function `nf_inq_dimlen_':
fort-dim.c:(.text+0x514): undefined reference to `nc_inq_dimlen'
/usr/local/lib/libnetcdff.a(fort-dim.o): In function `nf_rename_dim_':
fort-dim.c:(.text+0x58d): undefined reference to `nc_rename_dim'
/usr/local/lib/libnetcdff.a(fort-genatt.o): In function `nf_inq_att_':
fort-genatt.c:(.text+0x79): undefined reference to `nc_inq_att'
/usr/local/lib/libnetcdff.a(fort-genatt.o): In function `nf_inq_attid_':
fort-genatt.c:(.text+0x195): undefined reference to `nc_inq_attid'
/usr/local/lib/libnetcdff.a(fort-genatt.o): In function `nf_inq_atttype_':
fort-genatt.c:(.text+0x2a5): undefined reference to `nc_inq_atttype'
/usr/local/lib/libnetcdff.a(fort-genatt.o): In function `nf_inq_attlen_':
fort-genatt.c:(.text+0x3b5): undefined reference to `nc_inq_attlen'
/usr/local/lib/libnetcdff.a(fort-genatt.o): In function `nf_inq_attname_':
fort-genatt.c:(.text+0x4e6): undefined reference to `nc_inq_attname'
/usr/local/lib/libnetcdff.a(fort-genatt.o): In function `nf_copy_att_':
fort-genatt.c:(.text+0x639): undefined reference to `nc_copy_att'
/usr/local/lib/libnetcdff.a(fort-genatt.o): In function `nf_rename_att_':
fort-genatt.c:(.text+0x79e): undefined reference to `nc_rename_att'
/usr/local/lib/libnetcdff.a(fort-genatt.o): In function `nf_del_att_':
fort-genatt.c:(.text+0x92d): undefined reference to `nc_del_att'
/usr/local/lib/libnetcdff.a(fort-geninq.o): In function `nf_inq_':
fort-geninq.c:(.text+0x34): undefined reference to `nc_inq'
/usr/local/lib/libnetcdff.a(fort-geninq.o): In function `nf_inq_ndims_':
fort-geninq.c:(.text+0x8f): undefined reference to `nc_inq_ndims'
/usr/local/lib/libnetcdff.a(fort-geninq.o): In function `nf_inq_nvars_':
fort-geninq.c:(.text+0xaf): undefined reference to `nc_inq_nvars'
/usr/local/lib/libnetcdff.a(fort-geninq.o): In function `nf_inq_natts_':
fort-geninq.c:(.text+0xcf): undefined reference to `nc_inq_natts'
/usr/local/lib/libnetcdff.a(fort-geninq.o): In function `nf_inq_unlimdim_':
fort-geninq.c:(.text+0xf6): undefined reference to `nc_inq_unlimdim'
/usr/local/lib/libnetcdff.a(fort-geninq.o): In function `nf_inq_format_':
fort-geninq.c:(.text+0x12f): undefined reference to `nc_inq_format'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_def_var_':
fort-genvar.c:(.text+0x97): undefined reference to `nc_def_var'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_inq_var_':
fort-genvar.c:(.text+0x20c): undefined reference to `nc_inq_var'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_inq_varid_':
fort-genvar.c:(.text+0x39c): undefined reference to `nc_inq_varid'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_inq_varname_':
fort-genvar.c:(.text+0x4b7): undefined reference to `nc_inq_varname'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_inq_vartype_':
fort-genvar.c:(.text+0x5a4): undefined reference to `nc_inq_vartype'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_inq_varndims_':
fort-genvar.c:(.text+0x5d4): undefined reference to `nc_inq_varndims'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_inq_vardimid_':
fort-genvar.c:(.text+0x614): undefined reference to `nc_inq_vardimid'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_inq_varnatts_':
fort-genvar.c:(.text+0x654): undefined reference to `nc_inq_varnatts'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_rename_var_':
fort-genvar.c:(.text+0x6cd): undefined reference to `nc_rename_var'
/usr/local/lib/libnetcdff.a(fort-genvar.o): In function `nf_copy_var_':
fort-genvar.c:(.text+0x779): undefined reference to `nc_copy_var'
/usr/local/lib/libnetcdff.a(fort-lib.o): In function `c2f_dimids':
fort-lib.c:(.text+0xf): undefined reference to `nc_inq_varndims'
/usr/local/lib/libnetcdff.a(fort-lib.o): In function `c2f_chunksiz
fort-lib.c:(.text+0x12f): undefined reference to `nc_inq_varndims'
/usr/local/lib/libnetcdff.a(fort-lib.o): In function `f2c_chunksizes':
fort-lib.c:(.text+0x1bf): undefined reference to `nc_inq_varndims'
/usr/local/lib/libnetcdff.a(fort-lib.o): In function `f2c_coords':
fort-lib.c:(.text+0x25f): undefined reference to `nc_inq_varndims'
/usr/local/lib/libnetcdff.a(fort-lib.o): In function `f2c_counts':
fort-lib.c:(.text+0x44e): undefined reference to `nc_inq_varndims'
/usr/local/lib/libnetcdff.a(fort-lib.o):fort-lib.c:(.text+0x61e): more undefined references to `nc_inq_varndims' follow
/usr/local/lib/libnetcdff.a(fort-lib.o): In function `nc_inq_varids_f':
fort-lib.c:(.text+0x9c8): undefined reference to `nc_inq_varids'
fort-lib.c:(.text+0x9f9): undefined reference to `nc_inq_varids'
 

3)  ifort -static -o convert2 convert2.f -I/usr/local/include -L/usr/local/lib -LNETCDF -lnetcdff

The same errors as second compilation are shown.

We installed NetCDF package with its default options.  I am not sure if this error is related to NetCDF installation.  If we use compiling option -static to compile the fortran code, does it require to have -static option to install the NetCDF package?

Thanks,

Lianyuan

0 Kudos
Highlighted
Black Belt
10 Views

At the end of the build

At the end of the build commands 2) and 3), you should add -lnetcdf in order to link the basic NetCDF C library, as I stated in #2. When you use the shared NETCDFF library, this is not needed because, during the process of building libnetcdff.so, the linker would have already bound in the necessary routines from libnetcdf.so .

0 Kudos
Highlighted
10 Views

Hi, 

Hi, 

Sorry I have a typo by missing -lnetcdf in command 3).  The command 3) is

ifort -o convert2 convert2.f -I/use/local/include -L/use/local/lib -lnetcdf -lnetcdff

The error messages are the same as command 2).

Thanks,

Lianyuan

0 Kudos
Highlighted
Black Belt
10 Views

You specified the Netcdf

You specified the Netcdf libraries in the wrong order in #5.

You should have -lnetcdff first, then -lnetcdf, as I stated in #4.

See http://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_18.html .

0 Kudos
Highlighted
10 Views

Hi, Mecej4,

Hi, Mecej4,

Thank you very much for pointing out the linking libraries order.  Base on your suggestion, I run the command:

ifort -static convert2.f -o convert2 -I/usr/local/include -L/usr/local/lib -lnetcdff -lnetcdf

The attached file shows the compilation errors that related to curl, hdf5, etc.  Please advice!

Thanks,

Lianyuan

0 Kudos
Highlighted
Black Belt
10 Views

From the list of unsatisfied

From the list of unsatisfied externals in "compile.txt", I see that you may need to link with a few more libraries (libcurl.a, libhdf5.a, and other inherited dependencies). I cannot help you with those.

It is also probable that at some point you will find that trying to build an executable that does not depend on any shared libraries is no longer feasible in modern versions of Linux. Many years ago, I had a bad experience doing something similar in Linux. Some package required something from a specific version of libc.so.6. Ignorantly, I downloaded and installed that version. Very soon, the system became completely unusable, because all the system commands such as ls, etc., all depended on libc.so.6, and the newly installed version was not compatible with them. I ended up reinstalling/refreshing the whole OS.

0 Kudos
Highlighted
10 Views

Hi, Mecej4,

Hi, Mecej4,

On our machine, I can find libcurl.so and libhdf5.so, but I can not find their library files: libcurl.a and libhdf5.a.  I don't know if these libraries are not installed on machine, how the NetCDF package can be correctly installed.  I will ask our IT to install these libraries on machine.

Thanks,

Lianyuan

0 Kudos
Highlighted
Black Belt
10 Views

I just remembered one more

I just remembered one more dependency that you may have to consider, based on building NetCDF several years ago: you may need a static library for SZIP on your Machine-2, since HDF may depend on SZIP (please check whether the current version also has this dependency).

0 Kudos
Highlighted
10 Views

Ok, Thank you very much,

Ok, Thank you very much, mecej4.  Your comments are very helpful.

Lianyuan

0 Kudos