Error linking to Boost filesystem using cmake on cygwin - c++

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

Related

CMake can't find Boost_LIBRARIES variable

Final Edit: Solved.
Guys the problem occured because of the "WinSock32".
I've added
target_link_libraries(modernC__ -lws2_32)
and the code has been builded.
I have been using the Boost library on CLion in Ubuntu for about 1 year. But this week I also decided to install it on the Windows operating system.
so I downloaded the Boost library on GitHub and installed the Boost library using Find.Boost first and then "Find.Boost".
After writing the necessary commands in CMake settings section in CLion, I noticed that the variable $ {Boost_LIBRARIES} is empty.
When I don't use the message () function, the "CMake" part of the project does not give an error, but I get the error "undefined reference" after "build". Below are the CMake commands I wrote on CLion and the errors I received.
cmake_minimum_required(VERSION 3.16)
project(modernC__)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost 1.66.0)
message(${Boost_INCLUDE_DIR})
message(${Boost_FOUND})
message(${Boost_LIBRARY_DIRS})
IF (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
add_executable(modernC__
main.cpp
#concurrencyExampleOne.cpp
#adapterExampleOne.cpp
)
target_link_libraries(modernC__ ${Boost_LIBRARIES})
message(${Boost_LIBRARIES})
endif()
My output is:
"C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Berke\CLionProjects\modernC++
C:/boost/include/boost-1_66
TRUE
C:/boost/lib
CMake Error at CMakeLists.txt:22 (message):
message called with incorrect number of arguments
-- Configuring incomplete, errors occurred!
In this case, obviously Cmake is currently unable to find the .lib files that should link to my code.
My question is; How can I permanently drop .lib files into this variable, or is there any other way to do this?
If I do not use "message(${Boost_LIBRARIES}" so the compiler gives me this error;
[ 50%] Linking CXX executable modernC__.exe
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition':
C:/boost/include/boost-1_66/boost/system/error_code.hpp:676: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:679: undefined reference to `boost::system::generic_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei':
C:/boost/include/boost-1_66/boost/system/error_code.hpp:706: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:709: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:721: undefined reference to `boost::system::generic_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio5error19get_system_categoryEv':
C:/boost/include/boost-1_66/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh':
C:/boost/include/boost-1_66/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `_imp__WSAStartup#8'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE':
C:/boost/include/boost-1_66/boost/asio/detail/impl/winsock_init.ipp:56: undefined reference to `_imp__WSACleanup#0'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\modernC__.dir\build.make:86: modernC__.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/modernC__.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/modernC__.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: modernC__] Error 2
I think, you should specify boost libraries (as components) explicitly.
And you should add libraries to target.
Like this:
find_package(Boost COMPONENTS filesystem system locale context REQUIRED)
...
target_link_libraries(${PROJECT_NAME}
...
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_LOCALE_LIBRARY}
${Boost_CONTEXT_LIBRARY}
...
)

Undefined reference in libfreenect c++ wrapper

I want to print the number of connected devices with libfreenect in c++. As described in https://openkinect.org/wiki/C%2B%2B_Wrapper
i include the libfreenect.hpp header file in my TestKinectConnection.cpp.
My TestKinectConnection.cpp:
#include <iostream>
#include "libfreenect.hpp"
using namespace std;
int main(void) {
Freenect::Freenect nect;
freenect_context *f_ctx;
cout << nect.deviceCount() << endl;
return(0);
}
When i build with cmake --build build -- -j3 the terminal shows
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::Freenect()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0x40): undefined reference to `freenect_init'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0x90): undefined reference to `freenect_select_subdevices'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0xb0): undefined reference to `pthread_create'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::~Freenect()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectD2Ev[_ZN8Freenect8FreenectD5Ev]+0xa5): undefined reference to `pthread_join'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectD2Ev[_ZN8Freenect8FreenectD5Ev]+0xb4): undefined reference to `freenect_shutdown'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::deviceCount()':
TestKinectConnection.cpp:(.text._ZN8Freenect8Freenect11deviceCountEv[_ZN8Freenect8Freenect11deviceCountEv]+0x17): undefined reference to `freenect_num_devices'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::operator()()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectclEv[_ZN8Freenect8FreenectclEv]+0x4f): undefined reference to `freenect_process_events_timeout'
collect2: error: ld returned 1 exit status
CMakeFiles/projektinf.dir/build.make:95: recipe for target '../bin/projektinf' failed
make[2]: *** [../bin/projektinf] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/projektinf.dir/all' failed
make[1]: *** [CMakeFiles/projektinf.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
My CMakeLists.txt:
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.10)
# Project's name
project(projektinf)
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
set(PROJECT_SOURCE_DIR ${CMAKE_SOURCE_DIR})
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_SOURCE_DIR}/lib)
# The following folder will be included
include_directories("${PROJECT_SOURCE_DIR}/src/include")
add_executable(projektinf ${PROJECT_SOURCE_DIR}/src/main/TestKinectConnection.cpp)
add_library(libfreenect ${PROJECT_SOURCE_DIR}/src/include/libfreenect.hpp)
target_link_libraries(projektinf PUBLIC libfreenect)
set_target_properties(libfreenect PROPERTIES LINKER_LANGUAGE CXX)
https://github.com/OpenKinect/libfreenect/blob/master/wrappers/cpp/libfreenect.hpp
https://github.com/OpenKinect/libfreenect/blob/master/include/libfreenect.h
In short summary, libfreenect.hpp tries to include libfreenect.h und completely fails.
From your question it is unclear whether you installed libfreenect globally or simply bundled the library with your code.
If you installed it globally, you forgot to tell CMake that the libfreenect target needs to link with libfreenect.so using the -lfreenect linker flag.
Adding the following should fix that:
set_property(TARGET libfreenect PROPERTY INTERFACE_LINK_LIBRARIES -lfreenect)
The proper approach is to make libfreenect an IMPORTED target, as documented in "It's time to do CMake right".
If you bundled the .cpp with your code, you need to add the .cpp file to the add_library statement that defines the libfreenect target.

Linking GCC static/shared libraries and threads and -fPIC flag

I'm building a shared library (C++ code using some C++11/14 features) which links to my own static library and uses threads. It all uses CMake. When I link my shared library ("gscore") and it tries to link to my static library ("gslicense"), I get and error regarding a symbol in pthreads. Full text is further down but the short version is this:
'__pthread_key_create' can not be used when making a shared object; recompile with -fPIC
I've tried following the advice and added -fPIC to the compile-time flags for both of my projects. I've verified that -fPIC is part of my compilation flags but the error persists. I'm not sure what I'm missing.
Here is the error.
[ 57%] Linking CXX shared library libgscore.so
/usr/bin/ld: ../gslicense/libgslicense.a(gslicensing.cpp.o): relocation R_X86_64_32 against `__pthread_key_create' can not be used when making a shared object; recompile with -fPIC
../gslicense/libgslicense.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
GSCore/CMakeFiles/gscore.dir/build.make:1014: recipe for target 'GSCore/libgscore.so' failed
make[2]: *** [GSCore/libgscore.so] Error 1
CMakeFiles/Makefile2:173: recipe for target 'GSCore/CMakeFiles/gscore.dir/all' failed
make[1]: *** [GSCore/CMakeFiles/gscore.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
Here is how I added threading to my project in my CMakeLists.txt file
#
# pthread
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(gscore Threads::Threads)
This is all part of a port of a Windows codebase to linux using CMake. I'm extremely familiar with Windows but fumble around in Linux. I've tried searching all the threads I can find on SO but I'm still at a loss.
Any idea what I'm missing?

adding boost filesystem.hpp doesn't compile

I have the filesystem in my cmake:
find_package(Boost 1.63.0 COMPONENTS program_options system filesystem thread REQUIRED)
When I compile it says Found the following Boost libraries and filesystem is included.
Everything compiles fine. However once I try to include:
#include <boost/filesystem.hpp>
I get the following error. What's happening here?
CMakeFiles\runBasicBrimusTests.dir/objects.a(boost_filesystem_check.cpp.obj):
In function `_static_initialization_and_destruction_0':
C:/Users/b.karjoo/Documents/boost_1_63_0/boost/system/error_code.hpp:221:
undefined reference to `boost::system::generic_category()'
C:/Users/b.karjoo/Documents/boost_1_63_0/boost/system/error_code.hpp:222:
undefined reference to `boost::system::generic_category()'
C:/Users/b.karjoo/Documents/boost_1_63_0/boost/system/error_code.hpp:223:
undefined reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
Brimus-Test\basic_tests\CMakeFiles\runBasicBrimusTests.dir\build.make:910:
recipe for target 'Brimus-Test/basic_tests/runBasicBrimusTests.exe'
failed CMakeFiles\Makefile2:293: recipe for target
'Brimus-Test/basic_tests/CMakeFiles/runBasicBrimusTests.dir/all'
failed mingw32-make.exe[3]: ***
[Brimus-Test/basic_tests/runBasicBrimusTests.exe] Error 1
mingw32-make.exe[2]: ***
[Brimus-Test/basic_tests/CMakeFiles/runBasicBrimusTests.dir/all] Error
2 mingw32-make.exe[1]: ***
[Brimus-Test/basic_tests/CMakeFiles/runBasicBrimusTests.dir/rule]
Error 2 CMakeFiles\Makefile2:305: recipe for target
'Brimus-Test/basic_tests/CMakeFiles/runBasicBrimusTests.dir/rule'
failed Makefile:169: recipe for target 'runBasicBrimusTests' failed
mingw32-make.exe: *** [runBasicBrimusTests] Error 2
You should add target link libraries in cmake.
find_package(Boost 1.63.0 COMPONENTS program_options system filesystem thread
REQUIRED)
target_link_libraries(mytarget
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)

How to correctly import freenect2 using cmake?

I have to use a kinect2 (ubuntu 16.04 LTS). So I installed several things :
OpenNi (https://github.com/OpenNI/OpenNI)
OpenNi2 (https://github.com/occipital/openni2
libfreenect, (because I used the first kinect before) (https://github.com/OpenKinect/libfreenect)
libfreenect2 (https://github.com/OpenKinect/libfreenect2)
PrimeSese (with the corresponding part of https://www.icyphy.org/accessors/wiki/Main/InstallingThePrimeSenseKinectSensorOnUbuntu)
OpenCV (https://github.com/opencv/opencv)
When I was using the first kinect I was able to import the libfreenect tools without an problem, but now way with libfreenect !
You can find my CMake here. There isn't any problem with the others libs in the CMake.
What I changed to install libfreenect2:
I clone the repository in my folder ~/sofware. (I put all my libs here)
Instead of
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/freenect2
I did
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/software/libfreenect2/freenect2
With my Cmake when I compile I got this:
CMakeFiles/Kinect2CaptureYM.dir/app/Kinect2CaptureYM.cpp.o: In function `Kinect2CaptureYM::Kinect2CaptureYM()':
Kinect2CaptureYM.cpp:(.text._ZN16Kinect2CaptureYMC1Ev[_ZN16Kinect2CaptureYMC1Ev]+0x108): undefined reference to `libfreenect2::Freenect2::Freenect2(void*)'
Kinect2CaptureYM.cpp:(.text._ZN16Kinect2CaptureYMC1Ev[_ZN16Kinect2CaptureYMC1Ev]+0x427): undefined reference to `libfreenect2::Freenect2::~Freenect2()'
CMakeFiles/Kinect2CaptureYM.dir/app/Kinect2CaptureYM.cpp.o: In function `Kinect2CaptureYM::~Kinect2CaptureYM()':
Kinect2CaptureYM.cpp:(.text._ZN16Kinect2CaptureYMD1Ev[_ZN16Kinect2CaptureYMD1Ev]+0xb8): undefined reference to `libfreenect2::Freenect2::~Freenect2()'
collect2: error: ld returned 1 exit status
CMakeFiles/Kinect2CaptureYM.dir/build.make:156: recipe for target 'Kinect2CaptureYM' failed
make[2]: *** [Kinect2CaptureYM] Error 1
CMakeFiles/Makefile2:190: recipe for target 'CMakeFiles/Kinect2CaptureYM.dir/all' failed
make[1]: *** [CMakeFiles/Kinect2CaptureYM.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
So my make isn't able to link the the freenec2 lib. But I don't understand why as I put this in my makefile:
FIND_PACKAGE(freenect2 REQUIRED)
LIST(APPEND INCLUDE_DIRS ${FREENECT2_INCLUDE_DIRS})
LIST(APPEND LIBRARIES ${FREENECT2_LIBRARIES})
Does someone know how to link it correctly ? I'm really stuck beacause of that :/
If you need anything else just ask, thx!
PS : The most important things in my code are (enough to make the make crash):
the include : #include <libfreenect2/libfreenect2.hpp>
the declaration : libfreenect2::Freenect2 freenect2;