Calling Fortran functions from .cu files (linking problem)

Hi everybody, I am new into CUDA and nvcc, so hopefully my doubt will be trivial!

I have to call a fortran function from a .cu file, so I set up a very simple example. Here are the two files:

helloworld.cu

#include <stdio.h>
#include
#include <cuda.h>
#include <cuda_runtime.h>

void extern hello_(void);
global void kernel(void){
printf(“hello world CU\n”);
}

int main (void){
kernel<<<1,1>>>();
printf(“hello world C \n”);
cudaDeviceSynchronize();
hello_();

return 0;
}

hello.f90
subroutine hello()
implicit none

write(*,*) "Hello from Fortran routine..."

end subroutine hello

finally, I compile the two as follow:

nvcc -c helloworld.cu;
gfortran -ffree-form -c hello.f90;
nvcc -o hiFromAll helloworld.o hello.o -lgfortran ;

getting the following output:

helloworld.o: In function main': tmpxft_00000604_00000000-4_helloworld.cudafe1.cpp:(.text+0x90): undefined reference to hello_()’
collect2: error: ld returned 1 exit status

Does somebody have any idea where the error in the linking procedure is?

I have also made some tests with the correspondent .c .f90 version (excluding the cuda calls and the nvcc compilation) and it works…

nvcc: NVIDIA ® Cuda compiler driver
Copyright © 2005-2015 NVIDIA Corporation
Built on Tue_Aug_11_14:27:32_CDT_2015
Cuda compilation tools, release 7.5, V7.5.17

GNU Fortran (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4

instead of this:

void extern hello_(void);

try this:

extern “C” void hello_(void);

nvcc uses C++ style linking, so you need to force C-style linking:

http://pages.tacc.utexas.edu/~eijkhout/istc/html/language.html

Thanks txbob, that solved the problem! Also thanks for the link…it is very useful.