Discussion:
fortran module name mangling
Frederik Beaujean
2013-08-17 11:07:42 UTC
Permalink
Hello

I want to call a subroutine in a fortran module in C++ and discovered
the AC_FC_FUNC macro. It seems it does not work with fortran modules. So
the result it produces is useless. For concreteness, I want to call
nestRun from the multinest library defined in fortran as

module Nested
...
contains

subroutine nestRun(...)
...

end subroutine nestRun
end module Nested

On linux with gfortran 4.6 and autoconf 2.68, I try

AC_FC_FUNC(nestRun)
echo "mangled name:" $nestRun

and obtain

mangled name: nestrun_

but the actual name in the library is __nested_MOD_nestrun

Until now, the "recommended" procedure in multinest is to manually check
for either of the gnu or intel compiler and enter the mangled routine
name by hand. But this is hardly portable, as I just tried it out on the
old gfortran 4.2 on a mac, and the mangled name is different from the above.

Browsing the web, I found this document
http://www.netlib.org/lapack/lawnspdf/lawn270.pdf that describes how
cmake also supports module name mangling. If there is a standard way to
achieve this with autoconf, please let me know. Else I would like to
request that such a feature be included in autoconf as well.

Best regards

Fred
Bastien ROUCARIES
2013-08-18 08:16:31 UTC
Permalink
Use cfrotran. Ans report a bug against the debian BTS with a test case.

I am the maintener of it and i welcome any bug report.
Post by Frederik Beaujean
Hello
I want to call a subroutine in a fortran module in C++ and discovered
the AC_FC_FUNC macro. It seems it does not work with fortran modules. So
the result it produces is useless. For concreteness, I want to call
nestRun from the multinest library defined in fortran as
module Nested
...
contains
subroutine nestRun(...)
...
end subroutine nestRun
end module Nested
On linux with gfortran 4.6 and autoconf 2.68, I try
AC_FC_FUNC(nestRun)
echo "mangled name:" $nestRun
and obtain
mangled name: nestrun_
but the actual name in the library is __nested_MOD_nestrun
Until now, the "recommended" procedure in multinest is to manually check
for either of the gnu or intel compiler and enter the mangled routine
name by hand. But this is hardly portable, as I just tried it out on the
old gfortran 4.2 on a mac, and the mangled name is different from the above.
Browsing the web, I found this document
http://www.netlib.org/lapack/lawnspdf/lawn270.pdf that describes how
cmake also supports module name mangling. If there is a standard way to
achieve this with autoconf, please let me know. Else I would like to
request that such a feature be included in autoconf as well.
Best regards
Fred
_______________________________________________
Autoconf mailing list
https://lists.gnu.org/mailman/listinfo/autoconf
Thomas Jahns
2013-08-18 18:49:11 UTC
Permalink
Hello,
Post by Frederik Beaujean
I want to call a subroutine in a fortran module in C++ and discovered
the AC_FC_FUNC macro. It seems it does not work with fortran
modules. So
the result it produces is useless. For concreteness, I want to call
nestRun from the multinest library defined in fortran as
module Nested
...
contains
subroutine nestRun(...)
...
end subroutine nestRun
end module Nested
On linux with gfortran 4.6 and autoconf 2.68, I try
AC_FC_FUNC(nestRun)
echo "mangled name:" $nestRun
and obtain
mangled name: nestrun_
but the actual name in the library is __nested_MOD_nestrun
Until now, the "recommended" procedure in multinest is to manually check
for either of the gnu or intel compiler and enter the mangled routine
name by hand. But this is hardly portable, as I just tried it out on the
old gfortran 4.2 on a mac, and the mangled name is different from the above.
I'd like to collaborate on this but I only have the following
compilers available to test for:

- PGI versions from 9.x on
- Intel versions 10.x and later
- IBM XL Fortran 12.1.0.x and later
- NAG 5.1 and later
- SX but I have no control over the version on the system

(and several versions of gfortran of course), so if one could find
others with Lahey and other Fortran 90 or more modern compilers, that
would be a plus.

Regards, Thomas
--
Thomas Jahns
DKRZ GmbH, Department: Application software

Deutsches Klimarechenzentrum
Bundesstraße 45a
D-20146 Hamburg

Phone: +49-40-460094-151
Fax: +49-40-460094-270
Email: Thomas Jahns <***@dkrz.de>
Bastien ROUCARIES
2013-08-19 10:59:00 UTC
Permalink
Post by Thomas Jahns
Hello,
Post by Frederik Beaujean
I want to call a subroutine in a fortran module in C++ and discovered
the AC_FC_FUNC macro. It seems it does not work with fortran modules. So
the result it produces is useless. For concreteness, I want to call
nestRun from the multinest library defined in fortran as
module Nested
...
contains
subroutine nestRun(...)
...
end subroutine nestRun
end module Nested
On linux with gfortran 4.6 and autoconf 2.68, I try
AC_FC_FUNC(nestRun)
echo "mangled name:" $nestRun
and obtain
mangled name: nestrun_
but the actual name in the library is __nested_MOD_nestrun
Until now, the "recommended" procedure in multinest is to manually check
for either of the gnu or intel compiler and enter the mangled routine
name by hand. But this is hardly portable, as I just tried it out on the
old gfortran 4.2 on a mac, and the mangled name is different from the above.
- PGI versions from 9.x on
- Intel versions 10.x and later
- IBM XL Fortran 12.1.0.x and later
- NAG 5.1 and later
- SX but I have no control over the version on the system
(and several versions of gfortran of course), so if one could find others with Lahey and other Fortran 90 or more modern compilers, that would be a plus.
Regards, Thomas
Hi did you try cfortran ?

It is a header only file what allow to link fortran with C and vice versa.

I use debian package and i maintain upstream.

http://packages.qa.debian.org/c/cfortran.html

Please report bug here
http://bugs.debian.org/cgi-bin/pkgreport.cgi?repeatmerged=no&src=cfortran

And download from here

http://bugs.debian.org/cgi-bin/pkgreport.cgi?repeatmerged=no&src=cfortran

I plan to modernize it, so please test it and report any error.

Please test plain old fortran call first not function that are part of module.

Bastien
Post by Thomas Jahns
--
Thomas Jahns
DKRZ GmbH, Department: Application software
Deutsches Klimarechenzentrum
Bundesstraße 45a
D-20146 Hamburg
Phone: +49-40-460094-151
Fax: +49-40-460094-270
_______________________________________________
Autoconf mailing list
https://lists.gnu.org/mailman/listinfo/autoconf
Frederik Beaujean
2013-08-19 12:28:00 UTC
Permalink
Post by Bastien ROUCARIES
Hi did you try cfortran ?
It is a header only file what allow to link fortran with C and vice versa.
I use debian package and i maintain upstream.
http://packages.qa.debian.org/c/cfortran.html
Please report bug here
http://bugs.debian.org/cgi-bin/pkgreport.cgi?repeatmerged=no&src=cfortran
And download from here
http://bugs.debian.org/cgi-bin/pkgreport.cgi?repeatmerged=no&src=cfortran
I plan to modernize it, so please test it and report any error.
Please test plain old fortran call first not function that are part of module.
Yes, I looked into cfortran. Took me a while to find my way through the
docs, but it seems there is no mention of modules at all. The Fortran
code I want to call is not under my control, and I don't want to patch
it. Else I would add BIND(C) calls where necessary. For the same reason,
I can't move the subroutine out of its module either.

With cfortran, I end up with this linker error:

... undefined reference to `NESTRUN_'

Since cfortran doesn't deal with modules, that is the wrong name, should
be __nested_MOD_nestrun. Apart from that, cfortran.h did what it
promised to.
@Bastien: If I understand correctly, this is not a bug of cfortran, it
just doesn't support modules, right? Is module support part of what you
want to add when modernizing cfortran.h?

bye

Fred
Bastien ROUCARIES
2013-08-19 12:33:49 UTC
Permalink
On Mon, Aug 19, 2013 at 2:28 PM, Frederik Beaujean
Post by Frederik Beaujean
Post by Bastien ROUCARIES
Hi did you try cfortran ?
It is a header only file what allow to link fortran with C and vice versa.
I use debian package and i maintain upstream.
http://packages.qa.debian.org/c/cfortran.html
Please report bug here
http://bugs.debian.org/cgi-bin/pkgreport.cgi?repeatmerged=no&src=cfortran
And download from here
http://bugs.debian.org/cgi-bin/pkgreport.cgi?repeatmerged=no&src=cfortran
I plan to modernize it, so please test it and report any error.
Please test plain old fortran call first not function that are part of module.
Yes, I looked into cfortran. Took me a while to find my way through the
docs, but it seems there is no mention of modules at all. The Fortran
code I want to call is not under my control, and I don't want to patch
it. Else I would add BIND(C) calls where necessary. For the same reason,
I can't move the subroutine out of its module either.
... undefined reference to `NESTRUN_'
Since cfortran doesn't deal with modules, that is the wrong name, should
be __nested_MOD_nestrun. Apart from that, cfortran.h did what it
promised to.
@Bastien: If I understand correctly, this is not a bug of cfortran, it
just doesn't support modules, right? Is module support part of what you
want to add when modernizing cfortran.h?
Yes module is part of modernizing cfortran.h.
Post by Frederik Beaujean
bye
Fred
Thomas Jahns
2013-08-19 14:28:14 UTC
Permalink
Hello,
Post by Bastien ROUCARIES
Hi did you try cfortran ?
I'm a quite well versed user of cfortran.h. But with respect to calling Fortran
from C it's only useful so far for external procedures, not module procedures.
That's something I'd like to improve: generating module procedure names in
addition to external procedure names.
Post by Bastien ROUCARIES
I plan to modernize it, so please test it and report any error.
In that case: you got mail.

Regards, Thomas
--
Thomas Jahns
DKRZ GmbH, Department: Application software

Deutsches Klimarechenzentrum
Bundesstraße 45a
D-20146 Hamburg

Phone: +49-40-460094-151
Fax: +49-40-460094-270
Email: Thomas Jahns <***@dkrz.de>
Frederik Beaujean
2013-08-19 14:41:00 UTC
Permalink
Post by Thomas Jahns
I'm a quite well versed user of cfortran.h. But with respect to calling Fortran
from C it's only useful so far for external procedures, not module procedures.
That's something I'd like to improve: generating module procedure names in
addition to external procedure names.
Post by Bastien ROUCARIES
I plan to modernize it, so please test it and report any error.
Unfortunately I have never written a line of fortran code in my life, so
I guess I wouldn't be of much help. But if the two of you, Bastien and
Thomas, want to improve cfortran.h to support modules, I would really
appreciate it and would offer help testing it as well.

bye

Fred

Rhys Ulerich
2013-08-19 14:36:54 UTC
Permalink
Post by Frederik Beaujean
I want to call a subroutine in a fortran module in C++ and discovered
the AC_FC_FUNC macro. It seems it does not work with fortran modules.
Can you use the newer ISO_C_BINDING intrinsic module to write a
C-compatible wrapper employing BIND(C), write your C++ to that API,
and be done?

- Rhys
Loading...