尝试使用 C++ 代码调用 fortran 子例程时出现链接错误

问题描述 投票:0回答:0

我目前正在尝试在我的 C++ 程序中使用一些 fortran 子例程。然而,我之前从未尝试过,所以我写了一个简单的代码示例来尝试混合编程。

fortran代码(test_f.f90)如下:

subroutine PRINT_NUMBER()
    use iso_c_binding 
    implicit none

    write(*,*) "This is fortran answering!"
    
end subroutine PRINT_NUMBER

它只是做一个简单的打印过程。

C++代码(test_c.cpp)如下:

#include <iostream>

extern "C"
{
    void print_number_();
}

int main()
{
    std::cout << "This is C++ answering!" << std::endl;
    print_number_();

    return 0;
}

调用fortran代码文件中的子程序打印“This is fortran answering!”并打印“这是 C++ 回答!”本身。

我使用 gfortran(gcc 版本 12.1.0)编译 fortran 代码和 g++(Apple clang 版本 14.0.0)编译 C++ 代码:

gfotran -c test_f.f90
g++ -c test_c.cpp

我得到了两个目标文件 test_f.o 和 test_c.o

然后我尝试链接这两个目标文件:

g++ -o a.out test_f.o test_c.o

我收到错误信息如下:

ld: warning: object file (test_f.o) was built for newer macOS version (12.5) than being linked (12.0)
Undefined symbols for architecture x86_64:
  "__gfortran_st_write", referenced from:
      _print_number_ in test_f.o
  "__gfortran_st_write_done", referenced from:
      _print_number_ in test_f.o
  "__gfortran_transfer_character_write", referenced from:
      _print_number_ in test_f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我注意到 gfortran 库在这里不起作用,所以我使用以下方法找到 gfortran 库:

locate libgfortran

我得到了

/usr/local/Cellar/gcc/10.2.0/lib/gcc/10/libgfortran.5.dylib
/usr/local/Cellar/gcc/10.2.0/lib/gcc/10/libgfortran.a
/usr/local/Cellar/gcc/10.2.0/lib/gcc/10/libgfortran.dylib
/usr/local/Cellar/gcc/10.2.0/lib/gcc/10/libgfortran.spec
/usr/local/gfortran/lib/libgfortran.5.dylib
/usr/local/gfortran/lib/libgfortran.a
/usr/local/gfortran/lib/libgfortran.dylib
/usr/local/gfortran/lib/libgfortran.la
/usr/local/gfortran/lib/libgfortran.spec

我再次尝试使用静态库 libgfortran.a 链接对象:

g++ -o a.out /usr/local/gfortran/lib/libgfortran.a test_c.o test_f.o

除了一些“警告:目标文件 (/usr/local/gfortran/lib/libgfortran.a(...)) 是为更新的 macOS 版本 (12.3) 而不是链接 (12.0) 构建的”我得到的警告:

Undefined symbols for architecture x86_64:
  "___divtf3", referenced from:
      _determine_en_precision in libgfortran.a(write.o)
      _get_float_string in libgfortran.a(write.o)
  "___eqtf2", referenced from:
      _get_float_string in libgfortran.a(write.o)
  "___gttf2", referenced from:
      _determine_en_precision in libgfortran.a(write.o)
      _get_float_string in libgfortran.a(write.o)
  "___letf2", referenced from:
      _get_float_string in libgfortran.a(write.o)
  "___lttf2", referenced from:
      _determine_en_precision in libgfortran.a(write.o)
      _get_float_string in libgfortran.a(write.o)
  "___multf3", referenced from:
      _determine_en_precision in libgfortran.a(write.o)
      _get_float_string in libgfortran.a(write.o)
  "___subtf3", referenced from:
      _determine_en_precision in libgfortran.a(write.o)
      _get_float_string in libgfortran.a(write.o)
  "___unordtf2", referenced from:
      _determine_en_precision in libgfortran.a(write.o)
      _get_float_string in libgfortran.a(write.o)
  "_quadmath_snprintf", referenced from:
      _determine_en_precision in libgfortran.a(write.o)
      _get_float_string in libgfortran.a(write.o)
  "_strtoflt128", referenced from:
      __gfortrani_convert_real in libgfortran.a(read.o)
      __gfortrani_convert_infnan in libgfortran.a(read.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

还是不行。看来我需要更多的图书馆。我很困惑。

c++ fortran linker-errors hybrid
© www.soinside.com 2019 - 2024. All rights reserved.