C++ compile error when includeing Irrlicht static lib - c++

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

Related

Make failing with not used function

I am trying to build my code.
After I do cmake .. from a build directory I do make -j8 and I get
[ 90%] Building CXX object common/CMakeFiles/common.dir/src/utils/path_util.cpp.o
[ 95%] Linking CXX executable myproj
CMakeFiles/myproj.dir/main.cpp.o: In function `cv::String::~String()':
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
CMakeFiles/myproj.dir/main.cpp.o: In function `cv::String::operator=(cv::String const&)':
main.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
CMakeFiles/myproj.dir/build.make:95: recipe for target 'myproj' failed
make[2]: *** [myproj] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/myproj.dir/all' failed
make[1]: *** [CMakeFiles/myproj.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
The curious thing is that nowhere in the code I use cv::String.
I have also put
#Search for dependencies
set(MIN_OPENCV_VERSION "3.4.11" CACHE STRING "OpenCV version")
find_package(OpenCV ${MIN_OPENCV_VERSION} REQUIRED
COMPONENTS core
PATHS /usr/local/opencv-${MIN_OPENCV_VERSION}
NO_DEFAULT_PATH
)
in several CMakeLists.txt files and cmake finds opencv
What could be the problem?
EDIT
I set the VERBOSE environment variable to 1 as stated here
and I got
[ 90%] Building CXX object common/CMakeFiles/common.dir/src/utils/path_util.cpp.o
cd /home/user/ws/src/build/common && /usr/bin/c++ -I/usr/local/include/eigen3 -isystem /usr/local/opencv-3.4.11/include -isystem /usr/local/opencv-3.4.11/include/opencv -I/home/user/ws/src/common/include -I/home/user/ws/src/common/src -isystem /usr/local -fPIC -o CMakeFiles/common.dir/src/utils/path_util.cpp.o -c /home/user/ws/src/common/src/utils/path_util.cpp
[ 95%] Linking CXX executable road_info
/usr/bin/cmake -E cmake_link_script CMakeFiles/myproj.dir/link.txt --verbose=1
/usr/bin/c++ -rdynamic CMakeFiles/myproj.dir/main.cpp.o -o myproj mainpub_lib/mainpub.a
CMakeFiles/myproj.dir/main.cpp.o: In function `cv::String::~String()':
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
CMakeFiles/myproj.dir/main.cpp.o: In function `cv::String::operator=(cv::String const&)':
main.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
CMakeFiles/myproj.dir/build.make:95: recipe for target 'myproj' failed
make[2]: *** [myproj] Error 1
make[2]: Leaving directory '/home/user/ws/src/build'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/myproj.dir/all' failed
make[1]: *** [CMakeFiles/myproj.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
First, thanks #fabian for the help and pointers
I finally realized that the problem was not in my main.cpp but in a hpp file that main calls. This one.hpp file included another hpp file that was the one that caused the problem (When I commented it, the problem disapeared)
So what I did was change the CMakeLists.txt of the second level (the one dealing with the problematic hpp file) and added
target_link_libraries(mainpub PRIVATE ${OpenCV_LIBS})
With this the problem was solved
To allow for a verbose make I did export VERBOSE=1 because my make version is old. With the verbose output I could see that mainpub was the only library linked
What strikes me strange is that mainpub was apparently being built even with the problem instead the main build was signaled as problematic

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

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.

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

C++ CMake build error: undefined reference to `boost::throw_exception(std::exception const&)

I have been trying to integrate boost.Log into my project, but when building I am receiving the error message:
/usr/lib/cross/boost/lib/libboost_date_time.so: undefined reference to `boost::throw_exception(std::exception const&)'
I have checked the ensure I do not have BOOST_NO_EXCEPTIONS defined anywhere, but am still stumped as to why I am getting this error. My CMAKE file is as follows:
set(Boost_INCLUDE_DIR /usr/lib/cross/boost/include)
set(Boost_LIBRARY_DIR /usr/lib/cross/boost/lib)
find_package(Boost 1.63.0 COMPONENTS system filesystem log REQUIRED)
message(status "** Boost Include: ${Boost_INCLUDE_DIR}")
message(status "** Boost Libraries: ${Boost_LIBRARY_DIRS}")
message(status "** Boost Libraries: ${Boost_LIBRARIES}")
set (PROJECT_LINK_LIBS thrift crypto ssl sqlcipher)
link_directories( $ENV{LD_LIBRARY_PATH}
${Boost_LIBRARY_DIRS}
)
include_directories( $ENV{LD_INCLUDE_PATH}
$ENV{LD_INCLUDE_PATH}/sqlcipher
${Boost_INCLUDE_DIR}
)
add_executable(databaseManager ${MY_SRC})
target_link_libraries(databaseManager ${Boost_LIBRARIES} ${PROJECT_LINK_LIBS} )
The output from the FIND for the libraries seem to be ok:
-- Boost version: 1.63.0
1> -- Found the following Boost libraries:
1> -- system
1> -- filesystem
1> -- log
1> -- date_time
1> -- log_setup
1> -- thread
1> -- regex
1> -- chrono
1> -- atomic
1> status** Boost Include: /usr/lib/cross/boost/include
1> status** Boost Libraries: /usr/lib/cross/boost/lib
1> status** Boost Libraries:
/usr/lib/cross/boost/lib/libboost_system.so;/usr/lib/cross/boost/lib/libboost_filesystem.so;/usr/lib/cross/boost/lib/libboost_log.so;/usr/lib/cross/boost/lib/libboost_date_time.so;/usr/lib/cross/boost/lib/libboost_log_setup.so;/usr/lib/cross/boost/lib/libboost_thread.so;/usr/lib/cross/boost/lib/libboost_regex.so;/usr/lib/cross/boost/lib/libboost_chrono.so;/usr/lib/cross/boost/lib/libboost_atomic.so
Finally, the verbose output of the linking step where it fails:
/home/imx7/build/imx7-firmware-dev-package/src/maketools/obj.iMX7/bin/arm-devolo-linux-g++ -fexceptions -frtti -pthread -O3 -DNDEBUG -rdynamic CMakeFiles/databaseManager.dir/DatabaseManagerEventHandler.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerHandler.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerServer.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerService.cpp.o CMakeFiles/databaseManager.dir/Logger.cpp.o CMakeFiles/databaseManager.dir/dbConnection.cpp.o CMakeFiles/databaseManager.dir/thrift_common/base_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_common/base_types.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/DatabaseManager.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/dbManager_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/dbManager_types.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/Info.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/Supervision.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/supervision_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/supervision_types.cpp.o -o databaseManager -L/usr/lib/cross/official_libs -L/usr/lib/cross/boost/lib -Wl,-rpath,/usr/lib/cross/official_libs:/usr/lib/cross/boost/lib: /usr/lib/cross/boost/lib/libboost_system.so /usr/lib/cross/boost/lib/libboost_filesystem.so /usr/lib/cross/boost/lib/libboost_chrono.so /usr/lib/cross/boost/lib/libboost_log.so /usr/lib/cross/boost/lib/libboost_date_time.so /usr/lib/cross/boost/lib/libboost_log_setup.so /usr/lib/cross/boost/lib/libboost_thread.so /usr/lib/cross/boost/lib/libboost_regex.so /usr/lib/cross/boost/lib/libboost_atomic.so -lthrift -lcrypto -lssl -lsqlcipher
/home/imx7/build/imx7-firmware-dev-package/src/maketools/binaries.iMX7/tools/bin/../lib/gcc/arm-devolo-linux-gnueabihf/4.9.4/../../../../arm-devolo-linux-gnueabihf/bin/ld: warning: libz.so.1, needed by /usr/lib/cross/official_libs/libsqlcipher.so, not found (try using -rpath or -rpath-link)
/usr/lib/cross/boost/lib/libboost_date_time.so: undefined reference to `boost::throw_exception(std::exception const&)'
collect2: error: ld returned 1 exit status
src/CMakeFiles/databaseManager.dir/build.make:470: recipe for target 'src/databaseManager' failed
make[2]: Leaving directory '/var/cppbuild/DatabaseManager/build/Linux-Release'
CMakeFiles/Makefile2:88: recipe for target 'src/CMakeFiles/databaseManager.dir/all' failed
make[1]: Leaving directory '/var/cppbuild/DatabaseManager/build/Linux-Release'
make[2]: *** [src/databaseManager] Error 1
make[1]: *** [src/CMakeFiles/databaseManager.dir/all] Error 2
Makefile:132: recipe for target 'all' failed
make: *** [all] Error 2
Does anyone have any idea what else I can check or do to get past this error?
Cheers!
******* EDIT *********
There have been a few suggestions about adding exception to the COMPONENTS list. I have tried and tested this and unfortunately receive the same error.
The only thing of note is all the main libraries are linked dynamically (.so versions pulled in) and boost_exception is pulled in statically (.a version).
New version of find_package command:
find_package(Boost 1.63.0 COMPONENTS system atomic filesystem log exception REQUIRED)
Build error with above change:
/home/imx7/build/imx7-firmware-dev-package/src/maketools/obj.iMX7/bin/arm-devolo-linux-g++ -fexceptions -frtti -pthread -g -rdynamic CMakeFiles/databaseManager.dir/DatabaseManagerEventHandler.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerHandler.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerServer.cpp.o CMakeFiles/databaseManager.dir/DatabaseManagerService.cpp.o CMakeFiles/databaseManager.dir/Logger.cpp.o CMakeFiles/databaseManager.dir/dbConnection.cpp.o CMakeFiles/databaseManager.dir/thrift_common/base_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_common/base_types.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/DatabaseManager.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/dbManager_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_dbManager/dbManager_types.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/Info.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/Supervision.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/supervision_constants.cpp.o CMakeFiles/databaseManager.dir/thrift_supervisor/supervision_types.cpp.o -o databaseManager -L/usr/lib/cross/official_libs -L/usr/lib/cross/boost/lib -Wl,-rpath,/usr/lib/cross/official_libs:/usr/lib/cross/boost/lib: /usr/lib/cross/boost/lib/libboost_system.so /usr/lib/cross/boost/lib/libboost_atomic.so /usr/lib/cross/boost/lib/libboost_filesystem.so /usr/lib/cross/boost/lib/libboost_log.so /usr/lib/cross/boost/lib/libboost_exception.a /usr/lib/cross/boost/lib/libboost_date_time.so /usr/lib/cross/boost/lib/libboost_log_setup.so /usr/lib/cross/boost/lib/libboost_thread.so /usr/lib/cross/boost/lib/libboost_regex.so /usr/lib/cross/boost/lib/libboost_chrono.so -lthrift -lcrypto -lssl -lsqlcipher
/home/imx7/build/imx7-firmware-dev-package/src/maketools/binaries.iMX7/tools/bin/../lib/gcc/arm-devolo-linux-gnueabihf/4.9.4/../../../../arm-devolo-linux-gnueabihf/bin/ld: warning: libz.so.1, needed by /usr/lib/cross/official_libs/libsqlcipher.so, not found (try using -rpath or -rpath-link)
/usr/lib/cross/boost/lib/libboost_date_time.so: undefined reference to `boost::throw_exception(std::exception const&)'
collect2: error: ld returned 1 exit status
src/CMakeFiles/databaseManager.dir/build.make:471: recipe for target 'src/databaseManager' failed
make[2]: Leaving directory '/var/cppbuild/DatabaseManager/build/Linux-Debug'
CMakeFiles/Makefile2:88: recipe for target 'src/CMakeFiles/databaseManager.dir/all' failed
make[1]: Leaving directory '/var/cppbuild/DatabaseManager/build/Linux-Debug'
make[2]: *** [src/databaseManager] Error 1
make[1]: *** [src/CMakeFiles/databaseManager.dir/all] Error 2
Makefile:132: recipe for target 'all' failed
make: *** [all] Error 2
An update if anyone else ever comes across this.
The only way I was able to get it to work was with having the following snippet of code:
#define BOOST_NO_EXCEPTIONS
#include <boost/throw_exception.hpp>
void boost::throw_exception(std::exception const & e){
//do nothing
}
I played with the ordering of the libraries, with including them all explicitly, with using findBoost.cmake and all to no avail. Just a random quirk I guess!

Shared library on Linux and -fPIC error

I am trying to compile a shared library in Linux using a Makefile created with Cmake, but running make I obtain the following error:
Linking CXX shared library libcpp-lib.so
/usr/bin/ld: /home/davide/Desktop/boost_1_55_0/stage/lib/libboost_system.a(error_code.o): relocation R_X86_64_32 against .rodata.str1.1 can not be used when making a shared object; recompile with -fPIC
/home/davide/Desktop/boost_1_55_0/stage/lib/libboost_system.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
make[2]: *** [libcpp-lib.so] Error 1
make[1]: *** [CMakeFiles/cpp-lib.dir/all] Error 2
make: *** [all] Error 2
I provide the following command in the CMakeLists.txt in order to say that I want a shared (.so) library:
add_library(cpp-lib SHARED ${CPP_FILES})
What else do I need to specify in order to avoid the -fPIC error shown above?
Thanks a lot in advance
The boost libraries needs to be compiled using -fPIC:
Please have a look at: How to compile static library with -fPIC from boost.python
Try to add compiler flags by cmake by in your project:
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")