Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

linker - Mixed C++ and Fortran Linking Issue

I have done some searching online but I cannot find out how to compile a simple C++ and Fortran code from linux. I need to get complex with it, but I just need to know how to start with a simple example.

My C++ code is this:

#include <iostream>
using namespace std;

extern int Add( int *, int * );
extern int Multiply( int *, int * );

int main()
{
    int a,b,c;  
    cout << "Enter 2 values: ";
    cin >> a >> b;

    c = Add(&a,&b);
    cout << a << " + " << b << " = " << c << endl;
    c = Multiply(&a,&b);
    cout << a << " * " << b << " = " << c << endl;
    return 0;
}

My Fortran Code is this:

integer function Add(a,b)
    integer a,b
    Add = a+b
    return
end


integer function Multiply(a,b)
    integer a,b
    Multiply = a*b
    return
end

I am using ifort to compile my Fortran code and g++ for C++ code. I have tried this terminal command:

$ ifort -c Program.f90
$ g++ -o Main.cpp Program.o

But the error I am getting says "linker input file unused because linking not done." I am not sure how to link the two together. If someone could please help me out I would greatly appreciate it!

PS - I have tried adding -lg2c at the end of my compilation line, and it is not recognized.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are few issues here that don't let names of the objects match. First, specify in the C++ code that the external functions have the C signature:

In test.cpp:

extern "C" int Add( int *, int * );
extern "C" int Multiply( int *, int * );

See In C++ source, what is the effect of extern "C"? for more details.

In your Fortran code, make the interface explicit by placing procedures in the module, and use iso_c_binding to let Fortran objects appear as valid C objects. Notice that we can explicitly specify the names of the objects that the C or C++ programs will see through the bind keyword:

test_f.f90:

module mymod
use iso_c_binding
implicit none

contains

integer(kind=c_int) function Add(a,b) bind(c,name='Add')
    integer(kind=c_int) :: a,b
    Add = a+b
end function

integer(kind=c_int) function Multiply(a,b) bind(c,name='Multiply')
    integer(kind=c_int) :: a,b
    Multiply = a*b
end function

endmodule mymod

Compile (don't mind me using the Intel suite, my g++ & gfortran are very old):

$ ifort -c test_f.f90 
$ icpc -c test.cpp 

Link:

$ icpc test_f.o test.o

Executing a.out should now work as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...