ld cannot find library that exists - c++

I'm trying to link my code to a library called clik. I'm passing the directory to ld with -L DIRECTORY and -lclik. DIRECTORY contains the file libclik.so. However, ld gives and error "ld: cannot find -lclik". Any ideas what could cause this?
To be more specific, I am using cmake. In cmake I have something like
find_library(CLIKLIB clik DIRECTORY)
and then for my executable I do:
target_link_libraries(executable ${CLIKLIB})
and that's how cmake generates the code for linking to clik but then ld fails.

Related

Why is there this dune build error? 'cannot find -lz'

I am trying to build a project with this dune file:
src/lib/precomputed_values/gen_values/dune:
(executable
(name gen_values)
(flags -w -32)
(libraries
;; opam libraries
stdio
async_kernel
compiler-libs
core_kernel
ppxlib
ppxlib.ast
ppxlib.astlib
async
core
ocaml-migrate-parsetree
base
ocaml-compiler-libs.common
async_unix
base.caml
sexplib0
;; local libraries
coda_runtime_config
consensus
transaction_snark
mina_state
snark_params
coda_genesis_proof
blockchain_snark
mina_base
global_signer_private_key
ppx_util
snarky.backendless
genesis_constants
pickles
test_genesis_ledger
coda_genesis_ledger
staged_ledger_diff
)
(preprocessor_deps ../../../config.mlh)
(preprocess
(pps ppx_version ppx_optcomp ppx_let ppxlib.metaquot ppx_here))
(instrumentation (backend bisect_ppx))
(modes native))
But am getting the error:
File "src/lib/precomputed_values/gen_values/dune", line 2, characters 7-17:
2 | (name gen_values)
^^^^^^^^^^
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status
File "caml_startup", line 1:
Error: Error during linking (exit code 1)
Makefile:84: recipe for target 'build' failed
make: *** [build] Error 1
Could anyone point me in the right direction?
Thanks :)
Do you have the libz available on your system ? it looks like the ld linker does not find it in its search path.
libz.a is a C library that implements C function for compressing/decompressing data. Such libraries come with your OS distribution independently of Ocaml.
Ocaml tooling provides way to use C library (via stub) using ld (the standard linker) that will link your ocaml stubs to the functions of libz.a . But if the required library is missing, you get the error you quoted.
-lz is saying to ld to link your application with the libz.a (or libz.so) library that is usually located in /usr/lib - if libz.a (libz.so) is not there, the linker will fail causing the emission of the following message :
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status

Linking a prebuilt binary in cpp does not work?

I just build the following package: faiss and my cpp program now recognises all library header so I can include them into my program. But my programm throws undefined reference to. So when I built faiss it created a libfaiss.a and a libfaiss.so which one to link and how??? I think I have tried any solution that I could find in the internet, but my knowledge of the build/linking process is limited.
My Faiss build-directory looks like this:
faiss-1.5.3/
-libfaiss.a
-libfaiss.so
-foo.h
-foo.o
-foo.cpp
-bar.h
-...
My tries. (I am using ROS, catkin_simple and gtests)
So this is how I would usually do it:
find_library(LibFaiss faiss HINT /home/tim/faiss/faiss-1_5_3/faiss-1.5.3/)
catkin_add_gtest(test_inverted_nn test/test_inverted-nn.cc
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}})
target_link_libraries(test_inverted_nn ${LIBRARY_NAME} ${LibFaiss})
I get the following Error:
/usr/bin/ld: cannot find -lLibFaiss-NOTFOUND}
collect2: error: ld returned 1 exit status
CMakeFiles/test_inverted_nn.dir/build.make:358: recipe for target '/home/tim/catkin_ws/devel/lib/nn/test_inverted_nn' failed
make[3]: *** [/home/tim/catkin_ws/devel/lib/inverted_nn/test_inverted_nn] Error 1
I also found this Solution here Import an external library into a ROS node, but that does not work for me:
add_library(libfaiss STATIC IMPORTED)
set_target_properties(libfaiss PROPERTIES IMPORTED_LOCATION /home/tim/faiss/faiss-1_5_3/faiss-1.5.3/libfaiss.a)
target_link_libraries(test_inverted_nn ${LIBRARY_NAME} ${libfaiss})
This throws the following Error:
/usr/bin/ld: cannot find -l}
collect2: error: ld returned 1 exit status
EDIT
After correcting the not_found error I am getting the following error:
CMake Warning at /opt/ros/melodic/share/catkin/cmake/test/gtest.cmake:180 (add_executable):
Cannot generate a safe runtime search path for target
test_inverted_multi_index because files in some directories may conflict
with libraries in implicit directories:
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/usr/local/lib
Some of these libraries may not be found correctly.
*** It does not matter what I add inside target_link_libraries I am always getting that error***
EDIT2
So with the tipp of #Aconcagua I deleted everything from the CMakeLists.txt and only added the following to my cmake options:
-DCMAKE_CXX_FLAGS=-L/home/tim/faiss/faiss-1_5_3/faiss-1.5.3/
But Im still getting the undefined reference errors. It does not make any difference

How to link with ntdll.lib using CMake?

I am using ntdll.lib functions in my code to set the system timer to a higher resolution.
But when I build my project I get this error:
...
.../bin/ld.exe: ... undefined reference to `__imp_NtSetTimerResolution'
collect2.exe: error: ld returned 1 exit status
...
How do I tell the linker to link with ntdll.lib in my CMake?
This worked for me:
if (WIN32)
target_link_libraries(executable ntdll)
endif()

Error building mpi2 - trouble linking libboost_system

I am trying to build mpi2 and encounter the following error during make:
/usr/bin/ld: CMakeFiles/env.dir/env.cc.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/usr/local/boost-1.56.0/lib/libboost_system.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
The cmake command I used prior to running make was:
cmake -DCMAKE_BUILD_TYPE=NativeRelease ../ -DBOOST_ROOT=/usr/local/boost-1.56.0/ -DBOOST_INCLUDEDIR=/usr/local/boost-1.56.0/include -DBOOST_LIBRARYDIR=/usr/local/boost-1.56.0/lib
I have tried linking against libboost_system.so, but I'm unsure how to do this with cmake (I tried adding -DCMAKE_CXX_FLAGS="-lboost_system" to the cmake command, but this had no effect).
How do I correctly link this with cmake to resolve this error?
I think the problem may be that Boost.System isn't listed as a requirement in the CMakeLists.txt.
Looking at https://github.com/uma-pi1/mpi2/blob/master/CMakeLists.txt#L67, it seems like system should be added immediately after chrono in the find_package(Boost ...) call.

Linking boost libraries

I have downloaded the boost library (version 1.46.1), but I don't know how to link it through xcode.I found an old question says to put the -lfftw3 flag, so I've put it.
I also added the path: /home/Documents/C++/boost_1_46_1 (it's the directory where I have put the library), but I am getting an error from the linker:
ld: warning: directory not found for option '-L/home/ramy/Documents/C++/boost_1_46_1'
ld: library not found for -lfftw3
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Command /Developer/usr/bin/clang++ failed with exit code 1
So the question are two:
1)How to manage xcode to link boost?
2)Where to put the library in file system? In linux there was /usr/lib, here there isn't this path, should I put it in /Developer/usr/lib?
Or for those who are looking for a quick answer (and are on linux), the magic is simply to add the following flags:
-l boost_system