在Visual Studio 2010中从Fortran调用C ++函数 [英] Calling C++ function from Fortran in Visual Studio 2010

查看:115
本文介绍了在Visual Studio 2010中从Fortran调用C ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Fortran调用C ++函数.为此,我在Visual Studio 2010中创建一个FORTRAN项目.此后,我向该FORTRAN项目中添加了一个Cpp项目.当我要构建程序时,会发生以下错误:

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;
}
 

非常感谢.

解决方案

您的问题是,由于使用C ++编译器进行编译,因此print_C不是C函数,而是C ++函数.由于Fortran代码尝试调用C函数,因此找不到C ++函数.

因此,解决您的问题的方法是

  • 可以使用C编译器进行编译,因此您可以获得实际的C代码,
  • 或告诉C ++编译器您实际上要声明C函数.

后者是通过extern "C"完成的,如下所示:

extern "C" void print_C(char *text)
{
  printf("%s\n", text);
}

如果您希望能够同时将代码编译为C和C ++,则可以使用

#ifdef __cplusplus
extern "C"
#endif
void print_C(char *text)
{
  printf("%s\n", text);
}

符号__cplusplus是为C ++定义的,但没有为C定义的,因此您的代码将在两者中都可以工作(当然,只要其余代码也保留在该子集中).

I want to call a C++ function from Fortran. To do that, I make a FORTRAN project in Visual Studio 2010. After that I add a Cpp project to that FORTRAN project. The following errors occur when I want to build the program:

Error 1: unresolved external symbol print_C referenced in function MAIN_main.obj    
Error 2:    1 unresolved externals

Following are the Fortran program and C++ function.

Fortran program:

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++ function:

# include <stdlib.h>
# include <stdio.h>

void print_C ( char *text )

{
  printf ( "%s\n", text );

  return;
}

Thanks a lot in advance.

解决方案

Your problem is that since you compile with a C++ compiler, print_C is not a C function, but a C++ function. Since the Fortran code tries to call a C function, it cannot find the C++ function.

The solution to your problem therefore is

  • either compile with a C compiler, so you get actual C code,
  • or tell the C++ compiler that you actually want to declare a C function.

The latter is done with extern "C", like this:

extern "C" void print_C(char *text)
{
  printf("%s\n", text);
}

If you want to be able to compile your code both as C and as C++, you can use

#ifdef __cplusplus
extern "C"
#endif
void print_C(char *text)
{
  printf("%s\n", text);
}

The symbol __cplusplus is defined for C++, but not for C, so your code will work in both (of course only as long as the rest of your code also remains in that subset).

这篇关于在Visual Studio 2010中从Fortran调用C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆