Need help compiling mixed C++ / Fortran code with MinGW
Author |
Message |
FileFantasy
|
Posted: 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) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
btiffin
|
|
|
|
|
FileFantasy
|
Posted: 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
code: | #include <iostream>
using namespace std;
extern "C"
{
int sub1_();
}
main()
{
cout << "main" << "\n";
sub1_();
} |
sub1.f
code: | integer function sub1()
sub1=0
write (6,*) 'sub1'
return
end
|
Using MinGW in cmd.exe, I used this command in the directory that contains both files:
code: |
g77 main.cpp sub1.f -o prog.exe -lstdc++
|
It compiled, and running prog.exe gave:
Source: http://gcc.gnu.org/ml/gcc-help/2000-03/msg00027.html |
|
|
|
|
|
FileFantasy
|
Posted: 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
code: |
#include <iostream>
using namespace std;
extern "C"
{
int add_(int& mrow, int& ncol, double** mat);
}
int main()
{
int i, j, mrow = 5, ncol = 4;
double mat[mrow][ncol];
for (i = 0; i < mrow; i++)
{
for (j = 0; j < ncol; j++)
{
mat[i][j] = i*10+j;
cout<<mat[i][j]<<" ";
}
cout << endl;
}
add_(mrow, ncol, mat);
return 0;
}
|
add.f
code: |
subroutine add(mrow, ncol, mat)
integer ncol
integer mrow
double precision mat(ncol, mrow)
do 20 I=1, ncol
do 20 J=1, mrow
20 write (6,*) mat(I,J)
return
end
|
And it does not compile, claiming:
code: |
main.cpp: In function `int main()':
main.cpp:22: error: cannot convert `double (*)[((unsigned int)((int)ncol))]' to `double**' for argument `3' to `int add_(int&, int&, double**)'
|
I suspect 2D arrays can't be passed to Fortran like this?
If so, how do I go by passing 2d arrays into Fortran? |
|
|
|
|
|
btiffin
|
Posted: 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 |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: 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... |
|
|
|
|
|
FileFantasy
|
Posted: 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. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
FileFantasy
|
Posted: 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. |
|
|
|
|
|
hatef
|
Posted: 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:) |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: 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? |
|
|
|
|
|
hatef
|
Posted: 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 |
|
|
|
|
|
alex.john95
|
Posted: 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 |
|
|
|
|
|
|
|