我想从 Fortran 调用一个 C++ 函数。为此,我在 Visual Studio 2010 中创建了一个 FORTRAN 项目。之后,我将一个 Cpp 项目添加到该 FORTRAN 项目中。当我想构建程序时出现以下错误:
Error 1: unresolved external symbol print_C referenced in function MAIN_main.obj
Error 2: 1 unresolved externals
以下是 Fortran 程序和 C++ 函数。
Fortran 程序:
program main
use iso_c_binding, only : C_CHAR, C_NULL_CHAR
implicit none
interface
subroutine print_c ( string ) bind ( C, name = "print_C" )
use iso_c_binding, only : C_CHAR
character ( kind = C_CHAR ) :: string ( * )
end subroutine print_c
end interface
call print_C ( C_CHAR_"Hello World!" // C_NULL_CHAR )
end
C++ 函数:
# include <stdlib.h>
# include <stdio.h>
void print_C ( char *text )
{
printf ( "%s\n", text );
return;
}
提前非常感谢。