“__gfortran_pow_c8_i4"使用 g++ 从 g++ 和 gfortran 链接 .o 文件时出错

2022-01-14 00:00:00 fortran gfortran c++

我正在尝试链接使用 g++ 生成的 .o 文件和使用 gfortran 生成的另一个 .o 文件.

I am trying to link a .o file generated using g++ and another .o file generated using gfortran.

g++ -c mycppcode.cpp

生成文件 mycppcode.o 和命令

gfortran -c myfortrancode.f

生成文件myfortrancode.o

当我链接这两个文件以获得输出文件时

When I link these two files to get an output file

g++ -O mycppcode.o myfortrancode.o

我收到以下错误

Undefined symbols for architecture x86_64:
  "__gfortran_pow_c8_i4", referenced from:

有人可以帮我解决这个问题吗?我应该使用另一个编译器吗?另外,我想知道哪些函数或子例程调用__gfortran_pow_c8_i4",以便将来在fortran中尽量避免这些函数或子例程.

Could some one help me with this? Should I use another compiler? Also, I would like to know what functions or subroutines call "__gfortran_pow_c8_i4", so that I can try to avoid these functions or subroutines in fortran in future.

推荐答案

以下假设您使用的是 GNU 编译器工具.如果您使用其他编译器,情况可能会略有不同.

The following assumes you are using the GNU compiler tools. Things may be slightly different if you are using other compilers.

您可以使用任一编译器将两者链接在一起,但您需要提供相应的库.

You can use either compiler to link the two together, but you need to provide the appropriate libraries.

通常,您可以使用任一

gfortran fortobj.o cppobj.o -lstdc++

g++ fortobj.o cppobj.o -lgfortran

这假设您使用的设置是两个编译器都知道彼此的库(例如,如果您通过 linux 存储库安装).

This assumes that you are using a setup where both compilers know about each other's libraries (like if you installed through a linux repository).

对于 OP,C 编译器来自 XCode,而 gfortran 来自 homebrew.在这种情况下,gfortran 知道 g++ 库(因为它们用于编译编译器),但 g++ 不知道gfortran 库.这就是为什么使用 gfortran 链接可以像上面宣传的那样工作.但是,要与 g++ 链接,您需要在使用 -L 标志调用链接器时添加 libgfortran.* 的路径,例如

In the case of the OP the C compilers came from XCode and gfortran is from homebrew. In that case, gfortran knows about the g++ libraries (since they were used to compile the compiler), but g++ doesn't know about the gfortran libraries. This is why using gfortran to link worked as advertised above. However, to link with g++ you need to add the path to libgfortran.* when you call the linker using the -L flag, like

g++ fortobj.o cppobj.o -L/path/to/fortran/libs -lgfortran

<小时>

如果由于某种原因你的 gfortran 编译器不知道你的 g++ 库,你会这样做


If for some reason your gfortran compiler is unaware of your g++ libs, you would do

gfortran fortobj.o cppobj.o -L/path/to/c++/libs -lstdc++

<小时>

请注意,最终的可执行文件不应有任何差异.我不是编译器专家,但我的理解是,使用编译器将对象链接在一起可以方便地调用链接器(类 UNIX 操作系统上的 ld)以及与语言相关的适当库您正在使用.因此,只要包含正确的库,使用一个编译器或另一个编译器来链接应该无关紧要.


Note that there shouldn't be any difference in the final executable. I'm no compiler expert, but my understanding is that using the compiler to link your objects together is a convenience for calling the linker (ld on UNIX-like OS's) with the appropriate libraries associated with the language you are using. Therefore, using one compiler or the other to link shouldn't matter, as long as the right libraries are included.

相关文章