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

calling fortran routines in C++ program

dbiswas
Beginner
1,172 Views
I have created a static library using Intel's fortran compiler (ifl) and the xlib executables. In my C++ header file

-----------------------
extern "C" void _stdcall DSLUCS_(int & N, double *B, double *X, int & NELT,
int * IA, int * JA, double * A, int & ISYM, int & ITOL, double & TOL,
int & ITMAX, int & ITER, double & ERR, int & IERR, int & IUNIT, double *RWORK, int & LENW,
int *IWORK, int & LENIW);
--------------------------------
and in the cpp file I use

--------------
DSLUCS_(N,B,X,NELT,IA,JA,A,ISYM,ITOL,TOL,ITMAX,ITER,ERR,
IERR,IUNIT,RWORK,LENW,IWORK,LENIW);
---------------

I keep on getting the following error
------
rror LNK2001: unresolved external symbol _DSLUCS_@76
Release/Simulator.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
------------------

I have tried taking out the underscore from both cpp and header file and some other experimentation with upper and lower cases. I consistently get linking errors.

Please help.

Deepankar
0 Kudos
9 Replies
TimP
Honored Contributor III
1,172 Views
The default calling interface for IFL is cdecl, which you might consider using, if you have no strong preference. There is an option for compatibility with the default CVF interface, which is closer to what you are trying to use. I would expect to have to examine both .obj files with dumpbin in an attempt to sort out such a complex interface.
0 Kudos
dbiswas
Beginner
1,172 Views
Sorry. I tried using cdecl instead of stdcall it is still the same problem. Somwhow it cannto recognize the subroutine in Fortran library.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,172 Views
Isn't default name exporting in IFL in lowercase?

In any case, you can find out what's the actual symbol decoration by using:
dumpbin /symbols whatever.lib > output.txt
and examining contents of output.txt.

Jugoslav
0 Kudos
dbiswas
Beginner
1,172 Views
Thanks for the tip. After using dumpbin utility at least the C++ code can see the correct symbol in the library (no trailing underscores). However, I get the following linking errors. Seems like there is a conflict with
MSVCRT.lib. How do I turn this off or bypass. Thanks for your help.

Linking...
LIBC.lib(crt0dat.obj) : error LNK2005: _exit already defined in MSVCRT.lib(MSVCRT.dll)
LIBC.lib(crt0dat.obj) : error LNK2005: __exit already defined in MSVCRT.lib(MSVCRT.dll)
LIBC.lib(crt0.obj) : error LNK2005: _mainCRTStartup already defined in MSVCRT.lib(crtexe.obj)
LIBC.lib(crt0init.obj) : error LNK2005: ___xc_z already defined in MSVCRT.lib(cinitexe.obj)
LIBC.lib(crt0init.obj) : error LNK2005: ___xc_a already defined in MSVCRT.lib(cinitexe.obj)
LIBC.lib(crt0init.obj) : error LNK2005: ___xi_z already defined in MSVCRT.lib(cinitexe.obj)
LIBC.lib(crt0init.obj) : error LNK2005: ___xi_a already defined in MSVCRT.lib(cinitexe.obj)
LIBC.lib(winxfltr.obj) : error LNK2005: __XcptFilter already defined in MSVCRT.lib(MSVCRT.dll)
LINK : warning LNK4098: defaultlib "MSVCRT" conflicts with use of other libs; use /NODEFAULTLIB:library
LINK : warning LNK4098: defaultlib "LIBC" conflicts with use of other libs; use /NODEFAULTLIB:library
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,172 Views
This is known as multiple-C library syndrome. The cure is that you ensure that Fortran and C++ libraries on appropriate tabs of Project Settings are the same (e.g. "Single-threaded" for Release, "Single-threaded Debug" for Debug). In the special case of static library, it's better to ensure that the Fortran .lib does not contain hints to the linker about which run-time library to use -- check "Disable default library search rules" (/libdir:noauto) on Project/Settings/Fortran/Libraries/Other library options.

Jugoslav
0 Kudos
dbiswas
Beginner
1,172 Views
Thanks. In fact, i do not have Fortran settings inside my C++ IDE. However, I copiled my fortran sourse using ifl /MD option and then everything seems to be working fine. Somehow, all of this works only in the release version. The debug vresion stills complains that it cannto find _DSLUC (first error) even ater I set the project/settigs to debug-Multithreaded.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,172 Views
Oh, sorry, I already forgot it was IFL -- it was CVF solution :">. Check out the documentation -- in CVF, the switch is /libdir:noauto, and in VC++ it's /Zi; don't know about IFL. /MD will work, but if you decide to change run-time library in C++, you will have to rebuild Fortran sources as well.

However, the two problems shouldn't be related. Are you sure you rebuilt everything properly? Perhaps you forgot to add the Fortran .lib to linker settings for Debug configuration in VC++ (oh I hate VS Project Settings dialog)?

Jugoslav
0 Kudos
dbiswas
Beginner
1,172 Views
Dude you are genious. That was the problem. I had to add the library in the project->settings. For now I'm happy and my digital baby is working fine. More later.
0 Kudos
rahzan
New Contributor I
1,172 Views
Jugoslav is NO genius, he is GOD!

Tim
0 Kudos
Reply