- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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,
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?
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
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
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well, I'm not very experienced in C, too.
Your code looks good, I'll try it!
Thanks, Toni

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