Why CMake cannot find my Boost libraries on Ubuntu 18.04? - c++

This is driving me nuts. In my CMakeLists.txt I have:
find_package(Boost 1.63.0 REQUIRED SYSTEM)
Result:
[0/1] Re-running CMake...
Build type: Release
CMake Error at /usr/share/cmake-3.10/Modules/FindBoost.cmake:1947 (message):
Unable to find the requested Boost libraries.
Boost version: 1.65.1
Boost include path: /usr/include
Could not find the following Boost libraries:
boost_system
But:
$ locate libboost_system
/usr/lib/x86_64-linux-gnu/libboost_system.a
/usr/lib/x86_64-linux-gnu/libboost_system.so
/usr/lib/x86_64-linux-gnu/libboost_system.so.1.65.1
And:
$ sudo apt install libboost-all-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
libboost-all-dev is already the newest version (1.65.1.0ubuntu1).
I have previously used a self-compiled 1.63.0, but I have removed all the files from /usr/local/lib and /usr/local/include. Maybe it still somehow haunts here..?
If I find Boost like this (without system):
find_package(Boost 1.63.0 REQUIRED)
..then configuring succeeds, but won't link to ${Boost_SYSTEM_LIBRARY}:
/usr/bin/ld: src/app/CMakeFiles/app.dir/application.cpp.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
//usr/lib/x86_64-linux-gnu/libboost_system.so.1.65.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

The problem was that you should require system instead of SYSTEM even though it complains about missing boost_system also when SYSTEM was used. I find that a bit confusing:
find_package(Boost 1.63.0 REQUIRED system)

Related

Cannot find 'gflags/gflags.h' while building library OSX

I'm trying to build the library which included 'gflags/gflags.h' and am having trouble getting it to find.
I installed gflags and glog with homebrew.
CMake output says:
-- Found installed version of gflags: /usr/local/lib/cmake/gflags
-- Detected gflags version: 2.2.2
-- Found Gflags: /usr/local/include
-- Found Glog: /usr/local/include
While running "cmake" everything is okay there is no error. But when I run "make install" it cannot build and it says "fatal error: 'gflags/gflags.h' file not found"
How can I build the library which requires gflags in OsX?
First, find your package
find_package(Gflags REQUIRED)
find_package(Glog REQUIRED)
And then assign these library's header files to your executable include path
include_directories(${GLOG_INCLUDE_DIRS} ${GFLAGS_INCLUDE_DIRS})
Make sure that these variables are set properly
message(STATUS "GFLAGS include path: ${GFLAGS_INCLUDE_DIRS}")

Building Point Cloud Library (PCL) on macOS X Mojave error: ld: library not found for -lflann

I'm following a bunch of tutorials to install PCL... I'm getting the following error:
ld: library not found for -lflann
I installed pcl using brew install pcl. Here's my CMakeList.txt
set(PCL_DIR "/usr/local/Cellar/pcl/1.9.1/share/pcl-1.9/")
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(pcd_write)
set(PCL_DIR "/usr/local/Cellar/pcl/1.9.1/share/pcl-1.9/")
find_package(PCL 1.9 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (pcd_write pcd_write.cpp)
target_link_libraries (pcd_write ${PCL_LIBRARIES})
cmake runs fine and creates the Makefile. When running make, I get:
[ 50%] Linking CXX executable pcd_write
ld: library not found for -lflann
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [pcd_write] Error 1
make[1]: *** [CMakeFiles/pcd_write.dir/all] Error 2
make: *** [all] Error 2
Is there an updated, definitive guide to install PCL on macOS Mojave? Thanks..!
I have the same problem (when building pcl as part of ROS Kinetic on macOS Mojave). My workaround was opening link.txt in all the relevant subfolder of CMakeFiles and doing the following replacements:
Replacing -lflann by /usr/local/lib/libflann.dylib
Replacing -lflann_cpp by /usr/local/lib/libflann_cpp.dylib
Please note that these may occur several times in some files. As I use catkin for the entire build process, my location of these files will probably be slightly different from yours.
This solution is admittedly very hacky and I am sure there is a more proper way of doing this but it made the build work.
Maybe the best way to solve this problem is to download pcl and compile it by yourself. Seems that pcl(1.9.1_4) from brew has wrong links to FLANN(1.9.1_7)
Anyway we can hack this... by modifying FindFLANN.cmake file in '/usr/local/Cellar/pcl/1.9.1_4/share/pcl-1.9/Modules'
At the beginning of the file, add:
set(FLANN_LIBRARIES /usr/local/Cellar/flann/1.9.1_7/lib/libflann.dylib /usr/local/Cellar/flann/1.9.1_7/lib/libflann_cpp.dylib)
set(FLANN_FOUND TRUE)
Hope this will help if you are fighting for building ROS on Mac :)
I had built it successfully with following steps on Mojave 10.14
step 1
brew install boost
brew install eigen
brew install flann
brew install vtk
step 2
git clone https://github.com/PointCloudLibrary/pcl.git
mkdir BUILD
cd BUILD
cmake ..
make
sudo make install

references undefined when linking against boost 1.63 static libs

I'm linking against boost statically. When I do so, I get some undefined reference errors (below).
[100%] Linking CXX executable infiniteTests
/home/wdog/src/protos/infinite/libinfinite.a(ServiceDiscovery.cpp.o): In function `boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::simple_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::ostream&)':
/home/wdog/src/3rdp/boost_1_63_0/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
/home/wdog/src/3rdp/boost_1_63_0/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
collect2: error: ld returned 1 exit status
When I link dynamically, the references are presumably present, and the build links successfully.
I need to be able to build this statically; requirement of the project.
I'm building with cmake. My link directories are ordered thus:
link_directories(
$ENV{PROJECT_LIB_DIR}
$ENV{BOOST_LIBRARY_DIR}
$ENV{HIREDIS_LIB_DIR}
$ENV{LIBEVENT_LIB_DIR}
$ENV{PROJECT_ROOT}
/usr/lib
/usr/local/lib
)
My target link libaries are defined as:
target_link_libraries(
${sThisProject}
${Boost_LIBRARIES}
libhiredis.a
libevent.a
libevent_core.a
libevent_pthreads.a
libssl.so
libcrypto.so
libpthread.so.0
)
Here's the CMakeLists.txt entry where I find boost:
find_package( Boost 1.63 EXACT REQUIRED COMPONENTS system chrono date_time filesystem program_options regex thread REQUIRED )
I also set the following:
SET (Boost_NO_SYSTEM_PATHS ON)
SET (Boost_USE_STATIC_LIBS ON)
SET (Boost_USE_MULTITHREADED ON)
SET (Boost_USE_STATIC_RUNTIME ON)
I've checked that my build of 1.63 has successfully built the appropriate static lib - libboost_date_time.a - and it's present in ${BOOST}/stage/lib/
It's my understanding that this can happen if the linker finds a boost static lib on the system install before the correct path.
Cmake appears to find the correct path for boost 1_63:
(output during build:)
-- Boost version: 1.63.0
-- Found the following Boost libraries:
-- system
-- chrono
-- date_time
-- filesystem
-- program_options
-- regex
-- thread
I run fedora 25, so my system installed version of boost was 1_60, so to avoid the above problem, I uninstalled it completely:
sudo dnf remove boost boost-atomic boost-chrono boost-container boost-context boost-coroutine boost-date-time boost-devel boost-filesystem boost-graph boost-iostreams boost-locale boost-log boost-math boost-program-options boost-python boost-random boost-regex boost-serialization boost-signals boost-system boost-test boost-thread boost-timer boost-type_erasure boost-wave
I built but do not install 1.63 in a 3rd party directory in my source tree. It's built with the following commands:
./bootstrap.sh
./b2 -j8
I set linker output with:
SET (CMAKE_EXE_LINKER_FLAGS -v)
And determined via the following output that I'm linking against the correct static libary:
-o
...
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_system.a
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_chrono.a
**/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_date_time.a**
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_filesystem.a
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_program_options.a
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_regex.a
/home/wdog/src/3rdp/boost_1_63_0/stage/lib/libboost_thread.a
...
I've run out of ideas; is there something else I should check?
That libinfinite.a needs to precede the boost libraries in the command line.

Error while importing Boost 1.61.0 to C++ project

I tried to import Boost 1.61.0 (downloaded from SourceForge - Boost 1.61.0 as .7z), but failed.
Console:
"D:\Program Files (x86)\JetBrains\CLion 2016.2\bin\cmake\bin\cmake.exe" --build C:\Users\Marczak\.CLion2016.2\system\cmake\generated\WsServer-e351c9f9\e351c9f9\Debug --target WsServer -- -j 4
[ 50%] Linking CXX executable WsServer.exe
CMakeFiles\WsServer.dir\build.make:96: recipe for target 'WsServer.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/WsServer.dir/all' failed
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0':
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [WsServer.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/WsServer.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/WsServer.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/WsServer.dir/rule' failed
mingw32-make.exe: *** [WsServer] Error 2
Makefile:117: recipe for target 'WsServer' failed
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(WsServer)
set(BOOST_ROOT "C:/Users/Marczak/boost_1_61_0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(SOURCE_FILES src/main.cpp)
find_package(Boost)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(WsServer ${SOURCE_FILES})
If I do find_package(Boost 1.61.0 COMPONENTS system filesystem REQUIRED) I get:
Error: Unable to find the requested Boost libraries.
Boost version: 1.61.0
Boost include path: C:/Users/Marczak/boost_1_61_0
Could not find the following static Boost libraries:
boost_system boost_filesystem
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.
I tried to set Boost_USE_STATIC_LIBRARIES on, but it failed too. I use CLion 2016.2.
UPDATE: I tried older versions too. Same error. What's inside the .7z:
In other topics I see lib folder. But here I don't see it. What I should put in BOOST_LIBRARYDIR?
UPDATE 2: Installed binary from https://sourceforge.net/projects/boost/files/boost-binaries/1.61.0/ . I noticed there's new folder: lib64-msvc-14.0. It contains many .dll and .lib files, e.g. boost_atomic-vc140-mt-1_61.dll.
Boost.org says:
If you plan to use your tools from the Windows command prompt, you're in the right place. If you plan to build from the Cygwin bash shell, you're actually running on a POSIX platform and should follow the instructions for getting started on Unix variants. Other command shells, such as MinGW's MSYS, are not supported—they may or may not work.
I'll try using Cygwin.
If you're new to C++, I suggest you to download MinGW distribution maintained by Stephan T. Lavavej (Microsoft C++ developer): https://nuwen.net/mingw.html. It, among other tools and libraries, contains pre-built boost binaries. Unpack it and specify the path to it via Settings | Build, Execution, Deployment | Toolchains.
After that you should be able to compile the program with the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(WsServer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(SOURCE_FILES src/main.cpp)
find_package(Boost REQUIRED COMPONENTS filesystem)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(WsServer ${SOURCE_FILES})
target_link_libraries(WsServer ${Boost_LIBRARIES})
Don't forget to drop CMake cache as find_packages doesn't update successful results due to performance reasons (in CLion it can be done via Cmake toolbar | Cache | red arrows icon).
Some additional remarks:
Boost_USE_STATIC_LIBRARIES is not meant to be set manually, it is set by running find_package(Boost), which uses BOOST_ROOT or BOOST_INCLUDEDIR + BOOST_LIBRARYDIR, you should set those if required. You don't have to do it with the MinGW distro I've linked because it already has boost includes and libraries in accessible locations.
You can check that the paths to libraries are correct by looking at Boost_* variables in CMake cache.
libs directory inside boost sources is unrelated to the problem, it doesn't conitain any binaries
You've downloaded boost binaries built with Visual Studio toolchain, not MinGW, so they are incompatible with your setup. If you don't want to use MinGW package I've linked, you have to either find boost binaries built with correct MinGW version or build it yourself.

problems with cmake and boost-signals

I'm using cmake for project with boost, I've set boost-signals as required and cmake states it finds it, but when I compile the project I get a linking error.
I can resolve it with a linker directive set(CMAKE_EXE_LINKER_FLAGS -lboost_signals) but
this seems contrary to what it should be doing. Can someone suggest a better way? Thanks
in CMakeLists.txt
IF(UNIX)
find_package(Boost COMPONENTS system filesystem random regex signals thread program_options date_time REQUIRED)
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} )
ENDIF()
Running cmake:
$ cmake ../
-- Boost version: 1.49.0
-- Found the following Boost libraries:
-- system
-- filesystem
-- random
-- regex
-- signals
-- thread
-- program_options
-- date_time
-- Configuring done
-- Generating done
-- Build files have been written to: /home/me/myproject/build
Linker error:
/usr/bin/ld: /usr/local/lib/libwt.so: undefined reference to symbol '_ZN5boost7signals9trackableD2Ev'
/usr/bin/ld: note: '_ZN5boost7signals9trackableD2Ev' is defined in DSO /usr/lib/libboost_signals.so.1.49.0 so try adding it to the linker command line
/usr/lib/libboost_signals.so.1.49.0: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
Boost Signals is not a header-only library. In addition to giving the include directory, you need to tell CMake to link against it too.
add_executable(MyProgram [...] )
target_link_libraries(MyProgram ${Boost_LIBRARIES})
In case you plan to build on Windows as well, you might also want to disable auto-linking:
add_definitions(-DBOOST_ALL_NO_LIB)
Note that Boost Signals has been deprecated and replaced by Signals2, which is in fact a header-only library. If it is up to you to make the choice, consider using Signals2 instead.