提问人:user37292 提问时间:11/20/2022 最后编辑:user37292 更新时间:11/21/2022 访问量:313
难以将 FFTW 库与 gfortran 链接
Difficulty in linking FFTW library with gfortran
问:
我有一个 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 中
我找到了文件。nm
libfftw3.a
libfftw3.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_
答: 暂无答案
评论
dfftw_plan_dft_1d
nm
-Wl,--trace