Problems from Debug to Release Mode

Problems from Debug to Release Mode

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;

}

5 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

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!)

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.

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?

Yes.

Laisser un commentaire

Veuillez ouvrir une session pour ajouter un commentaire. Pas encore membre ? Rejoignez-nous dès aujourd’hui