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

c++ calling fortran dll linker error

pradeepnatekar
Beginner
492 Views

I am using MS VS 6.0 for a c++ application which need to call a dll built with IVF (Version 9.0.2945.2003). The code which I have in C++ and Fortran is given below.

C++:

#include "stdafx.h"
#include
extern "C"
{
void __cdecl TestCode();
}
int main(int argc, char* argv[])
{
TestCode();
return 0;
}

FORTRAN:

subroutine TestCode

!DEC$ ATTRIBUTES DLLEXPORT::TestCode
!DEC$ ATTRIBUTES C, ALIAS: 'TestCode':: TestCode

call MainExp()
end subroutine TestCode

The TestCode.dll and .lib are in path of the C++ exe. Building the project gives following error.

Test.obj : error LNK2001: unresolved external symbol _TestCode
Debug/Test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Do I need to change something in vc++ project settings to get it right. Please help.

Regards,

Pradeep

0 Kudos
6 Replies
Steven_L_Intel1
Employee
492 Views
Either add the leading underscore in the ALIAS attribute or, preferably, add the DECORATE attribute as well.
0 Kudos
pradeepnatekar
Beginner
492 Views

Steve

I have made the changes as follows:

subroutine

TestCode

!DEC$ ATTRIBUTES DLLEXPORT:: TestCode

!DEC$ ATTRIBUTES C, DECORATE, ALIAS: '_TestCode':: TestCode

!call MainExp()

print*, "Say Hello from Fortran"

end subroutine

TestCode

Still the linker gives error.

0 Kudos
Steven_L_Intel1
Employee
492 Views
I said "either". Don't do both. Either add the underscore or add DECORATE, but both together will give you a symbol with two leading underscores.
0 Kudos
pradeepnatekar
Beginner
492 Views
Steve I tried both. Its not working. Do I need to change something in c++ project settings?
0 Kudos
Steven_L_Intel1
Employee
492 Views
Are you linking the export library (.lib) created by the Fortran project in the C++ project?

Be sure that you do not do BOTH of the things I mentioned. Use DECORATE and have the ALIAS omit the underscore.
0 Kudos
pradeepnatekar
Beginner
492 Views

Steve,

IusedDECORATE and ALIAS without the leading underscore. The only thing I forgot to do was to include the .lib in c++ project. Now I have added .lib to roject and kept .lib and .dll in path of c++ exe. It works fine now. Thanks for your help. Final code is as below:

---------------------Fortran Code--------------------

subroutine

TestCode

!DEC$ ATTRIBUTES DLLEXPORT:: TestCode

!DEC$ ATTRIBUTES C, DECORATE, ALIAS:'TestCode':: TestCode

print*, "Say Hello from Fortran"

end subroutine

TestCode

#include

"stdafx.h"

------------------C++ code -------------------

extern

"C"

{

void __cdecl TestCode();

}

int

main(int argc, char* argv[])

{

TestCode();

return 0;

}

0 Kudos
Reply