Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Fortran/C++ binding error

alphajet
Beginner
644 Views
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
0 Kudos
1 Solution
JVanB
Valued Contributor II
644 Views
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)

View solution in original post

0 Kudos
2 Replies
JVanB
Valued Contributor II
645 Views
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)
0 Kudos
alphajet
Beginner
644 Views
Thanks alot, it's working now !
Thanks for your prompt and to-the-point reply
0 Kudos
Reply