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

How to code C++ function to call different Fortran function (more than 1)?

wisperless
Beginner
1,100 Views

I've been trying to call different Fortran functions from C++. But it looks to me, the C++ functioncompiled byVC++2005can only call 1Fortran function(IVF 9.1). For example, if a Fortran file (f1.f90) has two Fortran functions ( F1, F2)

subroutine F1()

end subroutine

subroutine F2()

end subroutine

I am wondering, from the C++ function, how to call that two functions(F1 and F2). The following code is not working

void F1();

void F2();

int main()

{

F1();

F2();

}

Thank you in advance!

0 Kudos
5 Replies
hajek
Beginner
1,100 Views
In a C++ program you should add extern "C" to the declarations, otherwise name-mangling may take place. Also, IIRC, Intel (as well as most other compilers) adds an underscore to Fortran names, thus you should declare the subs as
extern "C" void F1_();
...
0 Kudos
wisperless
Beginner
1,100 Views

Thanks for your pointing out the missed extern "C". I did have that in my computer. I guess my problem is how to call a Fortran subroutine in module from visual C++. I can not even access a variable in the module. Here is my code.

Visual C++:

#include

"stdafx.h"

#ifdef

__cplusplus

extern

"C"

#endif

extern

"C" int EXAMP_mp_A;

int

main()

{

printf(

"test_mp_a %5.1f",EXAMP_mp_A);

return 0;

}

Here is my fortran code:

module

EXAMP

INTEGER

A

!DEC$ ATTRIBUTES DLLEXPORT::EXAMP

contains

subroutine

MODFUN

! Expose subroutine MODFUN to users of this DLL

!

write(*,*) "Call from fortran"

! Variables

! Body of MODFUN

end subroutine

MODFUN

end module

EXAMP

The compiling error is

C_CALL_MOD.obj : error LNK2001: unresolved external symbol _EXAMP_mp_A

Anyone can help me out?

Thank you in advance!!


					
				
			
			
				
			
			
			
			
			
			
			
		
0 Kudos
wisperless
Beginner
1,100 Views
Ok, I think I solve the problem after adding A

!DEC$ ATTRIBUTES DLLEXPORT::MODFUN

0 Kudos
hajek
Beginner
1,100 Views

I'm a little confused - I thought you were using static library. Anyway, the DLLEXPORT attribute for a module name has no effect (though it's allowed).

It is somewhat more portable to export only stand-alone subprograms, not module procedures - this way you avoid the name mangling (which is probably fixed forever by Intel compilers, but may differ with other compilers).

Alas, if you want to do the same for variables, you have to resort to the old ugly common blocks. Or wait for the C interop stuff.

By the way, the double extern "C" you specify in your C++ header is redundant. The one specified within the #ifdef block suffices.

0 Kudos
wisperless
Beginner
1,100 Views
Thank you for your help. I changed to DLL and that's the reason why I added the export. Sorry for the confusion.
0 Kudos
Reply