Zahur
Zahur

Reputation: 101

calling C++ function from fortran not C

is it possible to call a C++ function from FORTRAN such as

#include <iostream.h>
extern "C"
{
    void single_cell(void)
    {
        cout<<"Hi from C++";
    }
}

So when I am using C it is working fine but with the C++ function it gives errors like Undefined error to cout etc

Upvotes: 2

Views: 1565

Answers (2)

M. S. B.
M. S. B.

Reputation: 29381

Both g++ and gfortran, used as linkers, bring in extra libraries. That is why the Fortran/C++ combination is trickier than the Fortran/C combination ... just using the correct compiler as the linker won't work, you need to add a libary. Already suggested is to link with gfortran and specify the C++ runtime libraries. You can also link with g++ and specify the Fortran runtime libraries. See Linking fortran and c++ binaries using gcc for the details of both approaches.

Upvotes: 3

mah
mah

Reputation: 39797

Assuming you could have your Fortran code call into a C function, the problem is not the code but rather how you are linking. When you're linking C++ objects you need to also pull in the C++ runtime. If using GCC, link with the g++ command and it will pull in the parts you need.

Upvotes: 1

Related Questions