Hello,
I'm trying to link C++ and fortran prorgam together, but I get the following error
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
The main program is fortran, calling a C routine. The linking must be done using C compiler (Because in my work I'm in fact compiling with nvcc (nvidia compiler for cuda) and the linking then can not be done using fortran compiler). I didn't find on the web anything that worked.
Moreover, GNU compiler is working fine (I pasted the test code at the end),
g++ -lstdc++ -g -c add.c g++ -g -o main main.o add.o -lstdc++ -lgfortran
and then,
Hola a+b = 3 6 9 12 15
But compilation with Intel compiler fails,
ifort -cxxlib -lstdc++ -g -c main.f90 icpc -cxxlib -lstdc++ -g -c add.c icpc -g -o main main.o add.o -lstdc++ -lifcore /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' Makefile:12: recipe for target 'main' failed make: *** [main] Error 1
Does someone know what is wrong with my compilation?
Thank you,
Marc Barbry
The C code (add.c),
#include <stdlib.h> #include <stdio.h> #include "add.h" extern "C" void addition_(int *a, int *b, int *c, int *Np) { int N = *Np; int i; for (i=0; i<N; i++) { c[i] = a[i] + b[i]; } }
C header (add.h),
extern "C" void addition_(int *a, int *b, int *c, int *Np);
and Fortran code (main.f90),
program main implicit none integer, allocatable :: a(:), b(:), c(:) integer :: i, N N = 5 print*, 'Hola' allocate(a(N)) allocate(b(N)) allocate(c(N)) c = 0 do i=1, N a(i) = i b(i) = 2*i enddo call addition(a, b, c, N) print*, "a+b = ", c deallocate(a) deallocate(b) deallocate(c) end program