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

compiler directives with C++

cfann61
Beginner
718 Views

I'm trying to write a basic program in C++ that calls a routine and a function from Fortran. I found this example on the web ( http://www.neurophys.wisc.edu/comp/docs/notes/not017.html) and have been trying to duplicate it. I'm getting three errors when I try to build:

error: LNK2019: unresolved external symbol _Main__ referenced in function_main (coming from Fortran project)

error: LNK2019: unresolved external symbol _FF1@4 reference in function_main (coming from C++ project)

error: LNK2019: unresolved external symbol _FR1@8 reference in function_main (coming from C++ project)

My directives look like this for my subroutine:

!DEC$ ATTRIBUTES DLLEXPORT ::FR1

!DEC$ ATTRIBUTES STDCALL, ALIAS : '_FR1@8' :: FR1

And this for my function(which is in the subroutine)

!DEC$ ATTRIBUTES DLLEXPORT :: FF1

!DEC$ ATTRIBUTES STDCALL, ALIAS : '_FF1@4' :: FF1

C++ section looks like this:

extern "C" void _stdcall FR1(int*,int *);

extern "C" int _stdcall FF1(int *);

void main()

{

int n=10,nSq,nCube;

FR1(&n,&nSq);

nCube=FF1(&n);

}

Thanks

Chris

0 Kudos
1 Solution
Steven_L_Intel1
Employee
718 Views
I'm not really in agreement that STDCALL is obsolete - it is the ONLY method available for calls to the Win32 API and from VB, EXCEL, managed code, etc.

There is at least one problem you have in that the Fortran project is looking for a main program. This means that either you created the Fortran project as an executable application (Console, etc.) rather than static library, or you have run into a bug in older versions of Intel Visual Fortran where every source got linked. Most likely the former.

You want to create a static library project for the Fortran code and make it a "dependent" of the C++ project. (If you have VS2005, make sure it also has SP1 applied.) In the Fortran project, set the property Libraries > Disable default library search rules to "No". Under Tools > Options > Projects > VC++ Directories, add the path to the Fortran installation LIB folder to "Library Files".

If in fact you have done all of these but still get the Fortran code linked, in the Fortran project go to the Fortran > Command Line property page and add /c to the Additional Options text.

View solution in original post

0 Kudos
3 Replies
TimP
Honored Contributor III
718 Views
As stdcall is an obsolete ABI, support for it is not available in all Fortran compilers, nor is there a compatible default in those that have supported it in the last 5 years. I suspect you haven't attempted to turn it on on the Fortran side, nor would I recommend you try it. You don't show any motivation for avoiding Fortran 2003 C Interoperability method. If, for some reason, you don't want to use ISO C Interoperability, you still ought to avoid stdcall.
0 Kudos
Steven_L_Intel1
Employee
719 Views
I'm not really in agreement that STDCALL is obsolete - it is the ONLY method available for calls to the Win32 API and from VB, EXCEL, managed code, etc.

There is at least one problem you have in that the Fortran project is looking for a main program. This means that either you created the Fortran project as an executable application (Console, etc.) rather than static library, or you have run into a bug in older versions of Intel Visual Fortran where every source got linked. Most likely the former.

You want to create a static library project for the Fortran code and make it a "dependent" of the C++ project. (If you have VS2005, make sure it also has SP1 applied.) In the Fortran project, set the property Libraries > Disable default library search rules to "No". Under Tools > Options > Projects > VC++ Directories, add the path to the Fortran installation LIB folder to "Library Files".

If in fact you have done all of these but still get the Fortran code linked, in the Fortran project go to the Fortran > Command Line property page and add /c to the Additional Options text.
0 Kudos
cfann61
Beginner
718 Views

Thanks.

I was initializing everything as console applications. Working fine now.

0 Kudos
Reply