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

Printing from Fortran wrapped in C++

Stephen_Painchaud
1,049 Views

My C++ main code is

extern "C" { void HELLO(); }

int main()
{
HELLO();
}

The Fortran library code is

SUBROUTINE hello()
PRINT *, 'hi'
RETURN
END

I get the link errors:

2>Linking...
2>libifcoremt.lib(for_diags_intel.obj) : error LNK2001: unresolved external symbol __iob
2>libifcoremt.lib(for_nt_open_proc.obj) : error LNK2001: unresolved external symbol __iob
2>libifcoremt.lib(for_init.obj) : error LNK2001: unresolved external symbol ___argv
2>libifcoremt.lib(for_init.obj) : error LNK2001: unresolved external symbol ___argc

I am compiling the Fortran first and ignoring LIBCMT.lib. I have the path to the Fortran libraries in my VC++ directories for the library files. If I remove the Fortran print statement everything compiles and runs.

Am I missing something or is there a bug?

0 Kudos
4 Replies
Jugoslav_Dujic
Valued Contributor II
1,049 Views
stephen.painchaud@bakerhughes.com:
I am compiling the Fortran first and ignoring LIBCMT.lib.

Don't do that then.

You're removing the very C/C++ run-time library which contains those symbols. Instead, you should make sure that you use compatible C/C++ and Fortran RTLs; please check the documentation page "Building Intel Fortran/C Mixed-Language Programs on Windows Systems" (you will easily find it e.g. by searching for "libifcoremt"). If your C++ is built with "Multithreaded" option (libcmt.lib), then you should link with libifcoremt.lib,

0 Kudos
Stephen_Painchaud
1,049 Views

I am trying to determine why my application won't link by creating simple test code that I can play with and post here. Unfortunately, that is not working well for me.

I was ignoring LIBCMT.lib because it seems to conflict with MSVCRT.lib, and the linker indicates I need to ignore something. So if I ignore MSVCRT.lib, I see that my example above will work. Unfortunately, in my real application, I get link errors with the atof function.

I will read the article you have suggested, and see if that helps me make sense of the conflicting libraries. Thank you.

0 Kudos
Stephen_Painchaud
1,049 Views

OMG, I guess I need to kiss your feet. Everthing works now!

In my application, and all my test examples, the C++ wrapper was set to multi-threaded DLL (by default), and the Fortran static library was set to multi-threaded. By setting them both to multi-threaded, everything compiles and runs, and I don't have to "ignore" anything.

These issues are all new to me, so I never would have suspected this conflict. I was just using the default projects that get created. Maybe the problem was that I created the C++ wrapper first, which defaults to MT DLL, then I added a static library Fortran project afterwards.

Thanks.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,049 Views
stephen.painchaud@bakerhughes.com:
These issues are all new to me, so I never would have suspected this conflict. I was just using the default projects that get created. Maybe the problem was that I created the C++ wrapper first, which defaults to MT DLL, then I added a static library Fortran project afterwards.


Actually, this is a FAQ here. The linker's hint ("defaultlib X conflicts with use of other libs; use /NODEFAULTLIB:library") to only adds up to the problem though. Basically, the problem is that you have libraries with same symbols defined -- but the cure is to ensure that you use the same one.

However, I'm not sure how you arrived there (that was more of a problem with Compaq Fortran). With Intel, the default setting for a static library project is /libdir:noauto (Properties/Fortran/Libraries/Disable default search rules), which basically means "don't reference any run-time library" (i.e. even if you set "multi-threaded" it will be basically ignored). When the .lib is then linked with a C++ exe, you will have to manually enter appropriate libif**.lib in the linker tab. Never mind, it's important that it works now.
0 Kudos
Reply