CMake linker cannot find library that does not begin with "lib" - c++

I am using CMake to compile an application which uses the HSImage library on github. After installing with pip, the HSI library generates a shared library file, in my case it is created at /usr/src/HSI/HSI.cpython-36m-aarch64-linux-gnu.so
I am trying to link this library to my application with CMake, but the CMake find_library method is having some trouble finding the library. Here is the relevant part of my CMakeLists.txt file:
CMakeLists.txt
set(HSI_DIR /usr/src/HSI)
find_library(HSI_LIB HSI.cpython-36m-aarch64-linux-gnu PATHS ${HSI_DIR})
message(STATUS "HSI: ${HSI_LIB}") # outputs /usr/src/HSI/HSI.cpython-36m-aarch64-linux-gnu.so
add_executable(${TARGET_NAME} <sources...>)
target_link_directories(${TARGET_NAME} PUBLIC ${HSI_DIR})
target_link_libraries(${TARGET_NAME}
${HSI_LIB}
<other libs...>
-Wl,--unresolved-symbols=ignore-in-shared-libs
)
When building, this produces the following error message:
cd /home/nvidia/projects/HsiInference/build;/usr/local/bin/cmake --build "/home/nvidia/projects/HsiInference/build" --target hsi_inference_onnx -- ;
Scanning dependencies of target hsi_inference_onnx
[ 14%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/targets/HsiInferenceOnnx/main_onnx.cpp.o
[ 28%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/targets/HsiInferenceOnnx/HsiInferenceOnnx.cpp.o
[ 42%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/ftpnano.cpp.o
[ 57%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/getOptions.cpp.o
[ 71%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/logger.cpp.o
[ 85%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/utils.cpp.o
[100%] Linking CXX executable hsi_inference_onnx_debug
CMakeFiles/hsi_inference_onnx.dir/build.make:245: recipe for target 'hsi_inference_onnx_debug' failed
CMakeFiles/Makefile2:123: recipe for target 'CMakeFiles/hsi_inference_onnx.dir/all' failed
**/usr/bin/ld: cannot find -lHSI.cpython-36m-aarch64-linux-gnu**
**collect2: error: ld returned 1 exit status**
make[3]: *** [hsi_inference_onnx_debug] Error 1
make[2]: *** [CMakeFiles/hsi_inference_onnx.dir/all] Error 2
CMakeFiles/Makefile2:130: recipe for target 'CMakeFiles/hsi_inference_onnx.dir/rule' failed
make[1]: *** [CMakeFiles/hsi_inference_onnx.dir/rule] Error 2
Makefile:196: recipe for target 'hsi_inference_onnx' failed
make: *** [hsi_inference_onnx] Error 2
Build failed.
The important part:
/usr/bin/ld: cannot find -lHSI.cpython-36m-aarch64-linux-gnu
collect2: error: ld returned 1 exit status
From what I have gathered, target_link_libraries simply adds -l<library_name> to the link command, and -l<library_name> assumes that there is a file called lib<library_name>.so to link, which is not the case here. How can I get CMake to link the library properly despite the weird filename?
NOTE:
I am able to get the project to build by doing the following:
Delete the project's build directory to clear CMake caches
Rename the file or create a symbolic link to libhsi.so
Change CMakeLists.txt to find_library(HSI_LIB hsi PATHS ${HSI_DIR})
This changes the link command to -lhsi instead, which is able to find the renamed/soft-linked library file. HOWEVER, this is not ideal for me and the original question remains unanswered :)

For libraries with weird filename you should add : before the filename. Be careful, like mentioned in https://linux.die.net/man/1/ld : If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a (namespec is what comes after -l).
For your example you should replace ${HSI_LIB} in target_link_libraries by :${HSI_LIB}.so.

Related

CMake issues with target_include directories

I'm working on a new project, and trying to set up cmake from the ground up for the first time. I have two libraries that get built as targets in my project; libnet and liberror. I am linking libnet with liberror when I build the libnet target, and want to be able to #include header files that exist within liberror. I have the following added to the CMakeLists.txt for liberror:
target_include_directories(liberror SYSTEM PUBLIC core/error)
Now, I have the following added to the CMakeLists.txt for libnet:
target_link_libraries(libnet
PUBLIC
liberror)
So, liberror provides a header called ExceptionBuilder.hpp. When I try to do the following from libnet:
#include <core/error/ExceptionBuilder.hpp>
I get this error:
fatal error: core/error/ExceptionBuilder.hpp: No such file or directory
3 | #include <core/error/ExceptionBuilder.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I am quite new to cmake, so probably just setting something up incorrectly here, but any ideas? Here is the project structure:
Project Structure
Here is the verbose build output:
nmcurtis#DESKTOP-JC26375:~/distro/build$ make
-- Boost version: 1.58.0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nmcurtis/distro/build
Consolidate compiler generated dependencies of target distro_core_error_objects
[ 11%] Built target distro_core_error_objects
Consolidate compiler generated dependencies of target distro_core_utility_objects
[ 16%] Built target distro_core_utility_objects
[ 22%] Linking CXX shared library libdistro_core_utility.so
/usr/local/gcc-11.3.0/bin/g++-11.3 -fPIC -fPIC -Wno-terminate -shared -Wl,-soname,libdistro_core_utility.so -o libdistro_core_utility.so CMakeFiles/distro_core_utility_objects.dir/Concepts.cpp.o
[ 22%] Built target distro_core_utility
[ 27%] Linking CXX shared library libdistro_core_error.so
/usr/local/gcc-11.3.0/bin/g++-11.3 -fPIC -fPIC -Wno-terminate -shared -Wl,-soname,libdistro_core_error.so -o libdistro_core_error.so CMakeFiles/distro_core_error_objects.dir/Exception.cpp.o CMakeFiles/distro_core_error_objects.dir/ExceptionBuilder.cpp.o -Wl,-rpath,/home/nmcurtis/distro/build/core/utility: ../utility/libdistro_core_utility.so
[ 27%] Built target distro_core_error
[ 33%] Linking CXX static library libdistro_core_utility_static.a
/usr/bin/ar qc libdistro_core_utility_static.a CMakeFiles/distro_core_utility_objects.dir/Concepts.cpp.o
/usr/bin/ranlib libdistro_core_utility_static.a
[ 33%] Built target distro_core_utility_static
[ 38%] Linking CXX static library libdistro_core_error_static.a
/usr/bin/ar qc libdistro_core_error_static.a CMakeFiles/distro_core_error_objects.dir/Exception.cpp.o CMakeFiles/distro_core_error_objects.dir/ExceptionBuilder.cpp.o
/usr/bin/ranlib libdistro_core_error_static.a
[ 38%] Built target distro_core_error_static
Consolidate compiler generated dependencies of target distro_core_net_objects
[ 44%] Building CXX object core/net/CMakeFiles/distro_core_net_objects.dir/Address.cpp.o
/home/nmcurtis/distro/core/net/Address.cpp:4:10: fatal error: core/error/ExceptionBuilder.hpp: No such file or directory
4 | #include <core/error/ExceptionBuilder.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
core/net/CMakeFiles/distro_core_net_objects.dir/build.make:120: recipe for target 'core/net/CMakeFiles/distro_core_net_objects.dir/Address.cpp.o' failed
make[2]: *** [core/net/CMakeFiles/distro_core_net_objects.dir/Address.cpp.o] Error 1
CMakeFiles/Makefile2:262: recipe for target 'core/net/CMakeFiles/distro_core_net_objects.dir/all' failed
make[1]: *** [core/net/CMakeFiles/distro_core_net_objects.dir/all] Error 2
Makefile:135: recipe for target 'all' failed
make: *** [all] Error 2
nmcurtis#DESKTOP-JC26375:~/distro/build$
If you do target_include_directories(liberror SYSTEM PUBLIC core/error) you do not need core/error in #include <core/error/ExceptionBuilder.hpp>
Do this in core/error/CMakeLists.txt.
target_include_directories(liberror SYSTEM PUBLIC .)
# or
# target_include_directories(liberror SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
#include "ExceptionBuilder.hpp"
If you wish #include "core/error/ExceptionBuilder.hpp" then
do this in core/error/CMakeLists.txt.
target_include_directories(liberror SYSTEM PUBLIC ../..)
# or
# target_include_directories(liberror SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../..)

How can I run many C++ source files in one CLion project?

I am using CLion as an IDE. When I create a project a main.cpp is automatically added. I would like to add 30-40 cpp files in a project and keep them as one project. Basically, I just wanna create many .cpp files in one folder and make CLion run them. I can do this in Pycharm by simply creating a project and add as many .py files as I want. But when I want to do this on CLion I got an error. Is it possible to add many .cpp files in a project in CLion, if yes, how can I do that?
An error could be seen in the below. I added a second.cpp to the project and run and this error message appears.
====================[ Build | trial | Debug ]===================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/mertsaner/CLionProjects/trial/cmake-build-debug --target trial -- -j 6
Scanning dependencies of target trial
[ 66%] Building CXX object CMakeFiles/trial.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/trial.dir/second.cpp.o
[100%] Linking CXX executable trial
duplicate symbol '_main' in:
CMakeFiles/trial.dir/main.cpp.o
CMakeFiles/trial.dir/second.cpp.o
ld: 1 duplicate symbol for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [trial] Error 1
make[2]: *** [CMakeFiles/trial.dir/all] Error 2
make[1]: *** [CMakeFiles/trial.dir/rule] Error 2
make: *** [trial] Error 2
Clion uses Cmake. If you want to create multiple executable files for eg with names (ex1.cpp, ex2.cpp. ex3.cpp) in one directory, you will do something like this in the CMake file of your directory.
cmake_minimum_required(VERSION 3.18)
project(some_project)
set(CMAKE_CXX_STANDARD 20)
add_executable(executable1 ex1.cpp)
add_executable(executable2 ex2.cpp)
add_executable(executable3 ex3.cpp)
and so on..

Cannot find lpublic library

Trying to compile my project on Centos 8.2 using cmake 3.12, im facing with the following error:
cannot find -lpublic
here is full log:
[ 20%] Building CXX object CMakeFiles/ .cpp.o
[ 40%] Building CXX object CMakeFiles/ .cpp.o
[ 60%] Linking CXX shared library .so
[ 60%] Built target myproj
Scanning dependencies of target nist01
[ 80%] Building CXX object CMakeFiles/ .cpp.o
[100%] Linking CXX executable
/usr/bin/ld: cannot find -lpublic
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/nist01.dir/build.make:85: ...] Error 1
make[1]: *** [CMakeFiles/Makefile2:105: CMakeFiles/...] Error 2
make: *** [Makefile:84: all] Error 2
Tried to find something on the internet, but nothing shown up.
Thanks for your help.
That seems like you try to link against the target "public", which is a suspicious target name. It might be target_link_libraries command with lowercase public instead of uppercase one, like target_link_libraryies(nist01 public something) instead of target_link_libraries(nist01 PUBLIC something).

Cannot build opencv 2.4 on a 64 bit mac for C++ project in Xcode

After looking around a lot and trying every option I found on SO, I'm a little confused about how to get the OpenCV library for architecture x86_64 loaded on my Mac. Here's a little bit of background info:
The first folder contains the Android SDK. The second is the iPhone framework, which uses i386 and armv7 architectures.
Now, I want to create a simple "Hello World" C++ application in Xcode which needs the correct library for x86_64 architecture.
I've looked at this tutorial as well as others' posted steps as follows:
mkdir build
cd build
cmake -G "Unix Makefiles" ..
make -j8
sudo make install
With all version (except OpenCV-2.4.6.1), the process fails at "make -j8". I get the same error every time:
Linking CXX shared library ../../lib/libopencv_core.dylib
Undefined symbols for architecture x86_64:
"___sincos_stret", referenced from:
cv::RotatedRect::points(cv::Point_<float>*) const in matrix.cpp.o
cv::RotatedRect::boundingRect() const in matrix.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_core.2.4.3.dylib] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 28%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfThreading.cpp.o
[ 28%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfTileDescriptionAttribute.cpp.o
[ 28%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfTiledInputFile.cpp.o
[ 29%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfTiledMisc.cpp.o
[ 29%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfTiledOutputFile.cpp.o
[ 29%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfTiledRgbaFile.cpp.o
[ 29%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfTileOffsets.cpp.o
[ 29%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfTimeCode.cpp.o
[ 29%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfTimeCodeAttribute.cpp.o
[ 29%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfVecAttribute.cpp.o
[ 29%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfVersion.cpp.o
[ 29%] [ 30%] Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfWav.cpp.o
Building CXX object 3rdparty/openexr/CMakeFiles/IlmImf.dir/IlmImf/ImfZipCompressor.cpp.o
Linking CXX static library ../lib/libIlmImf.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThread.cpp.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadMutex.cpp.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadSemaphore.cpp.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadSemaphorePosixCompat.cpp.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThread.cpp.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadMutex.cpp.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadSemaphore.cpp.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadSemaphorePosixCompat.cpp.o) has no symbols
[ 30%] Built target IlmImf
make: *** [all] Error 2
pdls-mbp:build pdl$
With OpenCV-2.4.6.1, the process fails at "sudo make install" and I get the following error:
Linking CXX shared library ../../lib/libopencv_core.dylib
Undefined symbols for architecture x86_64:
"___sincos_stret", referenced from:
cv::RotatedRect::points(cv::Point_<float>*) const in matrix.cpp.o
cv::RotatedRect::boundingRect() const in matrix.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_core.2.4.6.dylib] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
make: *** [all] Error 2
Lucys-MacBook-Pro:build pdl$
When I installed OpenCV using Homebrew, opencv was installed in use/local/opt/opencv and I can see all of the dylib files under the lib folder:
All of this completely confuses me, because I think all I need is a .a library file, no?
I have found the dylib files that were installed with HomeBrew in /usr/local/Cellar/opencv/2.4.6.1/lib but when I try to choose frameworks and libraries to add, they do not show up in the list. Instead, this is all I see:
Aldo, been following this MacPorts tutorial.
Have you considered using macports? It has opencv 2.4.8 available.
So after installing macports, all that nonsense you've been through, is reduced to:
$ sudo port selfupdate
$ sudo port install opencv
if you want a variant or two, for example OpenCL and Python support, then that would be:
$ sudo port install opencv +opencl +python27
and the binaries/libraries/whatever will be installed into /opt/local/....

C++ compile error when includeing Irrlicht static lib

U have a compile error when trying to include the Irrlicht static libraries into my cmake project
Compile error:
max#max-MS-7369:~/Desktop/survival/build$ make Scanning dependencies of target survival
[ 33%] Building CXX object src/CMakeFiles/survival.dir/technic.cpp.o
[ 66%] Building CXX object src/CMakeFiles/survival.dir/render.cpp.o
[100%] Building CXX object src/CMakeFiles/survival.dir/survival.cpp.o
Linking CXX executable ../debug/survival
/usr/bin/ld: ../../irrlicht/lib/Linux/libIrrlicht.a(CIrrDeviceLinux.o): undefined reference to symbol 'XConvertSelection'
/usr/bin/ld: note: 'XConvertSelection' is defined in DSO /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libX11.so so try adding it to the linker command line
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libX11.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
make[2]: *** [debug/survival] Error 1
make[1]: *** [src/CMakeFiles/survival.dir/all] Error 2
make: *** [all] Error 2
max#max-MS-7369:~/Desktop/survival/build$
And this is how I try to include the lib:
# find dependencies for irrlicht
FIND_PACKAGE(ZLIB)
FIND_PACKAGE(X11)
FIND_PACKAGE(OpenGL)
# includes
set( survival_CUSTOM_INCLUDES
${ZLIB_INCLUDE_DIR}
${X11_INCLUDE_DIR}
${OPENGL_INCLUDE_DIR}
${survival_SOURCE_DIR}/irrlicht/include
)
# libraries
set( survival_CUSTOM_LIBRARIES
${ZLIB_LIBRARIES}
${X11_LIBRARIES}
${OPENGL_LIBRARIES}
${survival_SOURCE_DIR}/irrlicht/lib/Linux/libIrrlicht.a
)
I don't get why it says it cant find XConvertSelection even though I have included the X11 lib.
i added those 2 libs to fix it
/usr/lib/x86_64-linux-gnu/libX11.so
/usr/lib/x86_64-linux-gnu/libXxf86vm.so.1
they werent included by the libs given from FIND_PACKAGE()