How to link with ntdll.lib using CMake? - c++

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()

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

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.

ld cannot find library that exists

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.

Error linking to Boost filesystem using cmake on cygwin

I'm using cmake 2.8.9, g++ 3.4.4, and Boost 1.50. in Cygwin on Windows 8 64 bit.
Here is the error message I get.
Linking CXX executable RayTracer.exe
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x89c):
undefined reference to boost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8a6):
undefined reference toboost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8b0):
undefined reference to boost::system::system_category()'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld:
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o: bad reloc address 0xb in
section
.text$_ZN5boost6system14error_categoryD1Ev[boost::system::error_category::~error_category()]'
collect2: ld returned 1 exit status
CMakeFiles/RayTracer.dir/build.make:94: recipe for target
RayTracer.exe' failed make[2]: *** [RayTracer.exe] Error 1
CMakeFiles/Makefile2:64: recipe for target
CMakeFiles/RayTracer.dir/all' failed make[1]: *
[CMakeFiles/RayTracer.dir/all] Error 2 Makefile:75: recipe for target
`all' failed make: * [all] Error 2
From what I've seen, the usual problem is failing to link the boost system library, but I made sure to do that. Here is the relevant portion of my CMakeLists.txt file:
#Edit: cmake can't find the static libraries on cygwin, so I'm setting this to false for now.
SET(Boost_USE_STATIC_LIBS FALSE)
FIND_PACKAGE(Boost 1.50 REQUIRED date_time program_options thread filesystem system unit_test_framework)
IF(${Boost_FOUND})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ENDIF()
add_executable(RayTracer
Ray_Tracer.cpp
)
target_link_libraries(RayTracer ${Boost_PROGRAM_OPTIONS_LIBRARIES})
And here's the line in my .cpp file that triggers the error:
#include <boost/filesystem.hpp>
Any idea what I'm doing wrong?
You need to tell the linker to link Boost.Filesystem and Boost.System libraries.
You can do:
target_link_libraries(RayTracer
${Boost_PROGRAM_OPTIONS_LIBRARIES}
${Boost_FILESYSTEM_LIBRARIES}
${Boost_SYSTEM_LIBRARIES}
)
or if you just want to link all the libs specified in your find_package(Boost...) call, you can do:
target_link_libraries(RayTracer ${Boost_LIBRARIES})
For further details on the FindBoost CMake module, see the docs or run:
cmake --help-module FindBoost