Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Fortran calls C

Intel_C_Intel
Employee
846 Views
Hi,
I'm brushing up my Fortran- and C-knowledge
because I have to call C-functions
(I only have the static library and header)
from Fortran. The basic stuff (naming and calling
conventions) is already done and works,
one problem remains:
How do I call the following:
extern void Cfunc1(char** Result);
which returns a pointer to a pointer to a string?
The other way round (passing a string to a Cfunction)
I managed with an interface:
extern void Cfunc2(const char* str);
INTEGER(4) FUNCTION Cfunc2[C, ALIAS:'_Cfunc2'](str)
CHARACTER*(*)str [REFERENCE]
END FUNCTION Cfunc2
Thx in advance for hints,
Toni
0 Kudos
6 Replies
Intel_C_Intel
Employee
846 Views
Hello. I you are using a C++ compiler, you can either use the compiler option /TC to avoid C++ mangling or write < extern "C" > before the function. Also you need to have the same calling convention, for examptle __stdcall or __cdecl to make it work. Regarding pointer to a pointer to a string, I think that it will be possible to pass the pointer from C to Fortran, but I am not sure how you could use that pointer the best way on the Fortran side. Maybe the workaround may be to pass pointer to string only and instead rewrite the code to pass all the string pointers you need to transfer. Lars Petter
0 Kudos
Intel_C_Intel
Employee
846 Views
Thanks Lars,
I've managed calling conventions and name mangling ;-)
I'm working on the problem how to use the (char**) in Fortran.
There are two ideas:
1. CVF has a lot of extensions that could solve the problem,e.g. integer pointer. Unfortunately not standard.
2. Probably better: write a C-wrapper.
Regards,
Toni
0 Kudos
Steven_L_Intel1
Employee
846 Views
There's an entire chapter in the CVF Programmer's Guide/IVF User Manual on mixed-language programming. It tells you most everything you need to know. There are also samples provided with CVF.
0 Kudos
Intel_C_Intel
Employee
846 Views
Thanks Steve,
from these ressoruces I learnt to call C from Fortran,
even to access a C-DLL dynamically.
The remaining problem is how to use a (char**) returned by a C-routine.
Regards, Toni
0 Kudos
Steven_L_Intel1
Employee
846 Views
C is not my best language - that's a pointer to something which is a pointer to characters? You'd do something like this

integer(int_ptr_kind()) :: pchar
character(*) :: string
pointer (p1, pchar)
pointer (p2, string)

call c_routine (p1)
! p1 has the char** value
p2 = pchar ! Dereference through p1
! Now you can access string
0 Kudos
Intel_C_Intel
Employee
846 Views
Well, I'm not very experienced in C, too.
Your code looks good, I'll try it!
Thanks, Toni
0 Kudos
Reply