1

I'm trying to use some fortran 90 subroutines in a c++ program. I have a function writio without any arguments that works fine, but my function twice doesn't do what it should. This is my first fortran function so the problem is probably somewhere in the file rocker.f90:

MODULE rocker

    CONTAINS

    SUBROUTINE writio
        WRITE(*,*) "writings"
    END SUBROUTINE writio

    SUBROUTINE twice(number, double)
        REAL*8, INTENT(IN) :: number
        REAL*8, INTENT(OUT) :: double
        WRITE(*,*) 2.0*number
        double=2.0*number;
    END SUBROUTINE twice

END MODULE rocker

This one I compile using f95 -c rocker.f90. Then I want to use the subroutines in the c++ program testcf.cpp:

using namespace std;
#include <cstdlib> 
#include <iostream>    
#include <iomanip>  
#include <sstream>
#include <fstream>
#include <stdio.h>    
#include <time.h>
#include <string.h>

extern "C" {
    extern void __rocker_MOD_writio();
    extern void __rocker_MOD_twice(double d1, double d2);
}

int main()  {

    __rocker_MOD_writio();
    double d1=3.5, d2;
    __rocker_MOD_twice(d1, d2);
    cout << d1 << "   " << d2 << endl;

    return 0;
}

I compile this using g++ testcf.cpp rocker.o -lgfortran -o testcf, and when running I get the following output:

 writings
  -3.59651870E+36

and then the program freezes. So it seems maybe the variable hasn't been passed correctly from c++ to f90?

EDIT: It seems the problem is not (only) in the Fortran code, because I can run it from the fortran program runrocker.f90:

PROGRAM runrocker
    USE rocker, ONLY: writio, twice
    IMPLICIT NONE 

    REAL*8 :: d1=3.5, d2=0.0    

    CALL writio()
    CALL twice(d1, d2)
    WRITE(*,*) d1, "  ", d2


END PROGRAM runrocker

1 Answer 1

1

Since your second parameter to twice is OUT, you probably want a reference:

 extern void __rocker_MOD_twice(double d1, double& d2);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I just noticed that in the tutorial I was using too, should read more closely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.