Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

mixed language linking error

jz001
Beginner
1,229 Views
Hello,

I'm trying ot integrate a Unix domain socket C routine into my Fortran90 program. Therefore I've created a simple test example. I'm using openSUSE 11.1, Intel Fortran Compiler 11.1 and Intel C Compiler 11.1.

The C routine:

// C++ - Routine
#include
#include
extern "C" void pythagoras (float a, float b, float *c)
{
*c = (float) sqrt(a*a+b*b);
}

The Fortran program:

module cproc
interface
subroutine pythagoras (a, b, res )
! DEC$ ATTRIBUTES C :: pythagoras
! DEC$ ATTRIBUTES REFERENCE :: res
real :: a, b, res
end subroutine
end interface
end module


program fmain
use cproc
implicit none
real :: x, y, z

write (* ,*) ' Berechnung der Hypotenusenlaenge eines rechtwickligen Dreiecks '
write (* ,*) ' Geben Sie die beiden Seitenlaengen ein , die den rechten Winkel '
write (* ,*) ' einschliessen :'
read (* ,*) x, y

call pythagoras (x,y,z)
write (* ,*) ' Die Laenge der Hypotenuse betraegt : ', z
end program fmain


The C routine is compiled by:

icc -c crout.cpp

The program then is compiled and linked by:

ifort fmain.f90 crout.o -cxxlib -o prog.out

The error message:

/tmp/ifortMMrDlc.o: In function `MAIN__':
fmain.f90:(.text+0x170): undefined reference to `pythagoras_'

occurs.

Is there is someone who can help me?

Thanks in advance.
jz001

0 Kudos
1 Solution
Tim_Gallagher
New Contributor II
1,229 Views
Put the .o file before the .f90 file on the ifort command line, and if that doesn't do it, try adding the underscore _ to the end of the C routine name and recompiling.

Tim

View solution in original post

0 Kudos
4 Replies
Tim_Gallagher
New Contributor II
1,230 Views
Put the .o file before the .f90 file on the ifort command line, and if that doesn't do it, try adding the underscore _ to the end of the C routine name and recompiling.

Tim
0 Kudos
mecej4
Honored Contributor III
1,229 Views
The Mixed Language Programming chapter of the Intel Fortran Users Guide will help you with this program. You should read about the different possible ways of linking C/C++ with Fortran, and make a selection suited to your needs.

The default calling convention for Fortran is by address, and the default symbol decoration is to append an underscore. One solution is to change the C++ routine to

// C++ - Routine
#include
extern "C" void pythagoras (float *pA, float *pB, float *c)
{float a=*pA, b=*pB;
*c = (float) sqrt(a*a+b*b);
}

and to compile using:

g++ -c pyth.cpp
ifort -assume nounderscoring fmain.f90 pyth.o
0 Kudos
TimP
Honored Contributor III
1,229 Views
Needless to say, if portable code is acceptable, you should be using the ISO C interoperability implemented in all Fortran compilers of the last few years.
0 Kudos
jz001
Beginner
1,229 Views
@Tim

Tanks it works
0 Kudos
Reply