- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I was trying to run a simple binding example between Fortran/C++, where Fortran calls a C++ function, the files are:
(cprog.cpp) C++
[cpp]#include
void print_C(char *string) /* equivalent: char string[] */{
printf("%s\n", string);
}
int main (void){
return 0;
}[/cpp] (fprog.f90) Fortran
[bash]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)
pause
end[/bash] I then attempt to run the files as follows:
1- compiling the C++ source:
cl /EHsc cprog.cpp
2- compiling the Fortran source
ifort fprog.f90 cprog.obj
But I get the following error:
unresolved external symbol print_C
which is the C++ function I'm trying to use.
Note: the example I copied these codes from didn't have a main function in the C++ source file, so when I tried to compile the C++ source, it threw me an error:
unresolved external symbol main
but the error went away when I declared the main function, and I'm now stuck at the print_C error
Thanks in advance
1- compiling the C++ source:
cl /EHsc cprog.cpp
2- compiling the Fortran source
ifort fprog.f90 cprog.obj
But I get the following error:
unresolved external symbol print_C
which is the C++ function I'm trying to use.
Note: the example I copied these codes from didn't have a main function in the C++ source file, so when I tried to compile the C++ source, it threw me an error:
unresolved external symbol main
but the error went away when I declared the main function, and I'm now stuck at the print_C error
Thanks in advance
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Get rid of the main() function in your C++ code. Instead compile with the /c (compile only) switch.
C++ mangles names unless you tell it not to. You must give print_C() the extern C attribute. Thus:
extern "C" void print_C(char *string)
C++ mangles names unless you tell it not to. You must give print_C() the extern C attribute. Thus:
extern "C" void print_C(char *string)
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Get rid of the main() function in your C++ code. Instead compile with the /c (compile only) switch.
C++ mangles names unless you tell it not to. You must give print_C() the extern C attribute. Thus:
extern "C" void print_C(char *string)
C++ mangles names unless you tell it not to. You must give print_C() the extern C attribute. Thus:
extern "C" void print_C(char *string)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks alot, it's working now !
Thanks for your prompt and to-the-point reply
Thanks for your prompt and to-the-point reply

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page