can't read arguments using getarg()


I am trying to compile some Fortran code. The compiling options I used are:

FFLAGS= -c -byteswapio -DLinux -O
LDFLAGS = -lg2c

In the Fortran code, there are some places call iargc() and getarg(). But the program seems couldn’t read the arguments from the command line such as:

*.exe input.file1 input.file2

When I tried to print out the value of iargc(), it gave me –1.

Then I tried to write some Fortran code very simple which also calls those functions. And it worked fine until I added -lg2c when compiling. So I guess it’s –lg2c gave me trouble, and I tried to delete -lg2c in my Makefile. However it ended up with giving me lots of undefined reference error to some Gempak library which was compiled by gcc and g77.

Any suggestions on how to fix it?


Hi nov,

The easy solution is to use the PGI flag “-g77libs” instead of “-lg2c”. “-g77libs” includes either -lg2c or -lf2c depending upon your system, but does so after the PGI runtime libraries. This causes the linker to use the PGI instead of the GNU versions of these routines.

  • Mat

It works! Thanks, Mat!

But I still don’t quite understand why –lg2c didn’t work?

Also I am wondering if there is a good source of general information about compiling and linking? Every time when I try to compile codes from other people, I get puzzled quite easily.


Hi nov,

When different libraries contain the same symbol name, the linker only uses the first symbol it finds. When you use “-lg2c”, the g2c library is put in front of the PGI fortran runtime library, and the GNU version of these functions are used. When using “-g77libs”, the g2c library is added after the PGI runtime and the PGI version is used.

Off-hand I don’t know of a good source for general information. Maybe someone else does? The PGI user’s guide will give you a good understanding of the various optimization and flags, but doesn’t sound like what your looking for.

  • Mat

Hi Mat,

This is much clear to me now. Thanks!