Passing address of a C function to Fortran - syntax??

A

Adrian

I am trying to pass the address of a C++ function into a Fortran
routine to enable the Fortran routine to call this C++ function. I
have to do it this way as our build process does not allow circular
dependencies of DLL's.

Does anyone know how to do this, I have tried everything in my book.

I have a C++ function GetP:

DllExport void GetP()
{
...code...
return;
}

I am trying to pass the address of this function into a Fortran
routine called BIND_FTN. The call is made from another area of the
C++ code as follows:

void (GetP)();
DllImport void __stdcall BIND_FTN(int* GetP);

void Initialize()
{
...code...
BIND_FTN(GetP); // inform Fortran of address of callback
function
return;
}

The C++ will not compile, I get:
error C2664: 'BIND_FTN' : cannot convert parameter 1 from 'void
(__cdecl *)(void)' to 'int *'

Can anyone see the problem here?

Here is the Fortran code for BIND_FTN:

subroutine BIND_FTN_EXTERNALS[DLLEXPORT](ext)
external ext

interface
subroutine cb_GetP()
!DEC$ ATTRIBUTES STDCALL :: cb_GetP
end subroutine cb_GetP
end interface

pointer (p_GetP, cb_GetP)
p_GetP = loc(ext)

call cb_GetP ! call the C++ function from Fortran

return
end

(I haven't got to the Fortran yet - there may be syntax errors in
there)

Adrian
 
J

Jack Klein

I am trying to pass the address of a C++ function into a Fortran

Your header said a C function, now you say C++ function. C++
functions, and C++ itself for that matter, are off-topic in
comp.lang.c. But before you run off to comp.lang.c++...
routine to enable the Fortran routine to call this C++ function. I
have to do it this way as our build process does not allow circular
dependencies of DLL's.

Neither C nor C++ defines an interface to any other language.
Does anyone know how to do this, I have tried everything in my book.

Perhaps you need another book.

In any case, this is not a language question (C, C++, or FORTRAN) at
all. This is a question about specific compiler and linker operations
for your particular platform.

You need to ask in one or more groups that support the particular C++
and FORTRAN compilers involved.
 
A

Adrian

Jack Klein said:
Your header said a C function, now you say C++ function. C++
functions, and C++ itself for that matter, are off-topic in
comp.lang.c. But before you run off to comp.lang.c++...

Doesn't matter - it's actually C code but it exists in a C++
application - I posted it here because it is really a C question -
there is nothing C++ specific about it.
Neither C nor C++ defines an interface to any other language.

Wrong. I have called Fortran routines from C / C++ for years.
Perhaps you need another book.

Your sarcasm is not appreciated - I came here for advice not cynical
comment.
In any case, this is not a language question (C, C++, or FORTRAN) at
all. This is a question about specific compiler and linker operations
for your particular platform.

You need to ask in one or more groups that support the particular C++
and FORTRAN compilers involved.

If you don't have any constructive comment and certainly if you don't
know what I'm talking about, it's best not to post.

Adrian
 
L

Lawrence Kirby

Doesn't matter - it's actually C code but it exists in a C++
application - I posted it here because it is really a C question -
there is nothing C++ specific about it.

Except that C++ has an extern "???" mechanism whereas C does not,
typically code that interfaces to other languages that is compiled with a
C++ compiler will need to be be different to such code compiled with a C
compiler.
Wrong. I have called Fortran routines from C / C++ for years.

Jack is correct, noting that C++ has extern "C" but of course in this
context C isn't an "other" language.

You need to understand the distinction between what the language supports
and what a particular compiler/implemnentation supports. For example in
your code you have DllExport which is not a part of either the C or C++
languages, I'm guessing it is an extension provided by some Windows
related compiler/implementation you are using.
Your sarcasm is not appreciated - I came here for advice not cynical
comment.

Whether you consider it sarcastic or not, it could very well be what you
need.
If you don't have any constructive comment and certainly if you don't
know what I'm talking about, it's best not to post.

Jack gave you the best possible advice. Neither the C nor C++ languages
address your question, you need to find a platform-specific answer that
does. You need to find the experts in your platform in a newsgroup where
such discussion is topical. There are *lots* of Windows related newsgroups
available, maybe try something like comp.os.ms-windows.programmer.win32,
or one of the microsoft.public.* newsgroups.

Lawrence
 
N

Neil Kurzman

Lawrence said:
Except that C++ has an extern "???" mechanism whereas C does not,
typically code that interfaces to other languages that is compiled with a
C++ compiler will need to be be different to such code compiled with a C
compiler.


Jack is correct, noting that C++ has extern "C" but of course in this
context C isn't an "other" language.

You need to understand the distinction between what the language supports
and what a particular compiler/implemnentation supports. For example in
your code you have DllExport which is not a part of either the C or C++
languages, I'm guessing it is an extension provided by some Windows
related compiler/implementation you are using.


Whether you consider it sarcastic or not, it could very well be what you
need.


Jack gave you the best possible advice. Neither the C nor C++ languages
address your question, you need to find a platform-specific answer that
does. You need to find the experts in your platform in a newsgroup where
such discussion is topical. There are *lots* of Windows related newsgroups
available, maybe try something like comp.os.ms-windows.programmer.win32,
or one of the microsoft.public.* newsgroups.

Lawrence

This becomes a FORTRAN question.

"extern C" tells the compiler that the the parameters a push on the stack (assuming there is one) the C way,
not the C++ way. It is the FORTRAN compiler than must call the foreign library.
 
D

Dik T. Winter

> Lawrence Kirby wrote: ....

Might be the case, that does not mean that the way to do it is platform
independent. It is not. And the way to do it may well depend on the
kind of arguments you are passing (especially strings are tricky).
....
> This becomes a FORTRAN question.
>
> "extern C" tells the compiler that the the parameters a push on the stack
> (assuming there is one) the C way, not the C++ way. It is the FORTRAN
> compiler than must call the foreign library.

Let me expand on an earlier comment (see above):
*Neither C nor C++ nor Fortran defines an interface to any other language.*
The best way is (as remarked before) to ask in a platform or compiler
specific platform. BTW, I have worked on platforms were it was simply
impossible to do directly. You had to write an interface routine in
assembler to properly set up stack and heap environment for the C routines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top