type(C_PTR) from ISO_C_BINDING questions

type(C_PTR) from ISO_C_BINDING questions

I have an interface to a stdcall libary, and the documentation says the function return a C pointer. The interface says:

PUBLIC :: fred
INTERFACE
FUNCTION fred() 
 !DEC$ ATTRIBUTES STDCALL, REFERENCE, DECORATE, ALIAS:"fred" :: fred
IMPORT
TYPE(C_PTR) :: fred
END FUNCTION fred
END INTERFACE

This is Window X32 and link the linker fails looking for _fred@4 whearas the correct libaray symbol is _fred@0.

For x32

Integer(4) :: fred
works fine so may questions are:
1) What is the content of type C_PTR, I would have thought is was (in x32) just and Integer(4)?
2) Is the !DEC$ line incorrect in this case, if so what should it have said. I guess removing the decorate and manually decorating would have worked perhaps, but that would just seem wrong.
3) Is is !DEC$ line actually not functioning correctly in this case?
4) Is the use of C_PTR incorrect and should I be using a KIND parameter that is INT_PTR_KIND()
anyway?
5) Is C_PTR use only relevant id using BIND(C) which won't work in this case anyway.
6) Could be have a none standard !DEC$ ATTRIBUTE bind_C_decorate please so we could have one at the top of the module file and not then have to have zillions of non-standard !dec$ attributes littered through the source file?

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

This is a CATCH-22 problem. The C code is returning a pointer as an integer value in a register, but Intel Fortran always passes a hidden argument for functions that return derived types, no matter what their size. In order to get something of type C_PTR returned in a register, you need to make the interface BIND(C). But Intel Fortran doesn't let you use BIND(C) with STDCALL.

The solution here is to use integer(C_INTPTR_T) as the function type. You'll then need to TRANSFER it to a C_LOC if you then want to use it with C_F_POINTER or the like.

The ATTRIBUTES directive you have specified is correct. Do NOT try to "fix" it by adding explicit decoration.

Steve - Intel Developer Support

Thanks Steve, that is pretty much what I thought but I needed to know for sure, the  integer(C_INTPTR_T) is a useful tip also.

BIND(C) really needs some extension for STDCALL, I understand the discussion about replacing one non-standard directive with a new non standard directive but it could be made a lot easier for users. Other fortran compiler have BIND(C) extensions.

On another note are there any software tools that you know of for converting C header files into fortran modules that you know of partcularly ones that understand pre-processor directives? 

Leave a Comment

Please sign in to add a comment. Not a member? Join today