如何在Visual Studio 2010中混合Fortran和C ++? [英] how to mix fortran and C++ in visual studio 2010?
问题描述
我试图从Fortran主程序调用c ++函数。要做到这一点,我遵循visual Studio 2010中的以下步骤:
创建C ++静态库项目
- 在菜单栏上,选择File,New,Project。
- 在New Project对话框的左窗格中,展开Installed,Templates,Visual C ++,然后选择Win32。 b
- 在中心窗格中,选择Win32控制台应用程序。
- 在名称框中为项目指定名称,例如MathFuncsLib。在解决方案名称框中指定解决方案的名称(例如,StaticLibrary)。选择OK按钮。
- 在Win32 Application Wizard对话框的Overview页面上,选择Next按钮。
- 在Application Settings页面上,在应用程序类型下,选择静态库。
- 在应用程序设置页面的其他选项下,清除预编译头复选框。按钮创建项目。
创建可执行的Fortran项目
- 在菜单栏上,选择File,New,Project。
- 在New Project对话框的左窗格中,展开Installed,Templates,Intel(R) Visual Fortran,然后选择控制台应用程序。
- 在中心窗格中,选择空项目。 为项目指定名称,然后指定解决方案的名称。在解决方案框中选择添加到解决方案。选择确定按钮。
另外我在Visual Studio中进行如下设置:
- 右键单击可执行的Fortran项目并选择Dependencies以将可执行项目设置为依赖于静态库项目。
- 右键单击可执行项目并选择Set as Startup Project,以便您可以构建它并进行调试。
我有下面的Fortran主程序和C ++函数。
Fortran程序
程式main
使用iso_c_binding,只有:C_CHAR,C_NULL_CHAR
隐含无
接口
子程序print_C(字符串)bind(C,name =print_C)
仅使用iso_c_binding:C_CHAR
字符(kind = C_CHAR):: string (*)
结束子程序print_C
结束接口
调用print_C(C_CHAR_Hello World!// C_NULL_CHAR)
结束
C ++函数
#include< stdlib.h>
#include< stdio.h>
externCvoid print_C(char * text)
{
printf(%s\,text);
}
当我构建程序时,我会面对以下错误:
错误1:错误LNK2019:无法解析的外部符号_print_C在函数_MAIN__中引用Fortranmain.obj
错误2:致命错误LNK1120:1无法解析的外部Debug \Fortranmain.exe
任何人都可以帮我吗?任何建议都将非常感谢。
您需要将C ++库链接到Fortran可执行文件。右键单击Fortran项目并选择属性
I am trying to call the c++ function from Fortran main program. to do that I followed the bellow steps in visual Studio 2010: To create a C++ static library project
- On the menu bar, choose File, New, Project.
- In the left pane of the New Project dialog box, expand Installed, Templates, Visual C++, and then select Win32.
- In the center pane, select Win32 Console Application.
- Specify a name for the project—for example, MathFuncsLib—in the Name box. Specify a name for the solution—for example, StaticLibrary—in theSolution Name box. Choose the OK button.
- On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.
- On the Application Settings page, under Application type, select Static library.
- On the Application Settings page, under Additional options, clear the Precompiled header check box.
- Choose the Finish button to create the project.
To create a executable Fortran project
- On the menu bar, choose File, New, Project.
- In the left pane of the New Project dialog box, expand Installed, Templates, Intel(R) Visual Fortran , and then select Console Application.
- In the center pane, select Empty Project.
- Specify a name for the project and then Specify a name for the solution. In the solution box select the "Add to solution". Choose the OK button.
In addition I do some setting in Visual studio as below:
- Right-click the executable Fortran project and select Dependencies to set the executable project as dependent on the static library project.
- Right-click on the executable project and select Set as Startup Project so that you can build it and debug. I have the below Fortran main 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>
extern "C" void print_C (char *text)
{
printf("%s\n", text);
}
When I build the program I will confront to the following errors:
Error 1: error LNK2019: unresolved external symbol _print_C referenced in function _MAIN__ Fortranmain.obj
Error 2: fatal error LNK1120: 1 unresolved externals Debug\Fortranmain.exe
Could anyone help me? Any suggestion would be highly appreciate.
You need to link C++ library to Fortran executable. Dependencies are specifying the build order.
- Right-click the executable Fortran project and select Properties
- Add directory containing build C++ library to Configuration Properties -> Linker - General -> Additional Library Directories (This step is maybe not needed)
- Add C++ library .lib file to Configuration Properties -> Linker -> Input -> Additional Dependancies
这篇关于如何在Visual Studio 2010中混合Fortran和C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!