[英]Linking C/C++ and Fortran, unresolved external symbols
My Visual studio 2010 solution file for a typical purpose consists of one fortran project (of static library type and consists of source1.f90), one C/C++ project(of application type and contains main.cpp) and 13 C/C++ project(of static library type and contains different .cpp/.h files for different classes). 我的Visual Studio 2010解决方案文件用于典型目的,包括一个fortran项目(静态库类型,由source1.f90组成),一个C / C ++项目(应用程序类型和包含main.cpp)和13个C / C ++项目(静态库类型,包含不同类的不同.cpp / .h文件)。 My purpose is to call the some of the functions in fortran source files from one of the C/C++ static library type project, But I am not able to build the program and am getting errors.
我的目的是从一个C / C ++静态库类型项目中调用fortran源文件中的一些函数,但是我无法构建程序并且遇到错误。
My first attempt was to call the fortran subroutine from main.cpp. 我的第一次尝试是从main.cpp调用fortran子程序。 But I am getting the following error:
但是我收到以下错误:
Error 2 error LNK2019: unresolved external symbol "void __cdecl
bar_ftn(int,char *)" (?bar_ftn@@YAXHPAD@Z) referenced in function _main
G:\VS2010\uakron\sourcefiles\application\main.obj
Error 3 error LNK1120: 1 unresolved externals G:\VS2010\uakron
\build\win\debug\application_app.exe 1
source1.f90 source1.f90
subroutine bar_ftn ( len_input_file, input_file ) bind( c )
use, intrinsic :: iso_c_binding, only : c_int
implicit none
integer(c_int), value, intent(in) :: len_input_file
character(len=1), intent(in) :: input_file(len_input_file)
! Local declarations (copy c char array into fortran character)
character(len=len_input_file) :: infile
integer :: i
print *, "in bar_ftn"
print *, len_input_file
do i=1,len_input_file
end do
end subroutine bar_ftn
main.cpp main.cpp中
#include<iostream>
#include<fstream>
using namespace std;
extern void bar_ftn ( int flag_len, char* flag );
static void
DisplayUsage(char* programName);
int main(int argc, char *argv[])
{
char ctext[]="helloworld abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz";
int ctext_len=sizeof(ctext);
//Call the Fortran
bar_ftn( ctext_len, ctext );
return 0;
}
On the other side, I also called the fortran function from one of the class functions from one of the C/C++ static library projects, but I am getting the same type of error ( LNK2019
). 另一方面,我还从一个C / C ++静态库项目中的一个类函数调用了fortran函数,但是我得到了相同类型的错误(
LNK2019
)。
Any help would be highly appreciated. 任何帮助将受到高度赞赏。
One issue is that you're declaring bar_ftn
in C++, which results in name mangling , aka the odd ?bar_ftn@@YAXHPAD@Z
text you see in the error message. 一个问题是你在C ++中声明了
bar_ftn
,这会导致名称错误 ,也就是你在错误信息中看到的奇怪的?bar_ftn@@YAXHPAD@Z
文本。 To avoid this, you'll need to prepend extern "C"
before the function declaration: 为避免这种情况,您需要在函数声明之前添加
extern "C"
:
extern "C" void bar_ftn ( int flag_len, char* flag ); // note that 'extern' by itself is unnecessary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.