Reputation: 69
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
To create a executable Fortran project
In addition I do some setting in Visual studio as below:
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.
Upvotes: 3
Views: 1915
Reputation: 10195
You need to link C++ library to Fortran executable. Dependencies are specifying the build order.
Upvotes: 2