Difficulty in linking FFTW library with gfortran - g++

I have a c++ program, which uses the FFTW3 library, which I would like to translate to Fortran. I am using Ubuntu 22.04.1.
I installed the FFTW3 library as detailed in Installation on UNIX
The c++ main program is named ksbenchmark.cpp and, using g++, I compile it and link it (to the math and fftw3 libraries) with
g++ -o my_executable.out ksbenchmark.cpp -lm -lfftw3
which works great.
I have translate the c++ code in Fortran, the source file being named ksbenchmrk.f90.
If I issue
gfortran ksbenchmark.f90 -lfftw3 -lm
I get error messages
/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 have spent the afternoon reading about it, and it seems I may have to use -I and -L flags such as
gfortran test.f90 -L/new/path/to/lib -I/new/path/to/include -lfftw3 -lm
as illustrated in the post Problems linking FFTW with gfortran (symbol(s) not found for architecture x86_64).
The first question is, what paths to use?
I can find files referring to fftw3 in /usr/local/lib as well as in /usr/local/include, but the attempt
gfortran ksbenchmark.f90 -L/usr/local/lib -I/usr/local/include -lfftw3 -lm
returns the same error as before
/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
I am unable to understand which paths to the library are to be used.
How to find where the library is saved?
Why does not g++ need any paths to the library?
I apologise for how trivial the question might seem, I am a total newbie can ensure I put a significant effort in trying to sort this out. Any hint would be most appreciated, thanks
EDIT
Following Vladimir F Героям слава's advice, I tried using the nm command to verify the library contains what is expected to.
In /usr/local/lib
I found libfftw3.a and libfftw3.la files.
Issuing nm libfftw3.a I get quite a long list.
I tried looking for one of the commands I get an error about, dfftw_plan_dft_1d
nm -S libfftw3.a | grep dfftw_plan_dft_1d
and indeed it returns hits
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_

Related

cuda: undefined reference to `cufftExecR2C'

Im using Cudas cuFFT library and therefore I have added the library path to $LD_LIBRARY_PATH. When compiling, Im using the -lcufft flag, after including the header with #include<cufft.h>. But I still receive the following errors:
/usr/bin/ld: tmpxft_000029f1_00000000-6_render.cudafe1.cpp:(.text+0x51b): undefined reference to `cufftExecR2C'
/usr/bin/ld: tmpxft_000029f1_00000000-6_render.cudafe1.cpp:(.text+0x528): undefined reference to `cufftDestroy'
/usr/bin/ld: tmpxft_000029f1_00000000-6_render.cudafe1.cpp:(.text+0x16d8): undefined reference to `cufftPlan2d'
/usr/bin/ld: tmpxft_000029f1_00000000-6_render.cudafe1.cpp:(.text+0x16f3): undefined reference to `cufftExecC2R'
/usr/bin/ld: tmpxft_000029f1_00000000-6_render.cudafe1.cpp:(.text+0x16fd): undefined reference to `cufftDestroy'
collect2: error: ld returned 1 exit status
This is a CMake project and I link via: set(CMAKE_CUDA_FLAGS_RELEASE " -I/opt/nvidia/hpc_sdk/Linux_x86_64/22.11/math_libs/include -L/opt/nvidia/hpc_sdk/Linux_x86_64/22.11/math_libs/lib64 -lcufft")
Am I missing something crucial?

How do I solve GATE building file make error

I am installing gate on centOS 7. Almost finish all installation besides a little error when I try to build installation file in build directory before final installation, make install. Here is the command line response:
100%] Building CXX object CMakeFiles/Gate.dir/source/physics/src/GateXrayBoundaryPB.cc.o
[100%] Building CXX object CMakeFiles/Gate.dir/source/physics/src/GateeMultipleScatteringPB.cc.o
[100%] Building CXX object CMakeFiles/Gate.dir/source/physics/src/GatehMultipleScatteringPB.cc.o
[100%] Linking CXX executable Gate
/usr/bin/ld: warning: libtbb.so.2, needed by /home/xincao/GATE/root_v6.18/lib/libImt.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvdt.so, needed by /home/xincao/GATE/root_v6.18/lib/libROOTVecOps.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libRDAVIX.so.6.18, needed by /home/xincao/GATE/root_v6.18/lib/libROOTDataFrame.so, not found (try using -rpath or -rpath-link)
/home/xincao/GATE/root_v6.18/lib/libROOTDataFrame.so: undefined reference to `Davix::DavPosix::pread(Davix_fd*, void*, unsigned long, long, Davix::DavixError**)'
/home/xincao/GATE/root_v6.18/lib/libImt.so: undefined reference to `tbb::task_scheduler_init::initialize(int)'
/home/xincao/GATE/root_v6.18/lib/libImt.so: undefined reference to `tbb::task::note_affinity(unsigned short)'
/home/xincao/GATE/root_v6.18/lib/libImt.so: undefined reference to `tbb::internal::throw_exception_v4(tbb::internal::exception_id)'
/home/xincao/GATE/root_v6.18/lib/libImt.so: undefined reference to `tbb::task_group_context::is_group_execution_cancelled() const'
/home/xincao/GATE/root_v6.18/lib/libImt.so: undefined reference to `vtable for tbb::task'
/home/xincao/GATE/root_v6.18/lib/libImt.so: undefined reference to `tbb::task_group_context::reset()'
/home/xincao/GATE/root_v6.18/lib/libImt.so: undefined reference to `tbb::interface7::internal::task_arena_base::internal_terminate()'
...
/home/xincao/GATE/root_v6.18/lib/libROOTDataFrame.so: undefined reference to `Davix::DavPosix::stat(Davix::RequestParams const*, std::string const&, stat*, Davix::DavixError**)'
/home/xincao/GATE/root_v6.18/lib/libROOTDataFrame.so: undefined reference to `Davix::DavixError::getErrMsg() const'
/home/xincao/GATE/root_v6.18/lib/libImt.so: undefined reference to `tbb::interface7::internal::task_arena_base::internal_execute(tbb::interface7::internal::delegate_base&) const'
/home/xincao/GATE/root_v6.18/lib/libROOTDataFrame.so: undefined reference to `Davix::Context::~Context()'
/home/xincao/GATE/root_v6.18/lib/libImt.so: undefined reference to `tbb::internal::allocate_root_with_context_proxy::allocate(unsigned long) const'
collect2: error: ld returned 1 exit status
make[2]: *** [Gate] Error 1
make[1]: *** [CMakeFiles/Gate.dir/all] Error 2
make: *** [all] Error 2
Does anyone can help? Any reply will be highly appreciated!!
Thanks!
It is because the geant4.sh script in the Geant4 bin has not been run. This sets up the LD_LIBRARY_PATH variable which then allows these libraries to be found. (Note that you will need the data from CERN in order to successfully run geant4.sh)

std::experimental::filesystem::v1::status link error even after -lstdc++ flag

I try c++1z feature to use std::experimental::filesystem
After adding -lstdc++fs flag with g++, I can see that I am able to link std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts() with my program.
i.e I am not getting this error anymore
sdc.cpp:(.text+0x7e5): undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
However, even after linking, I get the following error.
g++ -std=c++1z timer.cpp -lstdc++fs -O2 -I include -L lib -lOpenTimer -lpthread -o timer.out
lib/libOpenTimer.a(spef.cpp.o): In function spef::Spef::read(std::experimental::filesystem::v1::__cxx11::path const&)':
spef.cpp:(.text._ZN4spef4Spef4readERKNSt12experimental10filesystem2v17__cxx114pathE[_ZN4spef4Spef4readERKNSt12experimental10filesystem2v17__cxx114pathE]+0x2e): undefined reference tostd::experimental::filesystem::v1::status(std::experimental::filesystem::v1::__cxx11::path const&)'
lib/libOpenTimer.a(sdc.cpp.o): In function ot::sdc::home[abi:cxx11]()':
sdc.cpp:(.text+0x414): undefined reference tostd::experimental::filesystem::v1::status(std::experimental::filesystem::v1::__cxx11::path const&)'
sdc.cpp:(.text+0x7ab): undefined reference to std::experimental::filesystem::v1::status(std::experimental::filesystem::v1::__cxx11::path const&)'
lib/libOpenTimer.a(sdc.cpp.o): In functionot::sdc::SDC::read(std::experimental::filesystem::v1::__cxx11::path const&)':
sdc.cpp:(.text+0x2fa1): undefined reference to std::experimental::filesystem::v1::status(std::experimental::filesystem::v1::__cxx11::path const&)'
sdc.cpp:(.text+0x3084): undefined reference tostd::experimental::filesystem::v1::status(std::experimental::filesystem::v1::__cxx11::path const&)'
sdc.cpp:(.text+0x30db): undefined reference to std::experimental::filesystem::v1::current_path[abi:cxx11]()'
sdc.cpp:(.text+0x30f7): undefined reference tostd::experimental::filesystem::v1::absolute(std::experimental::filesystem::v1::__cxx11::path const&, std::experimental::filesystem::v1::__cxx11::path const&)'
sdc.cpp:(.text+0x3788): undefined reference to std::experimental::filesystem::v1::remove(std::experimental::filesystem::v1::__cxx11::path const&)'
lib/libOpenTimer.a(os.cpp.o): In functionot::user_homeabi:cxx11':
os.cpp:(.text+0x30d): undefined reference to `std::experimental::filesystem::v1::current_pathabi:cxx11'
collect2: error: ld returned 1 exit status
Any help to resolve this would be much appreciated.
It was me who posted the question. It seems like I was able to resolve it myself.
I added the -lstdc++fs at the end instead of middle and it started working.
Here's the updated command
g++ -std=c++1z timerapp.cpp -O2 -I include -L lib -lOpenTimer -lpthread -o timer.out -lstdc++fs
compared to the previous command
g++ -std=c++1z timerapp.cpp -lstdc++fs -O2 -I include -L lib -lOpenTimer -lpthread -o timer.out
Don't know why some linking flags are expected to be added in the end though.
-lstdc++fs flag should be passed at the tail of linker command.
With CMake:
This works:
target_link_libraries(your_project stdc++fs)
And this won't:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -lstdc++fs")

relocation R_X86_64_PC32 against `_ZN7QStringD1Ev' can not be used when making a shared object; recompile with -fPIC

When I build qtwebkit, I got this problem.
/usr/bin/ld: /data/zhengjie/qt-everywhere-opensource-src-5.5.1/qtmultimedia/lib/libQt5Multimedia.a(qmediametadata.o): relocation R_X86_64_PC32 against `_ZN7QStringD1Ev' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
make: *** [../../lib/libWebCore.so.1.0.0] Error 1
Can anyone help?

/usr/bin/ld: cannot find -lpq

I want connect to postgresql by libpqxx (C++)
When running g++ with -lpq I got this error:
[michael#michael cpp]$ g++ Store.cpp -lpqxx -std=c++11 -lpq
/usr/bin/ld: cannot find -lpq collect2: error: ld returned 1 exit
status
When I remove -lpq it's run well and work, I want to know why with -lpq it's not work and how fix it.
Thanks, Michael.