pgf77 + g77 objects = broken iargc()

I have a g77-compiled library file from a third party and am trying to write Fortran code that calls functions in this library. While pgf77 compiles and links the code, the executable has a broken iargc() function (iargc returns the number of command line arguments). Here’s code to demonstrate the problem:

File test.f simulates my driver program, to be compiled w/pgf77:

      program test
      integer iargc
      write(6,*) 'iargc=',iargc()
      call abc()
      end

File abc.f will be compiled with g77 to produce abc.o, simulating my third-party library:

      subroutine abc()
      write(6,*) 'inside abc'
      return
      end

Compile and link with

 g77 -c abc.f
 pgf77 test.f abc.o -lg2c

The -lg2c library is needed to resolve the GNU functions s_wsle, do_lio, and e_wsle which appear in abc.o. Running the code produces incorrect output from iargc():

 ./a.out 1 2 3
 iargc=          -1
 inside abc

Using pgf77 to compile and link both files produces correct results:

pgf77 test.f abc.f
./a.out 1 2 3
 iargc=           3
 inside abc

This isn’t helpful for me though since I need to link with a g77-built library.

Anyone know of a fix or work-around for this problem?

My system:
Linux 2.6.9-34.ELsmp #1 SMP Wed Mar 8 00:27:03 CST 2006 i686 i686 i386 GNU/Linux

pgf77 -V
pgf77 6.2-5 32-bit target on x86 Linux
Copyright 1989-2000, The Portland Group, Inc. All Rights Reserved.
Copyright 2000-2006, STMicroelectronics, Inc. All Rights Reserved.

g77 --version
GNU Fortran (GCC) 3.4.5 20051201 (Red Hat 3.4.5-2)
Copyright © 2004 Free Software Foundation, Inc.

Hi alnd,

Instead of “-lg2c” use the PGI flag “-g77libs”. This flag puts the g2c lib in the correct order on the link line.

  • Mat

Mat: your tip worked beautifully, thanks! – Al