Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Strange behavior when compiling C++ for Fortran
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jomarin




PostPosted: Fri Dec 05, 2008 8:09 am   Post subject: Strange behavior when compiling C++ for Fortran

Hi everybody,

I need to call a C++ routine from a Fortran code. After some research on the internet, I found a solution using g++ and g77.
The problem is that I get some strange behavior, concerning the memory managment. The code of the small example is attached to this topic. Here is the contents :

file testmac.f :
code:

C
      INTEGER*4 sum
      INTEGER*4 im0,im1, im2, im3, im4, im5
     
C
      im0=41
      im1=42
      im2=43
      im3=44
      im4=45
      im5=46
     
      WRITE(*,*) im0,im1,im2,im3,im4,im5
     
      CALL MAC(im0,im1,im2,im3,im4,im5)
     
C
      sum=im0+im1+im2+im3+im4+im5
     
      WRITE(*,*) sum
           
      WRITE(*,*) im0,im1,im2,im3,im4,im5
      WRITE(*,*) sum

C
      STOP
      END


File mac.cpp
code:

#include "mac.h"

#include <iostream>

using namespace std;

// ##################################################################
// Getting MAC Address via popen() within C++-program in Windows 2000
// ##################################################################


#define MAC_ADDRESS_SIZE 17



// =====================================
void __stdcall mac_(int *v1,int *v2,int *v3,int *v4,int *v5,int *v6)
{   

  cout << *v1 << " " << *v2  << " " << *v3  << " " << *v4  << " " << *v5  << " " << *v6 << endl;
 
  *v1=1;
  *v2=2;
  *v3=3;
  *v4=4;
  *v5=5;
  *v6=6;
 
  cout << *v1 << " " << *v2  << " " << *v3  << " " << *v4  << " " << *v5  << " " << *v6 << endl;




File mac.h
code:

#ifndef _MAC_H
#define _MAC_H


extern "C"
      {
        void __stdcall mac_(int *,int *,int *,int *,int *,int *);
      }


#endif


To compile, I use :
g77 -g -c testmac.f
g++ -g -c mac.cpp
g77 mac.o testmac.o -o testmac.exe -lstdc++

I get following warning message :
Warning: resolving _mac_ by linking to _mac_@24
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups


And now the output with my comments :
41 42 43 44 45 46 // OK printing integer values in the fortran
41 42 43 44 45 46 // OK printing parameters values in the C++ function
1 2 3 4 5 6 // OK variables correctly set in the C++ function
21 // OK the sum is correct (with ddd I checked that the variable values are correct)
1 2 3 4 2289728 4199088 // WRONG, the two last variables have been overwritten
21 // OK the sum variable did'nt change

In fact, using ddd, it seems that the WRITE(*,*) sum command modifies variables im4 and im5.
That is the to last that I declared (I changed the declaration order and the problem is always on the two last declared variables).

I'm using g++ and g77 on cygwin.
I have no idea what I am doing wrong.

Thanks in advance,

Joel.



simpleBug.tar
 Description:

Download
 Filename:  simpleBug.tar
 Filesize:  1.08 MB
 Downloaded:  110 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
btiffin




PostPosted: Fri Dec 05, 2008 6:35 pm   Post subject: Re: Strange behavior when compiling C++ for Fortran

jomarin;

I had to remove the two occurrences of the __stdcall prototype modifiers, but everything worked.

So I can only guess that you are not doing anything wrong...
code:

~/lang/g77$ g++ -g -c mac.cpp
~/lang/g77$ gfortran -g -c testmac.f
~/lang/g77$ gfortran -o testmac testmac.o mac.o -lstdc++
~/lang/g77$ ./testmac
          41          42          43          44          45          46
41 42 43 44 45 46
1 2 3 4 5 6
          21
           1           2           3           4           5           6
          21
~/lang/g77$ gfortran --version
GNU Fortran (Debian 4.3.2-1) 4.3.2
Copyright (C) 2008 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING

~/lang/g77$ g++ --version
g++ (Debian 4.3.2-1) 4.3.2
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

~/lang/g77$ uname -a
Linux home 2.6.26-1-686 #1 SMP Sat Nov 8 19:00:26 UTC 2008 i686 GNU/Linux

Cheers
OneOffDriveByPoster




PostPosted: Sun Dec 07, 2008 5:49 pm   Post subject: Re: Strange behavior when compiling C++ for Fortran

btiffin @ Fri Dec 05, 2008 6:35 pm wrote:
I had to remove the two occurrences of the __stdcall prototype modifiers, but everything worked.

So I can only guess that you are not doing anything wrong...
Actually, having the __stdcall seems to be what the OP is doing wrong. It works fine on Cygwin if you take the __stdcall out.
jomarin




PostPosted: Mon Dec 08, 2008 3:01 am   Post subject: Re: Strange behavior when compiling C++ for Fortran

Thanks a lot for your answers.

I deleted the __stdcall and it works fine now, with no more warning message at compilation Very Happy .

However, It is a little bit strange because all sites concening linking C++ from fortran I have seen indicated to add __stdcall in the declaration ...

For example :

http://www.physiology.wisc.edu/comp/docs/notes/not017.html#ccall

Joel.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: