Cmake error when adding Boost library - c++

I am trying to add Boost library to my project using the CMakeLists.txt in the follwing way:
set(BOOST_INCLUDEDIR "C:/boost_1_57_0")
set(BOOST_LIBRARYDIR "C:/boost_1_57_0/stage/lib")
find_package(Boost 1.57.0 COMPONENTS filesystem)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(test test.cpp)
target_link_libraries(test ${Boost_LIBRARIES})
However, I get the followng error: LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc120-mt-1_57.lib'
libboost_filesystem-vc120-mt-1_57.lib is located in the stage/lib folder, so I don't know what is going on. I am compiling with Visual Studio 2013.
Any thoughts?

Try setting the Boost_USE_STATIC_LIBS and Boost_USE_MULTITHREADED CMake variables to ON before using find_package, i.e.:
set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED ON )
find_package( Boost 1.57.0 COMPONENTS filesystem )
I've come across this problem before and it seems as though, on multithreaded windows systems, the Boost bootstrap installer compiles multithreaded, static libraries by default. However, the CMake FindBoost script (which is used by find_package) searches for single-threaded, dynamic libraries by default.

Since you're using VS compiler I'll say you're working on Windows.
The error refers to the linker, which is failing to find boost libraries, as noticed.
Taking into account that the library exists in the boost path, my solution was to do a file(COPY) for the specific library, as a last resort.
if(WIN32)
set(BOOST_ROOT "C:/boost_1_57_0")
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/stage/lib/)
endif()
find_package(Boost 1.57.0 EXACT REQUIRED system filesystem)
if(Boost_FOUND)
message(STATUS "found boost, Boost_LIBRARIES <" ${Boost_LIBRARIES} ">")
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
else()
message(STATUS "boost not found")
endif()
target_link_libraries(boost_test ${Boost_LIBRARIES})
file(COPY "${Boost_LIBRARY_DIRS}/boost_filesystem-vc120-mt-1_57.dll" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
You may add some log messages to the CMake in order to know the values returned in the
find_package.

Make sure the architecture (x64) matches.
$ cmake -A x64 ..
Use link_directories command before adding executables just like include_directories.
link_directories(${Boost_LIBRARY_DIRS})

Related

Unable to find the requested Boost libraries boost_lboost_thread

I have been trying to run a Qt project in a CMake environment. The Qt project is using openCV and Boost dependencies. After successfully solving all compiling errors, I have been struggling with a Boost error.
As soon as I run CMake I get the following error:
Running "/usr/bin/cmake /home/labrat/Desktop/cam-proc '-GCodeBlocks - Unix Makefiles'" in /home/labrat/Desktop/cam-proc/build.
**CMake Error at /usr/share/cmake-3.5/Modules/FindBoost.cmake:1677 (message):**
Unable to find the requested Boost libraries.
Boost version: 1.58.0
Boost include path: /usr/include
Could not find the following Boost libraries:
boost_lboost_thread
Some (but not all) of the required Boost libraries were found. You may
need to install these additional Boost libraries. Alternatively, set
BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
to the location of Boost.
**Call Stack (most recent call first):
src/libCam/CMakeLists.txt:7 (find_package)**
Boost version: 1.58.0
Found the following Boost libraries:
system
thread
filesystem
chrono
date_time
atomic
-- Configuring incomplete, errors occurred!
See also "/home/labrat/Desktop/cam-proc/build/CMakeFiles/CMakeOutput.log".
See also "/home/labrat/Desktop/cam-proc/build/CMakeFiles/CMakeError.log".
*** cmake process exited with exit code 1.
I analyzed the error given CMake Error at /usr/share/cmake-3.5/Modules/FindBoost.cmake:1677 (message): and opened the enormous FindBoost.cmake file and here is the part where the error is, according to the warning, located:
message(SEND_ERROR "Unable to find the requested Boost libraries.\n${Boost_ERROR_REASON}")
# Add pthread library on UNIX if thread component was found
_Boost_consider_adding_pthreads(Boost_LIBRARIES ${Boost_LIBRARIES})
else()
if(Boost_FIND_REQUIRED)
message(SEND_ERROR "Unable to find the requested Boost libraries.\n${Boost_ERROR_REASON}")
else()
if(NOT Boost_FIND_QUIETLY)
# we opt not to automatically output Boost_ERROR_REASON here as
# it could be quite lengthy and somewhat imposing in its requests
# Since Boost is not always a required dependency we'll leave this
# up to the end-user.
if(Boost_DEBUG OR Boost_DETAILED_FAILURE_MSG)
message(STATUS "Could NOT find Boost\n${Boost_ERROR_REASON}")
else()
message(STATUS "Could NOT find Boost")
endif()
endif()
endif()
endif()
Also since another part of the error message said Call Stack (most recent call first): src/libCam/CMakeLists.txt:7 (find_package) here I am attaching the CMakeLists.txt realted to the libcam project if that's helpful:
cmake_minimum_required (VERSION 2.8.12)
project(libCam)
find_package(Qt5Widgets REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Boost COMPONENTS filesystem lboost_thread system REQUIRED)
find_package(Boost COMPONENTS system thread filesystem REQUIRED)
find_package(Qt5PrintSupport REQUIRED)
###
# make sure we use c++11
###
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
INCLUDE_DIRECTORIES(${Qt5Widgets_INCLUDE_DIRS})
include_regular_expression("^([^b]|b[^o]|bo[^o]|boo[^s]|boos[^t]|boost[^/]).*$")
qt5_wrap_ui (UIS_HDRS qtinclude/imagemanager.ui qtinclude/stereomanager.ui qtinclude/stereolistwidget.ui qtinclude/connectionmenu.ui)
file(GLOB LIBCAM_SRCS
"include/*.h"
"include/*.cpp"
"include/*.hpp"
"qtinclude/*.h"
"qtinclude/*.cpp"
"qtinclude/*.hpp"
)
file(GLOB UI_RC
"qtinclude/qdarkstyle/*.qrc"
)
add_library(libCam SHARED ${LIBCAM_SRCS} ${UIS_HDRS} ${UI_RC})
target_include_directories (libCam PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
target_link_libraries (libCam Qt5::Widgets Qt5::PrintSupport Qt5::Core Qt5::Quick Qt5::Sql Qt5::XmlPatterns ${Boost_LIBRARIES} ${OpenCV_LIBS} )
I have been googling the error for the past two days looking for information and code to solve this problem, but I am running out of ideas. I tried to download boost 1.55 but it didn't work as the compiler only sees boost 1.58.
Any idea on how to shed light on this matter? Is there something I am leaving out?
The problem is the line
find_package(Boost COMPONENTS filesystem lboost_thread system REQUIRED)
which should be changed to
find_package(Boost COMPONENTS filesystem thread system REQUIRED)
or simply deleted, since it is then equivalent to the following line.

CMake with Boost 1.62.0: "No Boost libraries were found"

Using the following CMake code:
cmake_minimum_required(VERSION 3.6)
project(TrustLineManager)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_ROOT D:/Tools/boost_1_62_0/)
set(Boost_INCLUDE_DIRS D:/Tools/boost_1_62_0/boost)
set(Boost_LIBRARY_DIRS D:/Tools/boost_1_62_0/libs)
set(BOOST_INCLUDEDIR C:/MinGW/include)
set(BOOST_LIBRARYDIR C:/MinGW/lib)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost
1.62.0
COMPONENTS system
filesystem
REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(TrustLineManager ${Boost_LIBRARIES})
I get this error:
Error:Unable to find the requested Boost libraries.
Boost version: 1.62.0
Boost include path: D:/Tools/boost_1_62_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.
What should I do to solve it?
EDIT:
I modified my CMake code into:
cmake_minimum_required(VERSION 3.6)
project(TrustLineManager)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
set(BOOST_ROOT "D:/Tools/boost_1_62_0")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(TrustLineManager ${SOURCE_FILES})
target_link_libraries(TrustLineManager Boost::filesystem Boost::thread)
I have now this error:
Error:Target "TrustLineManager" links to target "Boost::filesystem" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?
Here is a screenshot of Boost directory contents:
Obviously CMake finds Boost as it is able to detect its version (1.62.0).
CMake uses the file FindBoost.cmake to determine what libraries to link against.
This file is updated continuously when new boost versions appear.
There, support for Boost 1.62.0 is available only for CMake >= 3.7.0.
So just update your CMake version (or just the file FindBoost.cmake) and you will be fine.

How to link boost using cmake on windows

the StackOverFlow community!
I'm trying to link up boost libraries and there's an error of linking up the 'boost_system' static library.
I use cmake 2.8, the MinGW compiler and the 1.55.0 boost.
Judging by the logs, cmake doesn't see the pathway to the libraries.
Here's the error:
Unable to find the requested Boost libraries.
Boost version: 1.55.0
Boost include path: c:/local/boost_1_55_0
Could not find the following static Boost libraries:
boost_system
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.
Call Stack (most recent call first):
Here's the cmake code:
cmake_minimum_required(VERSION 2.8)
project(test)
if (WIN32)
set(BOOST_ROOT "c:/local/boost_1_55_0")
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/stage/lib/)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.55 COMPONENTS system REQUIRED)
add_definitions(${Boost_LIB_DIAGNOSTIC_DEFINITIONS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
include_directories(${Boost_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
add_executable(test ${SOURCE_FILES})
target_link_libraries(test ${Boost_LIBRARIES})
endif (WIN32)
I receive logs like this with the declared set(Boost_DETAILED_FAILURE_MSG on)
status** Boost Include: c:/local/boost_1_55_0
status** Boost Libraries:
status** Boost Libraries:
For me worked setting hint variables CMAKE_INCLUDE_PATH & CMAKE_LIBRARY_PATH. BOOST_ROOT & BOOST_LIBRARYDIR were not working also.
Another hint variables are Boost_COMPILER, Boost_NAMESPACE, Boost_ARCHITECTURE they help to define how your library is named to boost search script.
Using SET(Boost_DEBUG 1) helped me a lot do diagnose a problem -as it prints Boost path suffixes= and _boost_LIBRARY_SEARCH_DIRS_RELEASE
For example
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "D:/boost/boost_1_70_0")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "D:/boost/boost_1_70_0/stage/gcc/x64/lib")
SET(Boost_COMPILER ${Boost_COMPILER} "-mgw73")
SET(Boost_NAMESPACE ${Boost_NAMESPACE} "libboost")
SET(Boost_ARCHITECTURE "-x64")
Or
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "D:/boost/boost_1_70_0")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "D:/boost/boost_1_70_0/stage/lib")
SET(Boost_COMPILER ${Boost_COMPILER} "-vc142")

error: magic.h: No such file or directory (MacOS X V 10.6.8)

I am developing a project called "Duplicate File Finder" where I also want to extract the information about the type of the file i.e. png, zip, executable etc. I found out that libmagic (magic.h) can be used for finding filetype. But the problem I am facing for quite some time is that magic.h is not recognized when I compile the module. My project uses 'cmake' build system. I defined all the parameters in CMakeLists.txt and even it is not giving error when I am finding path and library for magic as it is installed in my system. I can do 'man libmagic' and it works. Following is my cmake file. Following is my CMakeLists.txt file.
cmake_minimum_required(VERSION 2.8)
project( duplicateFinder )
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(USE_OPENSSL TRUE)
find_package(Boost 1.53.0 COMPONENTS system filesystem regex thread REQUIRED)
find_package(OpenSSL)
FIND_PATH(SQLITE3_INCLUDE_DIR sqlite3.h)
find_library(SQLITE3_LIBRARY sqlite3)
FIND_PATH(MAGIC_INCLUDE_DIR magic.h)
find_library(MAGIC_LIBRARY magic $ENV{MAGIC_LIB_PATH})
if(NOT MAGIC_INCLUDE_DIR)
message(SEND_ERROR "Failed to find magic.h")
endif()
if(NOT MAGIC_LIBRARY)
message(SEND_ERROR "Failed to find the magic library")
endif()
if(NOT SQLITE3_INCLUDE_DIR)
message(SEND_ERROR "Failed to find sqlite3.h")
endif()
if(NOT SQLITE3_LIBRARY)
message(SEND_ERROR "Failed to find the sqlite3 library")
endif()
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
SET(duff_SOURCES src/run_duff.cpp src/sys_scan.cpp src/duff_utils.cpp)
SET(duff_HEADERS src/run_duff.h src/sys_scan.h src/duff_utils.cpp)
add_executable(bin/dup_finder ${duff_SOURCES} ${duff_HEADERS})
add_definitions( -DBOOST_ALL_NO_LIB )
add_definitions( -DBOOST_ALL_DYN_LINK )
target_link_libraries(bin/dup_finder ${Boost_LIBRARIES} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_THREAD_LIBRARY} ${OPENSSL_LIBRARIES} ${SQLITE3_LIBRARY} ${MAGIC_LIBRARY} )
endif()
I did an intensive research on Internet but was not able to solve the problem. I hope the problem statement is understandable. Can anybody help me in this regards. Thank you in advance.
You need to add
include_directories(${MAGIC_INCLUDE_DIR})
Additional comments: Since boost is REQUIRED, CMake will fail if it is not found, so it is a bit weird to wrap everything inside an if(Boost_FOUND).
It's doubly confusing since you link the non-boost libraries inside this if as well…
Also, you should add ${SQLITE3_INCLUDE_DIR} to the include_directories as well, since on another machine, they may be in a place not already included.

Boost log, GCC 4.4 and CMake

I am trying to get a simple boost.log example running on Linux using GCC 4.4.5, CMake 2.8.2 and Boost 1.53.0.
Compiling boost and boost log succeeded, but I keep getting issues when linking my test program to boost.log.
I use the following CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8)
project(QuantibBoostLogTest)
# Include boost headers
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Threads)
find_package(Boost 1.53.0 COMPONENTS thread date_time filesystem system log log_setup REQUIRED)
if(Boost_FOUND)
include_directories( ${Boost_INCLUDE_DIRS} )
link_libraries(${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
else(Boost_FOUND)
message(FATAL_ERROR "Cannot build Quantib Boost Log test without Boost. Please set Boost_DIR.")
endif(Boost_FOUND)
add_executable(quantibBoostLogTest boost_log_test.cxx)
install(TARGETS quantibBoostLogTest DESTINATION .)
CMake does detect the boost libraries correctly, but I still get linker errors, mostly of the form:
core.cpp:(.text+0x1b0e): undefined reference to `boost::detail::get_tss_data(void const*)'
I do link the thread libraries. Does anybody know how to solve this?
It seems like boost.log depends on boost.thread library then you need change order of libraries. See why link order does matter
Try following order
find_package(Boost 1.53.0 COMPONENTS log log_setup thread date_time filesystem system REQUIRED)
if it will not help try include them two times as following
link_libraries(${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${Boost_LIBRARIES})
The linker error you give has something to do with either not linking against a native threading library like pthreads and/or boost_thread. (or both)
1)
From what I see you don't link against pthreads library.
By merely calling a CMake custom module that tries to find the library doesn't mean it'll also link against it.
Try and do:
SET(CMAKE_THREAD_PREFER_PTHREAD true)
FIND_PACKAGE (Threads)
IF(Threads_FOUND)
INCLUDE_DIRECTORIES(SYSTEM ${Threads_INCLUDE_DIR})
MESSAGE("Are we using pthreads? ${CMAKE_USE_PTHREADS_INIT}")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
ENDIF()
Check the FindThreads.cmake file of the CMake installation you have for more information regarding the use of the threads module.
You can usually find it in /usr/share/cmake-2.8/Modules/
2) Maybe the ordering of the linked Boost libraries is incorrect or the version you specified for Boost is invalid.
Try changing the boost version or don't specify it at all or change the order of the linked libraries
SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE(Boost 1.53.0 COMPONENTS **system thread filesystem date_time log log_setup** REQUIRED)
IF(Boost_FOUND)
INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIR})
LINK_DIRECTORIES(${Boost_LIBRARY_DIR})
MESSAGE("Boost information")
MESSAGE("Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
MESSAGE("Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
MESSAGE("Boost Libraries: ${Boost_LIBRARIES}")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_LIBRARIES})
ENDIF()
(The second contention might be completely wrong as I think the ordering of the elements specified after COMPONENTS in FIND_PACKAGE doesn't matter)