nvcc Compiler problem in FORTRAN-C Mixed language programming fortran module access

nvcc Compiler problem in FORTRAN-C Mixed language programming

Under platform windows xp, Geforce 8800GT and Visual studio 2005

I want to access the Fortran module global variables in nvcc subroutines.(I use Intel fortran 10.1.019 compiler).

As Intel fortran compiler’s document says,

modules are the simplest way to exchange large groups of variables with C, because Intel Fortran modules are directly accessible from C/C++.

The following example declares a module in Fortran, then accesses its data from C:

//—File ctest.c—

[codebox]

extern void SET_DATA(void)

{

extern int HOST_DATA_mp_A;

HOST_DATA_mp_A=10;

return;

}

[/codebox]

C—File ftest.for—

[codebox]

MODULE HOST_DATA

IMPLICIT NONE

INTEGER A

END MODULE

PROGRAM TEST

USE HOST_DATA

IMPLICIT NONE

CALL SET_DATA

PRINT*,‘A=’,A

STOP

END

[/codebox]

I first use nvcc compiler compile ctest.c to object file

nvcc -c ctest.c

This will generate ctest.obj file

Then I use ifort compiler to compile and link to execuatble file.

ifort ftest.for ctest.obj c:\cuda\lib\cuda.lib c:\cuda\lib\cudart.lib

As expected, this will generate file ftest.exe and can output the correct result.

But when I change the name of ctest.c to ctest.cu and try to compile and link them together again,

errors occured.

nvcc -c ctest.cu

ifort ftest.for ctest.obj c:\cuda\lib\cuda.lib c:\cuda\lib\cudart.lib

Intel® Visual Fortran Compiler for applications running on IA-32, Version 10.1

Build 20080212 Package ID: w_fc_p_10.1.019

Copyright © 1985-2008 Intel Corporation. All rights reserved.

Microsoft ® Incremental Linker Version 8.00.50727.42

Copyright © Microsoft Corporation. All rights reserved.

-out:ftest.exe

-subsystem:console

ftest.obj

ctest.obj

c:\cuda\lib\cuda.lib

c:\cuda\lib\cudart.lib

ftest.obj : error LNK2019: unresolved external symbol SET_DATA in fuction MAIN

ctest.obj : error LNK2019: unresolved external symbol “int HOST_DATA_mp_A” (?HOST_DATA

_mp_A@@3HA) in function “void __cdecl SET_DATA(void)” (?SET_DATA@@YAXXZ)

ftest.exe : fatal error LNK1120: external reference unresolved。

In fact, I did nothing except changing the file extension name.

But I don’t know why the second one can not be linked normally.

Anyone can tell me the reason or how to solve this problem(acess global variables in fortran module in nvcc)?

Thank you in advance.

I’ve never done Fortran interop, but I bet you need an
extern “C” to use C-style linkage and not mangled-name C++ linkage.

Perhaps nvcc defaults to C mode for files with .c and uses C++ mode for .cu?

Yep, that’s it. Check out this thread:

[url=“http://forums.nvidia.com/index.php?showtopic=83811”]http://forums.nvidia.com/index.php?showtopic=83811[/url]

That is right.

Lots of thanks