undefined reference when linking pgi-build netcdf lib?

Hi,

I am using PGI 7.0.4 Server version on 64bits CentOS 5.
And try to link a netcdf lib which was compiled with PGI fortran & C.
This compiled netcdf lib always works when I try to link or include.
But I met a weird thing now.

When I compile with the following options

pgf90 -tp k8-32 -O -Mnosecond_underscore -L/opt/netcdf_pgi32/lib -lnetcdf -I/opt/netcdf_pgi32/include create_initial_lagpos.f90

Error messages shows and look like

create_initial_lagpos.f90:(.text+0x3acd): undefined reference to netcdf_nf90_create_' create_initial_lagpos.f90:(.text+0x3c4d): undefined reference to netcdf_nf90_strerror_’
create_initial_lagpos.f90:(.text+0x3d6f): undefined reference to netcdf_nf90_put_att_text_' create_initial_lagpos.f90:(.text+0x3da8): undefined reference to netcdf_nf90_put_att_one_fourbyteint_’
create_initial_lagpos.f90:(.text+0x3de1): undefined reference to netcdf_nf90_put_att_one_fourbytereal_' create_initial_lagpos.f90:(.text+0x3e1a): undefined reference to netcdf_nf90_put_att_one_fourbyteint_’
create_initial_lagpos.f90:(.text+0x3e55): undefined reference to `netcdf_nf90_def_dim_’

Added path of pgi-build netcdf in compile command seems not working.

Any suggestions?

Hi,

My guess is that you didn’t compile netcdf for fortran90, you can compile them manually by going to f90 directory and type make.

Hongyon

Thanks for your reply.
This lib is configured with the following option

env CC=pgcc CFLAGS=“-O -Msignextend -V " FC=pgf90 F90=pgf90 FFLAGS=”-g -w -V "
CXX=pgCC CPPFLAGS="-DNDEBUG -DpgiFortran " ./configure -prefix=/opt/netcdf_pgi32

And netcdf is version 3.6.2.

The f90 already compile and I check the source f90 directory, the f90 already compiled into the lib (.o & .mod already generated and when I type make, it shows Nothing to be done for `all’).

The code I try to compile should be OK.
When I try to produce object only (add -c), no error message show on the screen and create_initial_lagpos.o generated.
But when try to link with pgi-build netcdf lib, the undefined reference errors shows.
So I guess this means one of these (compile object and lib) lacks co-respond subroutine name and caused such errors.

But, how come?
The netcdf lib is called with its standard, like

nf90_create(trim(fname),nf90_64bit_offset,nc_fid)

But I noticed the error message shows

`netcdf_nf90_create_’

Is the compiler add prefix “netcdf_” in object and cause such problem?
I really don’t know.
But when I try to grep libnetcdf.a with string “netcdf_nf90_create_”,
the results say the lib contain this subroutine.
( grep netcdf_nf90_create_ *.a , and shows “Binary file libnetcdf.a matches”)
This result confuses me.
If both lib and .o match with subroutine name, how come the undefined reference errors happen?

Hi,

nf90_create is part of module netcdf, so a compiler add a module name in front of a function name when it generates code.

I believe your program has a statement “use netcdf” at the top of a function/routine to explicit tell a compiler that you intend to use netcdf module, right?

Try putting the library at the end on your link line, I suspect that in 7.0-4 there is a bug in our driver that put the library at the wrong place.

Try this:

pgf90 -tp k8-32 -O -Mnosecond_underscore -I/opt/netcdf_pgi32/include create_initial_lagpos.f90 -L/opt/netcdf_pgi32/lib -lnetcdf

Hongyon

Hi hongyon,

Thanks for your quick reply!

Amazingly I change the sequence of command (as your suggestions), and everything works now!

So the problem is caused by the bug of 7.0-4 of PGI, recently I will upgrade our server to newer version (7.1), hope new version already fix this. :)

Hi,

I am sorry that I gave an incorrect information. Actually it is not a bug. We preserve an order of a library and link command as is because sometimes the order of the library matter.

Hongyon

Hi,

I learn something today.
So you suggesting the command sequence is like

pgf90 option code.f90 lib

I thought the sequence can be arranged in random order before.
Now I will avoid this habit. :)

Anyway, thanks for your help!