CLion 1.2, with bundled CMake 3.3.2 and MinGW-w64 4.8.4
I need to get a single DLL in a result of building that no need any other libraries to work. But can't link Boost libraries statically. I bootstrapped and built Boost with corresponding MinGW.
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_ROOT "..\\lib\\boost_1_59_0")
set(Boost_USE_STATIC_LIBS ON)
set(BOOST_COMPONENTS_NEEDED filesystem )
find_package(Boost 1.59.0 REQUIRED COMPONENTS ${BOOST_COMPONENTS_NEEDED})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories(${Boost_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--kill-at -static-libgcc -static-libstdc++")
add_library(${CMAKE_PROJECT_NAME} SHARED ${SOURCE_FILES})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}\\..\\..\\output")
target_link_libraries(${CMAKE_PROJECT_NAME} ${Boost_LIBRARIES})
Build output:
O:/SampleProject/Cpp/lib/boost_1_59_0/stage/lib/libboost_filesystem-mgw48-mt-d-1_59.a(operations.o): In function error':
O:\SampleProject\Cpp\lib\boost_1_59_0/libs/filesystem/src/operations.cpp:286: undefined reference toboost::system::system_category()'
What else should I do to link with boost?
UPDATE: there is a list of built libraries
libboost_filesystem-mgw48-1_59.a
libboost_filesystem-mgw48-d-1_59.a
libboost_filesystem-mgw48-mt-1_59.a
libboost_filesystem-mgw48-mt-d-1_59.a
libboost_filesystem-mgw48-mt-s-1_59.a
libboost_filesystem-mgw48-mt-sd-1_59.a
libboost_filesystem-mgw48-s-1_59.a
libboost_filesystem-mgw48-sd-1_59.a
libboost_system-mgw48-1_59.a
libboost_system-mgw48-d-1_59.a
libboost_system-mgw48-mt-1_59.a
libboost_system-mgw48-mt-d-1_59.a
libboost_system-mgw48-mt-s-1_59.a
libboost_system-mgw48-mt-sd-1_59.a
libboost_system-mgw48-s-1_59.a
libboost_system-mgw48-sd-1_59.a
This looks like a linker error suggesting that you are not linking to Boost::system
You need to add system to BOOST_COMPONENTS_NEEDED. Change this line and see if it helps
set(BOOST_COMPONENTS_NEEDED system filesystem )
Related
I'm developing a cpp project in Clion. For the build I use cmake. Last weekend I upgraded from ubuntu 18.04 to 20.04, from then I cannot build my project and I get the error:
/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'. It seems that something in the linking isn't right. I reinstalled all packages, tried different versions but nothing. My CMakeLists.txt is:
cmake_minimum_required(VERSION 3.7)
project(pki_cpp)
set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
#find_package(libmongocxx REQUIRED)
find_package(mongocxx REQUIRED)
find_package(bsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories(SYSTEM ./lib)
set(BOOST_ROOT /usr/lib/)
find_package(Boost 1.71.0 REQUIRED COMPONENTS system chrono thread filesystem)
include_directories(${Boost_INCLUDE_DIR})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
# A custom library
add_subdirectory(asnDatatypes)
include_directories({CMAKE_CURRENT_SOURCE_DIR} asnDatatypes)
set_property(TARGET asnLibrary PROPERTY POSITION_INDEPENDENT_CODE ON)
# end custom library
#cryptopp library
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/contrib/cmake")
find_package(CryptoPP REQUIRED)
include_directories(${CRYPTOPP_INCLUDE_DIR})
link_directories(${CryptoPP_INCLUDE_DIR})
# end cryptopp
add_executable(pki_cpp main.cpp rootCA.cpp rootCA.h)
target_include_directories(pki_cpp PRIVATE ${Boost_INCLUDE_DIRS} ${LIBMONGOCXX_INCLUDE_DIRS} )
link_directories(pki_cpp ${Boost_LIBRARY_DIRS})
target_link_libraries(pki_cpp ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_LIBRARIES} Boost::system Threads::Threads boost_system asnLibrary OpenSSL::SSL ${CRYPTOPP_LIBRARIES} ${LIBMONGOCXX_LIBRARIES} mongo::bsoncxx_shared mongo::mongocxx_shared ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} Boost::system)
Any help is appreciated!
That's a lot of stuff, all randomly ordered and commented out. The obvious error is a failure to link Boost System
I'd go with
FIND_PACKAGE(Boost 1.65.0
COMPONENTS system program_options REQUIRED)
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${Boost_INLUDE_DIRS})
LINK_LIBRARIES(${Boost_LIBRARIES})
Alternatively you can be specific and link specific libs to specific targets
TARGET_LINK_LIBRARIES(mylib ${Boost_SYSTEM_LIBRARY})
TARGET_LINK_LIBRARIES(myexecutable ${Boost_THREAD_LIBRARY})
I have C++ project which compiles using cmake.
I'm trying to link library using relative path and not absolute path. The code compiles fine, but has problem while linking the libraries.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(GMW)
cmake_policy(SET CMP0015 NEW)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -maes -mpclmul -fPIC")
SET (BOOST_ROOT $ENV{HOME}/boost_1_60_0/)
SET (BOOST_INCLUDEDIR $ENV{HOME}/boost_1_60_0/)
SET (BOOST_LIBRARYDIR $ENV{HOME}/boost_1_60_0/stage/lib)
find_package(Boost COMPONENTS system thread REQUIRED)
INCLUDE_DIRECTORIES($ENV{HOME} ${BOOST_INCLUDEDIR}
../../lib/OTExtensionBristol ../../lib/)
link_directories(/usr/ssl/lib/ ../../install/lib ${BOOST_LIBRARYDIR})
set(SOURCE_FILES main.cpp GMWParty.h Circuit.cpp Circuit.h MPCCommunication.cpp MPCCommunication.h GMWParty.cpp)
add_executable(GMW ${SOURCE_FILES})
add_library(gmw ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(GMW ../../scapi.a gmp
OTExtensionBristol
../../install/lib/libsimpleot.a
boost_system boost_thread pthread crypto dl ssl z)
cmake_policy(SET CMP0015 NEW) Solved the problem for link directories.
How do I solve it for link libraries?
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.
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")
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})