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

Problems from Debug to Release Mode

Ashley_Weinstein
Beginner
475 Views

In summary, I have a C++ *.exe application which depends on a Fortran *.lib and a C++ *.lib that are compiled in the same solution as the *.exe. 

The code was working fine stepping through in Debug mode, however when I went to port to a Release mode version all heck broke loose.

The first problem I encountered was "error LNK2001: unresolved external symbol _WinMain@16    MSVCRT.lib".

I resolved this by adding the following code to the main *.cpp:

#ifdef CONSOLE
#pragma comment(linker, "/subsystem:console")
#else
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

This got me through compilation and linkage and created an *.exe file. Then upon execution I encountered an unhandled exception in the code.

I don't understand why the code worked fine stepping through in Debug then fails in Release. I checked all the options and when the code was working in Debug the subsystem was even set to Console. What's going on here? I know that all of the variables are mapped correctly across the C++/Fortran interfaces. I verified program values and results obtained using the interface in Debug versus a standalone Fortran project, so the calculations are working properly.

For reference I've attached the main blocks of three projects in the solution:

Cpp *.exe

#include "stdafx.h"
#pragma comment(lib,"CppCode.lib")
#pragma comment(lib,"FortranCode.lib")
#ifdef CONSOLE
#pragma comment(linker, "/subsystem:console")
#else
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

extern "C" {
    void FORLINK(double *in, double *figin, double *out);
}

int main(int argc, char* argv[])
{
    ....

}

Fortran *.lib

SUBROUTINE FORLINK(in, figin, out)

....

    intA=CPPEX(ab,cd)

....

END SUBROUTINE

Cpp *.lib

extern "C" int CPPEX(int ab, char *cd)
{

...

    return intB;

}

0 Kudos
4 Replies
IanH
Honored Contributor II
475 Views
Could you please post the C++ and fortran compile options that you use for your release configuration for the three projects. It sounds like you have something inconsistent there. You shouldn't need to add the code that you've added to the C++ main program if you have things set up right. Your code snippets are also missing the declarations for the dummy arguments for FORLINK, the actual arguments for CPPEX and the declaration of CPPEX. For what its worth, if you are calling fortran from C (or vice versa) I strongly recommend that you use Fortran 2003's C interoperability feature. Pop a BIND(C,NAME='FORLINK') clause on the SUBROUTINE statement for FORLINK, and on the INTERFACE block for CPPEX (if you don't have an interface block for CPPEX in your Fortran code, then get one!)
0 Kudos
Ashley_Weinstein
Beginner
475 Views
Hi Ian, I reduced down the code snippets for the post. All actual and dummy arguments are declared properly. I am using Fortran 2003's C interoperability BIND C feature on the INTERFACE block for CPPEX and for one of the variables passed to FORLINK from the main C++ program. I've checked all the variables and they are porting fine in Debug mode. In Release mode however, the values are all screwy across the interface. I'll post the C++ and Fortran compile options shortly.
0 Kudos
Ashley_Weinstein
Beginner
475 Views
Eventually I want to compile the Fortran subroutine (and its C++ "sub"-subroutine) into a DLL which the main C++ *.exe will call. At the moment I was simply testing the link by compiling the Fortran subroutine (and its C++ "sub"-subroutine) into a static LIB. Will the BIND C interoperability work OK with a call from C++ to a Fortran DLL?
0 Kudos
IanH
Honored Contributor II
475 Views
Yes.
0 Kudos
Reply