Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"Undefined reference" for C++ call of fortran subroutine

1,249 views
Skip to first unread message

Arno

unread,
Mar 25, 2015, 9:07:30 AM3/25/15
to
Dear all,

I am struggling with the following:

main.cpp:

#include <stdio.h>

extern"C" {void my_write(void);}

int main(void)
{
printf("hello Cpp world\n");

my_write();

return 0;
}


mymodule.f90:

module mymodule
use, intrinsic :: ISO_C_BINDING

contains

subroutine my_write() bind(C,name="my_write")

write(*,*) 'hello fortran world'

end subroutine

end module mymodule


Compiling results in an error:

> ifort -c mymodule.f90
> icc mymodule.o main.cpp
mymodule.o: In function `my_write':
mymodule.f90:(.text+0x4b): undefined reference to `for_write_seq_lis'

Is there something wrong with my code, or is it the compiler?

Thank you for time.

Arno

FX

unread,
Mar 25, 2015, 9:16:19 AM3/25/15
to
>> ifort -c mymodule.f90
>> icc mymodule.o main.cpp
> mymodule.o: In function `my_write':
> mymodule.f90:(.text+0x4b): undefined reference to `for_write_seq_lis'

You need to compiler the two object files (mymodule.o and main.o) with
icc and ifort respectively, and then do the final linking with ifort
instead of icc.

--
FX

Wolfgang Kilian

unread,
Mar 25, 2015, 9:32:23 AM3/25/15
to
This will work only if ifort links with the C++ runtime libraries by
default. If it doesn't (I can't check this),

ifort main.o mymodule.o -Lpath_to_icc_libraries -lstdc++

or something similar might be necessary.

-- Wolfgang

Arno

unread,
Mar 25, 2015, 10:07:06 AM3/25/15
to
Thank you. The combination of both replies worked for me.

Beliavsky

unread,
Mar 25, 2015, 10:37:22 AM3/25/15
to
Out of curiosity I decide to try with gcc compilers. Using gfortran and g++, the following works:

gfortran -c mymodule.f90
g++ -c main.cpp
gfortran main.o mymodule.o

as does replacing gfortran with g95. I assume that using ifort instead of gfortran but still using g++ would also work?

Arno

unread,
Mar 25, 2015, 11:39:18 AM3/25/15
to
I changed the code to also include a global variable:

#include <stdio.h>

extern"C" {void my_write(void);}

double rco2;

int main(void)
{
rco2 = 2.0;

printf("hello Cpp world\n");
printf("rco2 = %6.2f \n",rco2);

my_write();

return 0;
}
module mymodule
use, intrinsic :: ISO_C_BINDING

REAL(C_DOUBLE),BIND(C) :: rco2

contains

subroutine my_write() bind(C,name="my_write")

write(*,*) 'hello fortran world'
write(*,*) rco2

end subroutine

end module mymodule


This is what happens with g++, respectively, icc:




> ifort -c mymodule.f90
> g++ -c main.cpp
> ifort main.o mymodule.o
main.o: In function `main':
main.cpp:(.text+0x0): multiple definition of `main'
/opt/local/SRON64/intel/composer_xe_2015.1.133/compiler/lib/intel64/for_main.o:/export/users/nbtester/efi2linux_nightly/branch-15_0/20141024_000000/libdev/frtl/src/libfor/for_main.c:(.text+0x0): first defined here
/opt/local/SRON64/intel/composer_xe_2015.1.133/compiler/lib/intel64/for_main.o: In function `main':
/export/users/nbtester/efi2linux_nightly/branch-15_0/20141024_000000/libdev/frtl/src/libfor/for_main.c:(.text+0x2a): undefined reference to `MAIN__'

> ifort -nofor-main main.o mymodule.o
>

So, with g++ it works with the nofor-main flag. Now for icc:

> ifort -c ifort main.o mymodule.o
> icc -c main.cpp
> ifort main.o mymodule.o
main.o: In function `main':
main.cpp:(.text+0x0): multiple definition of `main'
/opt/local/SRON64/intel/composer_xe_2015.1.133/compiler/lib/intel64/for_main.o:/export/users/nbtester/efi2linux_nightly/branch-15_0/20141024_000000/libdev/frtl/src/libfor/for_main.c:(.text+0x0): first defined here
/opt/local/SRON64/intel/composer_xe_2015.1.133/compiler/lib/intel64/for_main.o: In function `main':
/export/users/nbtester/efi2linux_nightly/branch-15_0/20141024_000000/libdev/frtl/src/libfor/for_main.c:(.text+0x2a): undefined reference to `MAIN__'
main.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'

> ifort -nofor-main main.o mymodule.o
main.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'

> ifort -nofor-main -lstdc++ main.o mymodule.o
>

In case if icc, it works with the flags nofor-main and lstdc++

Not sure, what this means.

Arno

unread,
Mar 25, 2015, 11:47:13 AM3/25/15
to
One more thing that I noticed is that the use of the -ftrapuv flag (one of my favourite flags) prevents succesful compilation:

> ifort -ftrapuv -c mymodule.f90
> icc -c main.cpp
> ifort -nofor-main -lstdc++ -ftrapuv mymodule.o main.o
main.o:(.bss+0x0): multiple definition of `rco2'
mymodule.o:(.data+0x0): first defined here

Without the ftrapuv flag it compiles (and runs!) succesfully, but with this flag, it doesn't. Was this to be expected?

Tim Prince

unread,
Mar 25, 2015, 2:40:57 PM3/25/15
to
ifort links automatically all the usual icc libraries (and gcc ones, on
linux), but not stdc++, so Wolfgang is mostly correct
ifort main.o mymodule.o -lstdc++
0 new messages