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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With