Fortran Coder

查看: 6661|回复: 3

[混编] Call Fortran subroutine from C encounter“Undefined ... ”

[复制链接]

16

帖子

4

主题

0

精华

入门

F 币
106 元
贡献
59 点
发表于 2016-11-25 00:59:02 | 显示全部楼层 |阅读模式
I just want to test mixed programming with C/F, but i failed.
F subroutine subf.f90
[Fortran] 纯文本查看 复制代码
Integer Function Add(A,B)  Bind(C,Name="Add")!//要返回的话,请用 Function
  use , Intrinsic :: ISO_C_Binding
  implicit none
  integer , value :: A , B !/c#默认是传值,而fortran默认传址。因此要用value修饰
  Add = a + b !// Add(函数名)用于返回,而不能写 return a+b
end Function Add

C main test.cpp
[C] 纯文本查看 复制代码
#include <stdio.h>
extern "C" int Add(int A,int B);
int main()
{
      int c = Add(1,2);
      printf("c=%d\n",c);
      return 0;
}

makfile
[Make] 纯文本查看 复制代码
test: test.o subf.o
	icc -O2 -o test *.o
test.o:
	icc -c test.c
subf.o:
	ifort -c subf.f90

ERR
[Make] 纯文本查看 复制代码
test.o: In function `main':
test.cpp:(.text+0x35): undefined reference to `Add(int, int)'
Makefile:5: recipe for target 'test' failed


where did it all go wrong?

1948

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1298 元
贡献
547 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

发表于 2016-11-25 09:34:37 | 显示全部楼层
我没有装 icc ,也没有用过 icc
我试着用 gfortran 和 gcc 编译,是一切 OK 的。

我想,你是不是应该用 icpc 来代替 icc ?如果你的扩展名是 test.cpp 的话。
I did not install ICC , eh , I've never used it...
I tried to compile with gfortran and GCC , all OK.
I think you should use ICPC instead of ICC? If your extension is test.cpp.

QQ截图20161125093340.png

16

帖子

4

主题

0

精华

入门

F 币
106 元
贡献
59 点
 楼主| 发表于 2016-11-26 01:10:22 | 显示全部楼层
谢谢群主!一直没来得及回复,问题解决了。早上醒来 重新跑了一下就可以了。。。

16

帖子

4

主题

0

精华

入门

F 币
106 元
贡献
59 点
 楼主| 发表于 2016-11-26 01:10:59 | 显示全部楼层
fcode 发表于 2016-11-25 09:34
我没有装 icc ,也没有用过 icc
我试着用 gfortran 和 gcc 编译,是一切 OK 的。

icc/icpc 都是可以的。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-3-29 10:04

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表