Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    5

    Smile 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:
    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;
    }
    And my fortran's code is:
    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 compiled with GNU compiler like this:
    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
    I appreciate if someone could give me a hand.

    Thanks a lot.

    Taku.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,459
    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!

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    5
    Thanks for the tip. I tried it but this dont solve the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •