Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

mixed language linking error

jz001
初学者
1,305 次查看
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 项奖励
1 解答
Tim_Gallagher
新分销商 II
1,305 次查看
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 项奖励
4 回复数
Tim_Gallagher
新分销商 II
1,306 次查看
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 项奖励
mecej4
名誉分销商 III
1,305 次查看
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 项奖励
TimP
名誉分销商 III
1,305 次查看
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 项奖励
jz001
初学者
1,305 次查看
@Tim

Tanks it works
0 项奖励
回复