Results 1 to 3 of 3
Hello!.
This is my second post, and my first post was successful.
I have a new problem; i want to call a subroutine's fortran which have a function in the ...
- 03-09-2011 #1Just Joined!
- Join Date
- Jan 2011
- Posts
- 5
c++ program calling a Fortran subroutine with a function in the arg.
Hello!.
This is my second post, and my first post was successful.
I have a new problem; i want to call a subroutine's fortran which have a function in the argument and the compilation ran properly, but when i execute the program this shows me an "Segmentation fault".
This is my c++ program:
And my fortran's code is:Code://Main.cpp #include <stdio.h> #include <iostream> #include <cmath> #include <complex> using namespace std; struct Naa{ double x; void operator()(double xaux){ x = xaux*xaux; } }; extern"C" { void usefunc_(double *a, double *b, Naa *Func, double *CRES); } int main(){ double a = 0.0; Naa AA; double b = 1.0; double Func = 1; double CRES; usefunc_(&a, &b, &AA, &CRES); cout<<"The result is: "<< CRES <<endl; return 0; }
I compiled with GNU compiler like this:Code:!FunOFun.f90 SUBROUTINE usefunc(XL,XU,BF, BRES) IMPLICIT REAL *8 (A-B,D-H,O-Z) BRES = (XU-XF)/((BF(XU))*2) PRINT *,BRES END subroutine usefunc
I appreciate if someone could give me a hand.Code:g++ -O0 -g -c Main.cpp gfortran -O0 -g -c FunOFun.f90 -frecord-marker=8 g++ -O0 -g -o a.test FunOFun.o Main.o -frecord-marker=8 -lgfortran
Thanks a lot.
Taku.
- 03-10-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Remember, Fortran calling conventions pass arguments in reverse C/C++. So, in your "extern C" declaration, switch the function arguments back-to-front. It's been a donkey age since I had to integrate Fortran/F77 with C languages, but this is what I remember as the major caveat.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 03-10-2011 #3Just Joined!
- Join Date
- Jan 2011
- Posts
- 5
Thanks for the tip. I tried it but this dont solve the problem.


Reply With Quote