2

我正在尝试编译调用 fortran 子例程的 ac 代码,但总是出错。

这是fortran代码:

!fort_sub.f90
module myadd
use iso_c_binding
implicit none
contains

subroutine add1(a) bind(c)
implicit none
integer (c_int),intent (inout) :: a
a=a+1

if(a>10) then
   stop
endif
end subroutine add1
end module myadd

这是c代码

//main.cpp
extern "C"{ void add1(int * a); }

int main(void){
  int a=2;
  add1(&a);
  return 0;
}

当我编译它们时

ifort -c fort_subs.f90
icc main.cpp fort_subs.o

我收到错误

Undefined symbols for architecture x86_64:   "_for_stop_core", referenced from:
      _add1 in fort_subs.o ld: symbol(s) not found for architecture x86_64

当我编译它们时

icc -c main.cpp 
ifort -nofor-main fort_subs.f90 main.o

我收到错误

Undefined symbols for architecture x86_64:
  "___gxx_personality_v0", referenced from:
      Dwarf Exception Unwind Info (__eh_frame) in main.o
  "___intel_new_feature_proc_init", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64

那么为什么会出现这些错误以及如何解决它们呢?

我知道在 ibm 编译器中有一个选项“-lxlf90”,它告诉 c 编译器链接 fortran 库,这解决了“_for_stop_core”错误。intel c 编译器中是否有类似的选项?

4

1 回答 1

0

似乎 C 不喜欢 Fortran 的STOP命令。如果您想停止该程序,您可能需要考虑输入第二个值,例如

subroutine add1(a,kill) bind(c)
   integer (c_int), intent(inout) :: a, kill
   kill = 0
   a = a+1
   if(a > 10) kill=1
end subroutine

而在main.cpp,

//main.cpp
#include <stdio.h>
extern "C"{ void add1(int * a, int * kill); }

 int main(void){
  int a=20, kill;
  add1(&a, &kill);
  if(kill!=0) {
    printf("program halted due to a>10\n");
    return 0;
  }
  return 0;
}
于 2013-10-14T16:38:07.943 回答