Computer Science Canada Strange behavior when compiling C++ for Fortran |
Author: | jomarin [ 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 :
File mac.cpp
File mac.h
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. |
Author: | btiffin [ 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...
Cheers |
Author: | OneOffDriveByPoster [ 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.
Actually, having the __stdcall seems to be what the OP is doing wrong. It works fine on Cygwin if you take the __stdcall out.So I can only guess that you are not doing anything wrong... |
Author: | jomarin [ 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 ![]() 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. |