混合的C ++和Fortran链接问题 [英] Mixed C++ and Fortran Linking Issue

查看:198
本文介绍了混合的C ++和Fortran链接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上做了一些搜索,但是我找不到如何从linux编译一个简单的C ++和Fortran代码。我需要复杂一些,但我只需要知道如何从一个简单的例子开始。



我的C ++代码是这样的:

  #include< iostream> ; 
使用namespace std;

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

int main()
{
int a,b,c;
cout<< 输入2个值:;
cin>> a>> b;

c = Add(& a,& b);
cout<< a<< +<< b< =<< c< ENDL;
c =乘以(& a,& b);
cout<< a<< *<< b< =<< c< ENDL;
返回0;
}

我的Fortran代码是这样的:

 整数函数Add(a,b)
整数a,b
Add = a + b
返回
结束


整数函数乘法(a,b)
整数a,b
乘法= a * b
返回
结束

我使用 ifort 来编译我的Fortran代码和g ++ C ++代码。我试过这个终端命令:

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

但是我收到的错误说链接器输入文件未使用,因为链接未完成。
我不知道如何将两者联系在一起。如果有人可以请帮助我,我将不胜感激!



PS - 我已经尝试添加 -lg2c at我的编译行结束,并且它不被识别。

解决方案

有几个问题在这里不要让名字对象匹配。首先,在C ++代码中指定外部函数具有C签名:



在test.cpp中:

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

请参阅在C ++源代码中,更多详细信息,externC的作用是什么?



在Fortran代码中,通过在模块中放置过程来显式化接口,并使用 iso_c_binding 让Fortran对象显示为有效的C对象。请注意,我们可以明确指定C或C ++程序通过 bind 关键字看到的对象的名称:



test_f.f90:

 模块mymod 
使用iso_c_binding
隐式无

包含

整数(kind = c_int)函数Add(a,b)bind(c,name ='Add')
整数(kind = c_int):: a,b
Add = a + b
结束函数

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

endmodule mymod


<$>

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

链接:

  $ icpc test_f.o test.o 

执行 a.out 现在可以按预期工作。


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.

这篇关于混合的C ++和Fortran链接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆