Computer Science Canada Need help compiling mixed C++ / Fortran code with MinGW |
Author: | FileFantasy [ Fri Jun 27, 2008 10:21 am ] |
Post subject: | Need help compiling mixed C++ / Fortran code with MinGW |
Hey there, I have a subroutine written in Fortran (sub.f), and my main () is in the C++ (main.cpp), calling the Fortran subroutine. There's no other files, only sub.f and main.cpp The code in C++ that calls the Fortran subroutine should be written correctly, I'm just wondering how to compile this program using MinGW to run it as an executable. If it is not possible with MinGW's g++ or g77, can someone direct me to a compiler that can accomplish this task? Thanks. --- Edit: Sorry forgot to add - this is on Vista (sadly), working in cmd.exe (MinGW is properly set up and g++ & g77 both work) |
Author: | btiffin [ Fri Jun 27, 2008 12:14 pm ] |
Post subject: | RE:Need help compiling mixed C++ / Fortran code with MinGW |
Haven't looked through it, but try http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html and see if you can glean any MinGW hints from it. Should match fairly closely. Cheers |
Author: | FileFantasy [ Fri Jun 27, 2008 1:29 pm ] | ||||||||
Post subject: | Mixed language programming using C++ and FORTRAN 77 | ||||||||
K I found an answer. Given: main.cpp
sub1.f
Using MinGW in cmd.exe, I used this command in the directory that contains both files:
It compiled, and running prog.exe gave:
Source: http://gcc.gnu.org/ml/gcc-help/2000-03/msg00027.html |
Author: | FileFantasy [ Fri Jun 27, 2008 2:55 pm ] | ||||||
Post subject: | RE:Need help compiling mixed C++ / Fortran code with MinGW | ||||||
A new problem arises: I tried the following: main.cpp
add.f
And it does not compile, claiming:
I suspect 2D arrays can't be passed to Fortran like this? If so, how do I go by passing 2d arrays into Fortran? |
Author: | btiffin [ Fri Jun 27, 2008 6:27 pm ] |
Post subject: | RE:Need help compiling mixed C++ / Fortran code with MinGW |
Try http://arnholm.org/software/cppf77/cppf77.htm#Section3.5.3 for some details. Aside from just getting the datatype specs right, you'll need to deal with the (usually code change requiring) row-major, column-major difference in array indexing. Cheers |
Author: | OneOffDriveByPoster [ Fri Jun 27, 2008 6:28 pm ] |
Post subject: | Re: Need help compiling mixed C++ / Fortran code with MinGW |
Your array should be passed as a double*. &mat[0][0] works. You will have to link with the Fortran, C and C++ libraries. I don't know too much Fortran though... |
Author: | FileFantasy [ Fri Jun 27, 2008 8:54 pm ] |
Post subject: | Re: RE:Need help compiling mixed C++ / Fortran code with MinGW |
btiffin @ Fri Jun 27, 2008 6:27 pm wrote: Try
http://arnholm.org/software/cppf77/cppf77.htm#Section3.5.3 for some details. Aside from just getting the datatype specs right, you'll need to deal with the (usually code change requiring) row-major, column-major difference in array indexing. Cheers My matrix will be built in C++, but it will be built such that it becomes properly ordered once it gets passed into Fortran. OneOffDriveByPoster @ Fri Jun 27, 2008 6:28 pm wrote: Your array should be passed as a double*.
&mat[0][0] works. You will have to link with the Fortran, C and C++ libraries. I don't know too much Fortran though... Hm, I don't understand why though, because I'm passing a 2d array... Please explain. |
Author: | OneOffDriveByPoster [ Fri Jun 27, 2008 9:38 pm ] |
Post subject: | Re: RE:Need help compiling mixed C++ / Fortran code with MinGW |
FileFantasy @ Fri Jun 27, 2008 8:54 pm wrote: Hm, I don't understand why though, because I'm passing a 2d array... Please explain. The 2-D array in your case is not stored as an array of pointers to array. The 2-D array you have is a n*m block of contiguous memory. Passing a pointer that has the address of the first element is a reasonable way for you to pass such an array. AFAIK, Fortran 77 has only this type of array. |
Author: | FileFantasy [ Wed Jul 02, 2008 9:27 am ] |
Post subject: | RE:Need help compiling mixed C++ / Fortran code with MinGW |
Oh I see what you mean now; basically passing an array over to Fortran, then have Fortran format it as a matrix. Thank you, it works wonderfully. |
Author: | hatef [ Wed Jul 09, 2008 10:51 am ] |
Post subject: | Re: Need help compiling mixed C++ / Fortran code with MinGW |
Hi, I just saw your thread regarding Mixed programing. I also need to call a Fortran program from within C++ and need to pass a 2-D array to Fortran program. I saw you told you were able to solve the problem. Could you please let me know how did you do that. What I can glean from the discussion is that : extern "C" { void FortranProgram_(double* Array, int* m, int* n); } and then in "int main()": int m=2; int n=3; double Array[m][n]; Fortran_program_(&Array[0][0], &m, &n); Is that what you do or I am wrong??? another question is that the FortranProgram I am calling itself calls some other Fortran subroutines and when I try with the above procedure it gives me an error like "the type of the actual argument differ from the type of the dummy argument " Any help in this regard will be greatly appreciated. Hatef:) |
Author: | OneOffDriveByPoster [ Fri Jul 11, 2008 8:40 pm ] |
Post subject: | Re: Need help compiling mixed C++ / Fortran code with MinGW |
Yes, we are passing it like &a[0][0]. Can you post some code? |
Author: | hatef [ Fri Jul 11, 2008 10:00 pm ] |
Post subject: | Re: Need help compiling mixed C++ / Fortran code with MinGW |
Hi, Thank you for your reply but the problem has been solved. yes that was the correct way but before giving the 2D array to the Fortran subroutine in C++ I changed the array a bit to get exactly what I want in Fortran. The Fortran stores Column major but C++ does row major. and then to get the Array back in C++ the reverse change must be applied. Regards, Hatef |
Author: | alex.john95 [ Wed Jul 30, 2008 2:11 pm ] |
Post subject: | RE:Need help compiling mixed C++ / Fortran code with MinGW |
I agree with the above quote Thanks |