public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Accessing C++ function with Mixed Upper/Lower case  name from F90  subroutine
@ 2010-03-30 17:50 Glenn H Sembroski
  2010-03-30 23:58 ` Harald Servat
  2010-03-31 11:29 ` Axel Freyn
  0 siblings, 2 replies; 5+ messages in thread
From: Glenn H Sembroski @ 2010-03-30 17:50 UTC (permalink / raw)
  To: gcc-help

Hi,
   I have a problem trying to prepare a large simulation package, using 
mixed F90 and C++ routines, to compile and build under GCC (g++, and 
gfortran).  The problem is I have a C++ function: Gauss() which is 
called from an F90 routine:
   x=gauss()

This package was made able to  build under the Intel fortran compiler by 
adding to the fortran file the Intel compile directive:

!DEC$ ATTRIBUTES ALIAS:'Gauss' :: gauss

I attempted to build this program using GCC  where for F90 code the 
gfortran compiler option -fno-underscoring was used.

Presently my fortran routine won't build. It gets the error:

485: undefined reference to `gauss'

So, my question is, is there a way in GCC to enable the linker to find 
the C++  'Gauss' method?

Thanks,
 Dr. Glenn Sembroski
Physics Dept.,Purdue University
sembrosk@purdue.edu

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Accessing C++ function with Mixed Upper/Lower case  name from  F90  subroutine
  2010-03-30 17:50 Accessing C++ function with Mixed Upper/Lower case name from F90 subroutine Glenn H Sembroski
@ 2010-03-30 23:58 ` Harald Servat
  2010-03-31 11:33   ` Toon Moene
  2010-03-31 11:29 ` Axel Freyn
  1 sibling, 1 reply; 5+ messages in thread
From: Harald Servat @ 2010-03-30 23:58 UTC (permalink / raw)
  To: Glenn H Sembroski; +Cc: gcc-help

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Hello Glenn,

En/na Glenn H Sembroski ha escrit:
> Hi,
>   I have a problem trying to prepare a large simulation package, using
> mixed F90 and C++ routines, to compile and build under GCC (g++, and
> gfortran).  The problem is I have a C++ function: Gauss() which is
> called from an F90 routine:
>   x=gauss()
> 
> This package was made able to  build under the Intel fortran compiler by
> adding to the fortran file the Intel compile directive:
> 
> !DEC$ ATTRIBUTES ALIAS:'Gauss' :: gauss
> 
> I attempted to build this program using GCC  where for F90 code the
> gfortran compiler option -fno-underscoring was used.
> 
> Presently my fortran routine won't build. It gets the error:
> 
> 485: undefined reference to `gauss'
> 
> So, my question is, is there a way in GCC to enable the linker to find
> the C++  'Gauss' method?

  I don't know if the linker supports for this directly. But you can try
with the function attribute 'alias("target")' which is documented in
[1]. It creates a new name for the same routine, so you can call it
through the two names (the original, and the aliased).
  In your case, you should define in your C++ file an alias to "Gauss"
which is called "gauss".

  Besides, IIRC, ICC also supports this function attribute, so you
should be able to apply it transparently.

[1] http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

Regards.

> Thanks,
> Dr. Glenn Sembroski
> Physics Dept.,Purdue University
> sembrosk@purdue.edu
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEARECAAYFAkuyJs4ACgkQwMPeuqUCg9wTcQCffv1P6zfesFqNNB5IAfQc8JV8
1mAAnjz0pzkWaOSYUsox1t0+rqUqqdah
=c9fe
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Accessing C++ function with Mixed Upper/Lower case  name from  F90  subroutine
  2010-03-30 17:50 Accessing C++ function with Mixed Upper/Lower case name from F90 subroutine Glenn H Sembroski
  2010-03-30 23:58 ` Harald Servat
@ 2010-03-31 11:29 ` Axel Freyn
  1 sibling, 0 replies; 5+ messages in thread
From: Axel Freyn @ 2010-03-31 11:29 UTC (permalink / raw)
  To: gcc-help

Hi Glenn,
On Tue, Mar 30, 2010 at 11:15:27AM -0400, Glenn H Sembroski wrote:
>   I have a problem trying to prepare a large simulation package, using  
> mixed F90 and C++ routines, to compile and build under GCC (g++, and  
> gfortran).  The problem is I have a C++ function: Gauss() which is  
> called from an F90 routine:
>   x=gauss()
>
> This package was made able to  build under the Intel fortran compiler by  
> adding to the fortran file the Intel compile directive:
>
> !DEC$ ATTRIBUTES ALIAS:'Gauss' :: gauss
>
> I attempted to build this program using GCC  where for F90 code the  
> gfortran compiler option -fno-underscoring was used.
>
> Presently my fortran routine won't build. It gets the error:
>
> 485: undefined reference to `gauss'
>
> So, my question is, is there a way in GCC to enable the linker to find  
> the C++  'Gauss' method?

Well, just a possibility: C++-functions are something special (due to
the possibility that they can be overloaded e.g.), you can't call them
directly from C or F77 - and I think F90 might have the same problem: A
C++-Function "Gauss" will NOT be called "gauss" for linking - but
instead it will have a much longer name depending on its parameters...

You have to mark the C++-function as e.g. extern "C" void Gauss(); in
order to be able to call it from C or F77 (and probably also for calls
from F90?)

Axel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Accessing C++ function with Mixed Upper/Lower case  name from   F90  subroutine
  2010-03-30 23:58 ` Harald Servat
@ 2010-03-31 11:33   ` Toon Moene
  2010-03-31 12:51     ` Tobias Burnus
  0 siblings, 1 reply; 5+ messages in thread
From: Toon Moene @ 2010-03-31 11:33 UTC (permalink / raw)
  To: Harald Servat; +Cc: Glenn H Sembroski, gcc-help, gfortran

[ Copied to the GNU Fortran mailing list - probably a better forum for
   this question ]

Harald Servat wrote:

> Hello Glenn,

>>   I have a problem trying to prepare a large simulation package, using
>> mixed F90 and C++ routines, to compile and build under GCC (g++, and
>> gfortran).  The problem is I have a C++ function: Gauss() which is
>> called from an F90 routine:
>>   x=gauss()
>>
>> This package was made able to  build under the Intel fortran compiler by
>> adding to the fortran file the Intel compile directive:
>>
>> !DEC$ ATTRIBUTES ALIAS:'Gauss' :: gauss
>>
>> I attempted to build this program using GCC  where for F90 code the
>> gfortran compiler option -fno-underscoring was used.
>>
>> Presently my fortran routine won't build. It gets the error:
>>
>> 485: undefined reference to `gauss'
>>
>> So, my question is, is there a way in GCC to enable the linker to find
>> the C++  'Gauss' method?
> 
>   I don't know if the linker supports for this directly. But you can try
> with the function attribute 'alias("target")' which is documented in
> [1]. It creates a new name for the same routine, so you can call it
> through the two names (the original, and the aliased).
>   In your case, you should define in your C++ file an alias to "Gauss"
> which is called "gauss".
> 
>   Besides, IIRC, ICC also supports this function attribute, so you
> should be able to apply it transparently.
> 
> [1] http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

The Standard (as of Fortran 2003, supported by GNU Fortran and ifort) 
way to do this is to add the following to your Fortran code:

$ cat a.f90
interface gauss
function gauss() bind(c, name="Gauss")
use, intrinsic :: iso_c_binding
real(c_float) :: gauss
end function gauss
end interface gauss
x = gauss()
end
$ gfortran -c a.f90
# The command 'nm' will show you that the right external name for the
# "gauss" routine has been encoded in the object file:
$ nm a.o
                  U Gauss
0000000000000000 T MAIN__
                  U _gfortran_set_options
0000000000000000 r options.0.1534

Hope this helps,

-- 
Toon Moene - e-mail: toon@moene.org - phone: +31 346 214290
Saturnushof 14, 3738 XG  Maartensdijk, The Netherlands
At home: http://moene.org/~toon/
Progress of GNU Fortran: http://gcc.gnu.org/gcc-4.5/changes.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Accessing C++ function with Mixed Upper/Lower case  name from    F90  subroutine
  2010-03-31 11:33   ` Toon Moene
@ 2010-03-31 12:51     ` Tobias Burnus
  0 siblings, 0 replies; 5+ messages in thread
From: Tobias Burnus @ 2010-03-31 12:51 UTC (permalink / raw)
  To: Toon Moene; +Cc: Harald Servat, Glenn H Sembroski, gcc-help, gfortran

Hi all,

Toon Moene wrote:
> The Standard (as of Fortran 2003, supported by GNU Fortran and ifort)
> way to do this is to add the following to your Fortran code:
>
> $ cat a.f90
> interface gauss
> function gauss() bind(c, name="Gauss")
> use, intrinsic :: iso_c_binding
> real(c_float) :: gauss

See also:
http://gcc.gnu.org/onlinedocs/gfortran/Mixed_002dLanguage-Programming.html

Tobias

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-03-30 17:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-30 17:50 Accessing C++ function with Mixed Upper/Lower case name from F90 subroutine Glenn H Sembroski
2010-03-30 23:58 ` Harald Servat
2010-03-31 11:33   ` Toon Moene
2010-03-31 12:51     ` Tobias Burnus
2010-03-31 11:29 ` Axel Freyn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).