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

Problem with COMMON (Fortran + C++)

zeliboba1
Beginner
539 Views
Hello
This question was probably asked more than once, sorry. I could not find a solution of my problem.
MS VS 2008 + IFC 11.0

*******************FORTRAN*******************
Subroutine F
!DEC$ ATTRIBUTES DLLEXPORT::F,/abc/
!DEC$ ATTRIBUTES ALIAS: '_F@0' :: F
!DEC$ ATTRIBUTES ALIAS: '_abc' :: /abc/

real*8 a
common /abc/ a

write(*,*) "Test!"
write(*,*) a
end

****************c++**************************
#include
#pragma comment(lib,"fdll.lib")
using namespace std;

struct variables { double a; };

extern "C" {
extern struct variables abc;
void __stdcall F(); };

int main()
{
abc.a=1.0;
F();
return 0;
}

**********************************************

Error "Undefined symbol _abc".

I think that the error in! DEC $ ATTRIBUTES ALIAS: '_abc' :: / abc /
I tried '_abc' :: / abc /, '/ _abc /' :: / abc /, '_abc' :: abc - the result is the same.
I tried to use the BIND () - leads to the same error.

Please tell me what is my mistake?
Best regards.
0 Kudos
4 Replies
Steven_L_Intel1
Employee
539 Views
You missed __declspec(dllimport) on the struct, like so:

extern __declspec(dllimport) struct variables abc;

Also, you should remove the __stdcall from the C++ and remove !DEC$ ATTRIBUTES ALIAS: '_F@0' :: F. If you ever think you need an alias with @n on the end, you're doing it wrong.

I would recommend using module variables and the standard C interoperability features instead of COMMON.
0 Kudos
TimP
Honored Contributor III
539 Views
There are standard C interoperability features for COMMON as well.
0 Kudos
zeliboba1
Beginner
539 Views
my question was stupid? :)
For some reason, nowhere in the articles "mix fortran and c++" I did't see mention of "extern __ declspec (dllimport) struct variables abc;" and BIND()
it worked, thanks!
0 Kudos
Steven_L_Intel1
Employee
539 Views
Discussion of DLL export and import isn't really related to mixed-language - it applies to same-language programming too. While you can get away without a "dllimport" directive/pragma for routines, you can't for data.
0 Kudos
Reply