难以将 FFTW 库与 gfortran 链接

Difficulty in linking FFTW library with gfortran

提问人:user37292 提问时间:11/20/2022 最后编辑:user37292 更新时间:11/21/2022 访问量:313

问:

我有一个 c++ 程序,它使用 FFTW3 库,我想将其翻译成 Fortran。我正在使用 Ubuntu 22.04.1。

我安装了 FFTW3 库,详见在 UNIX 上安装 c++ 主程序名为 ksbenchmark.cpp,使用 g++ 编译它并将其链接(到 math 和 fftw3 库)

g++ -o my_executable.out ksbenchmark.cpp -lm -lfftw3

效果很好。

我已经在 Fortran 中翻译了 c++ 代码,源文件名为 ksbenchmrk.f90。

如果我发出

  gfortran ksbenchmark.f90 -lfftw3 -lm

我收到错误消息

/usr/bin/ld: /tmp/cca2xMGa.o: in function `ksintegrate_':
ksbenchmark.f90:(.text+0x59): undefined reference to `dfftw_plan_dft_1d_'
/usr/bin/ld: ksbenchmark.f90:(.text+0x94): undefined reference to `dfftw_plan_dft_1d_'
/usr/bin/ld: ksbenchmark.f90:(.text+0xcf): undefined reference to `dfftw_plan_dft_1d_'
/usr/bin/ld: ksbenchmark.f90:(.text+0x4de): undefined reference to `dfftw_execute_'
/usr/bin/ld: ksbenchmark.f90:(.text+0x5db): undefined reference to `dfftw_execute_'
/usr/bin/ld: ksbenchmark.f90:(.text+0x626): undefined reference to `dfftw_execute_'
/usr/bin/ld: ksbenchmark.f90:(.text+0x780): undefined reference to `dfftw_execute_'
/usr/bin/ld: ksbenchmark.f90:(.text+0xa1c): undefined reference to `dfftw_execute_'
collect2: error: ld returned 1 exit status

我花了一下午的时间阅读它,似乎我可能不得不使用 -I 和 -L 标志,例如

  gfortran test.f90 -L/new/path/to/lib -I/new/path/to/include -lfftw3 -lm

如帖子中所示,将 FFTW 与 gfortran 链接起来的问题(在架构 x86_64 中找不到符号)。

第一个问题是,使用什么路径?

我可以在 /usr/local/lib 和 /usr/local/include 中找到引用 fftw3 的文件,但尝试

   gfortran ksbenchmark.f90 -L/usr/local/lib -I/usr/local/include -lfftw3 -lm

返回与之前相同的错误

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
/usr/bin/ld: /tmp/ccW2DYGP.o: in function `ksintegrate_':
ksintegrate.f90:(.text+0x62): undefined reference to `dfftw_plan_dft_1d_'
/usr/bin/ld: ksintegrate.f90:(.text+0x9d): undefined reference to `dfftw_plan_dft_1d_'
/usr/bin/ld: ksintegrate.f90:(.text+0xd8): undefined reference to `dfftw_plan_dft_1d_'
/usr/bin/ld: ksintegrate.f90:(.text+0x4df): undefined reference to `dfftw_execute_'
/usr/bin/ld: ksintegrate.f90:(.text+0x5f7): undefined reference to `dfftw_execute_'
/usr/bin/ld: ksintegrate.f90:(.text+0x643): undefined reference to `dfftw_execute_'
/usr/bin/ld: ksintegrate.f90:(.text+0x79d): undefined reference to `dfftw_execute_'
/usr/bin/ld: ksintegrate.f90:(.text+0xa5b): undefined reference to `dfftw_execute_'
collect2: error: ld returned 1 exit status

我无法理解要使用库的哪些路径。 如何找到库的保存位置? 为什么 g++ 不需要任何指向库的路径?

对于这个问题看起来多么微不足道,我深表歉意,我是一个完全的新手,可以确保我付出了巨大的努力来解决这个问题。任何提示将不胜感激,谢谢

编辑

按照 Vladimir F Героям слава 的建议,我尝试使用该命令来验证库是否包含预期内容。 在 /usr/local/lib 中 我找到了文件。nmlibfftw3.alibfftw3.la

发行我得到了一个很长的清单。nm libfftw3.a

我尝试寻找我收到错误的命令之一,dfftw_plan_dft_1d

nm -S libfftw3.a | grep dfftw_plan_dft_1d

事实上,它返回了命中率

00000000000003a0 0000000000000026 T _Z18dfftw_plan_dft_1d_PP11fftw_plan_sPiPA2_dS4_S2_S2_
0000000000002df0 0000000000000026 T _Z19dfftw_plan_dft_1d__PP11fftw_plan_sPiPA2_dS4_S2_S2_
g++ linker-errors gfortran fftw

评论

0赞 Vladimir F Героям слава 11/21/2022
首先,请检查这些符号是否确实存在于您要链接的库中。当然,它们应该是,但如果找不到它们......您正在使用旧版界面。现代 Fortran(不是 FORTRAN)界面是不同的。
0赞 user37292 11/21/2022
@ Vladimir F Героям слава ,这些符号确实应该存在于库中。我不太确定我将如何明确检查,如果我理解正确,该库将作为目标文件(来自原始 C)提供。fftw.org/fftw3_doc/Fortran-Examples.html,此处提供了在 Fortran 中使用该库的示例,其中提到了该命令,这是我收到的第一个错误消息。dfftw_plan_dft_1d
0赞 Vladimir F Героям слава 11/21/2022
fftw3 库被拆分为几个较小的库文件。尝试使用该命令来确认里面有什么。nm
0赞 user37292 11/21/2022
@ Vladimir F Героям слава ,感谢您的建议,我想我确认了,为了清楚起见,我添加到了主要帖子中,非常感谢
0赞 n. m. could be an AI 11/21/2022
您的库导出损坏的 C++ 名称。而不是普通的朴素符号。它不应该。它可能构建不正确,尽管我不知道您应该做些什么才能实现它。要找出库的位置,请尝试使用编译命令进行标记。-Wl,--trace

答: 暂无答案