Calling C++ with iostream causes access violation

Calling C++ with iostream causes access violation

Calling a C++ routine that uses iostream causes the Fortran main program
to generate an access violation at the first write statement to unit 6.
The Fortran code:

      program TestFtn 
      interface 
        integer*4 function c_routine [c,alias:'_c_routine']( ) 
        end function c_routine 
      end interface 
      write( 6, * ) 'Fortran Test' 
      ireturn = c_routine( ) 
      stop 
      end 

The C++ code:

 
#include  
extern "C" 
{ 
int c_routine( ) 
{ 
    cout << "In c_routine" << end; 
    return  0; 
}; 
} 

On the Link tab under Project Settings, I checked "Ignore all default
libraries" and included the following libraries at the beginning of
the Object/library modules line:
dfor.lib dfqwin.lib dfqw_sdi.lib libcmtd.lib libcid.lib libcpd.lib

Can anyone shed any light on the matter ?

Thanks - Eric

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Let me see if I understand this - you have created a QuickWin application and are trying to use C++ iostream as well? Where do you want the iostream output to go?

Does this work in a console application?

In general, you want to avoid having more than one language do I/O to the same device (the "console" in this case.)

Steve

Steve - Intel Developer Support

I created a Fortran Console Application. The C++ output should go to the console.

You may have "created" a console application, but you overrode that with your choice of QuickWin libraries. May I suggest un-checking the "ignore all default libraries" option and removing all of the libraries you added? Since you're apparently building a debug application, check the Use Debug C Libraries box. Also, check Use Multithreaded Libraries.

I suspect it is the mismatch of libraries that results in the access violation.

Steve

Steve - Intel Developer Support

I created a Fortran Console Application. The C++ output should go to the console.

O.K. That works for using iostream. The reason I had to check "Ignore..."
and use the libraries was to overcome the link error LNK2005: __matherr
already defined in LIBCMTD.lib(matherr.obj). Now if I include math.h and
use a math function, I get the link error again. Can I get my cake and
eat it too ?
C++ code:
#include
#include
extern "C"
{
int c_routine( )
{
double a = 5;
int n = (int)fabs( a );
cout << "In c_routine n= " << n << endl;
return n;
};
}

Thanks again - Eric

Ignore Libraries is the wrong solution to this problem - yes, I know the linker suggests it.

Instead, add dfor.lib as the first library to search on the Linker settings tab.

Steve

Steve - Intel Developer Support

Leave a Comment

Please sign in to add a comment. Not a member? Join today