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

Calling C functions from Fortran code (Visual Studio 2005)

Vladimir_Baros
Beginner
624 Views
I'm trying to call C function from Fortran code. I'm using Intel Fortran compiler version 9.1 intergrated in Visual studio 2005.
Here are the steps:
1. Created new solution with some Fortran code
2. Added new project to existing solution. It's a win32 static library.
3. Made Fortran project dependent on win32 static library as it's suggested in Intel Fortran compiler. It is now automaticly linked to Fortran program.

Now I have this error:

Link: executing 'link'
proba.obj : error LNK2019: unresolved external symbol _CADDIJ referenced in function _MAIN__
Debug/Fortran_Console2.exe : fatal error LNK1120: 1 unresolved externals

Now I guess the problem is that fortran program can't find external c function by it's name.
Please help!
0 Kudos
6 Replies
Steven_L_Intel1
Employee
624 Views

Is the C function named CADDIJ with that exact case? It needs to be unless you want to add directives to the Fortran code to change the name. Please read the chapter on mixed-language programming in the "Building Applications" book of the Intel Fortran on-disk documentation.

0 Kudos
Vladimir_Baros
Beginner
624 Views

Idon't know what can be the problem.
I'm attaching a small project. If anyone can help I'll appreciate it very much.

0 Kudos
Steven_L_Intel1
Employee
624 Views

You have two problems. One is that you spelled the routine "caddij" in the C routine but Fortran upcases names by default. The other is that you added __stdcall, which is not compatible with Intel Fortran defaults. Try this:

voidCADDIJ (int *i, int *j, int *k)

0 Kudos
Vladimir_Baros
Beginner
624 Views

I did that, and now I get these errors:

Warning1 warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specificationFortran_Console2

Error2 error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)Fortran_Console2

Error3 error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)Fortran_Console2

Warning4 warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:libraryFortran_Console2

Error5 fatal error LNK1169: one or more multiply defined symbols foundFortran_Console2

0 Kudos
Steven_L_Intel1
Employee
624 Views
You need to set the run-time library type to the same selection for the C++ and Fortran projects. In C++ it's under "Code Generation", in Fortran under "Libraries". I suggest selecting "Multithreaded DLL" for both.
0 Kudos
Vladimir_Baros
Beginner
624 Views

Thanks, that did the trick finaly but only for debug version.
For Release version it just keep linking the files until all my virtual ram is consumed.

I hope that this thread will help anyone starting with Fortran and Visual Studio.

0 Kudos
Reply