[MITgcm-support] G95 build issue: f2c name mangling

Ed Hill ed at eh3.com
Mon Oct 9 00:00:49 EDT 2006

On Mon, 09 Oct 2006 14:38:23 +1300 Mark Hadfield
<m.hadfield at niwa.co.nz> wrote:

> Anyway, I've run into a problem with Fortran-C linking. I understand
> the problem, up to a point, and I can think of various ways to work
> around it. But there's an aspect that's really bugging me.

Hi Mark,

I think there is a much easier way to deal with this whole situation and
its the genmake2 "-ignoretime" flag which has been discussed in previous


Or, you can accomplish the same thing by commenting out the offending
function calls.

> There are various possible solutions to this problem. Just adding a 
> second underscore to the FC_NAMEMANGLE macro doesn't work, as it
> makes timenow invisible to the linker. (As far as I can tell, it is
> not possible to make a cpp macro that will emulate the weird f2c
> behaviour.) G95 supports an "-fno-second-underscore" option, which
> would solve the problem nicely, but I then need to rebuild the netCDF
> library and my initial attempts to do so did not work. The easiest
> workaround is to define a special preprocessor symbol
> cygwin_ia32_g95:
> and some special-case code:
> timer_stats.c:
> #if defined G95
>   double user_time__ (void)
> #else
>   double FC_NAMEMANGLE(user_time) (void)
> #endif
> That's what I've done, but it's not very pretty, is it?

I agree.  Its ugly.  And its a lot of annoyance for a few function
calls that, ultimately, have absolutely no bearing on the actual model

The easiest (and IMO smartest) thing to do here is is to eschew names
that contain underscores.  And make it easy to turn off the whole thing
when its not needed/wanted.

> OK, but the question that's really bugging me is this. G77 also
> follows the f2c name-mangling convention; the documentation says so
> and I have confirmed this with some toy examples. For example:
> $ cat main.f
>       program main
>       call c_sub()
>       end
> $ cat c_sub.c
> void c_sub_() {}
> $ gcc -c c_sub.c
> $ nm c_sub.o
> 00000000 b .bss
> 00000000 d .data
> 00000000 t .text
> 00000000 T _c_sub_
> g77 main.f c_sub.o
> /tmp/cc0iOz7I.o:main.f:(.text+0x7): undefined reference to `_c_sub__'
> collect2: ld returned 1 exit status
> So how does the G77 linker recognise the one-underscore names, 
> "user_time_" and "system_time_", in timer_stats.o when it doesn't 
> recognise "c_sub_" in the toy case above?

Yes, Fortran/C name-mangling is annoying.  And you are absolutely right
about the rule that g77 follows which is "one added trailing underscore
unless the name already contains an underscore in which case its *two*
added trailing underscores."  When I wrote "tim.c" and the related
Fortran bits I was careful with the names because I was aware of that

Unfortunately, the folks who wrote "timer_stats.F" were not so


Edward H. Hill III, PhD  |  ed at eh3.com  |  http://eh3.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mitgcm.org/pipermail/mitgcm-support/attachments/20061009/e1370cea/attachment.sig>

More information about the MITgcm-support mailing list