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). 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.
My first attempt was to call the fortran subroutine from main.cpp. 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
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
#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
).
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. To avoid this, you'll need to prepend extern "C"
before the function declaration:
extern "C" void bar_ftn ( int flag_len, char* flag ); // note that 'extern' by itself is unnecessary
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.