Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Smile c++ program calling a Fortran subroutine with a function in the arg.


    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:
    #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:
    	BRES = (XU-XF)/((BF(XU))*2)
    	END subroutine usefunc
    I compiled with GNU compiler like this:
    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.


  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    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
    Thanks for the tip. I tried it but this dont solve the problem.

  4. $spacer_open

Posting Permissions

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