I am aware this question was asked many times before, always ending with the same answer... add -DBOOST_LOG_DYN_LINK to your CMakeLists.txt file.
However, I've had this in my cmake for a long time and everything was linking without any problems. Now I decided to switch to Ubuntu 18.04 from 16.04 and update my projects one by one....
This is my cmake file:
cmake_minimum_required(VERSION 2.8.4)
project(FOO)
find_package(Boost REQUIRED COMPONENTS system timer filesystem log program_options unit_test_framework)
find_package(CGAL COMPONENTS Core)
find_library(OPEN_MESH_CORE_LIBRARY OpenMeshCore /usr/local/lib/OpenMesh REQUIRED)
find_library(YAML_CPP_LIBRARY yaml-cpp REQUIRED)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp)
link_libraries(${JSONCPP_LIBRARIES})
include_directories(${JSONCPP_INCLUDE_DIRS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffast-math -fopenmp -msse2 -march=native -W -Wall -std=c++11")
add_definitions(-DBOOST_LOG_DYN_LINK=1)
add_definitions(-DUNIT_TEST_DATA="${CMAKE_SOURCE_DIR}/data")
include( ${CGAL_USE_FILE} )
include( CGAL_CreateSingleSourceCGALProgram )
set(SOURCE_FILES
Foo1.cpp
Foo2.cpp
Foo3.cpp)
add_executable(FOO main.cpp ${SOURCE_FILES})
target_include_directories(FOO PRIVATE ${JsonCpp_INCLUDE_DIRS})
target_link_libraries(FOO ${OPEN_MESH_CORE_LIBRARY} ${Boost_LIBRARIES} ${YAML_CPP_LIBRARY} ${JSONCPP_LIBRARIES})
add_library(foo SHARED ${SOURCE_FILES})
target_link_libraries(foo ${OPEN_MESH_CORE_LIBRARY} ${Boost_LIBRARIES} ${YAML_CPP_LIBRARY} ${JSONCPP_LIBRARIES})
add_executable(tests test.cpp ${SOURCE_FILES})
target_include_directories(FOO PRIVATE ${JsonCpp_INCLUDE_DIRS})
target_link_libraries(tests ${OPEN_MESH_CORE_LIBRARY} ${Boost_LIBRARIES} ${YAML_CPP_LIBRARY} ${JSONCPP_LIBRARIES})
When compiled, it fails with known long list of problems, all related ending with a message
undefined reference to boost::log::v2_mt_posix::...
I am not a cmake ninja and it is very likely that I am doing something wrong, however, I can not find out what it is.
EDIT:
I have successfully tried to build it on a clean Ubuntu 16.04 with gcc 5 and boost 1.58
I have unsuccessfully tried to build it on a clean 18.04 with gcc7.1
and boost 1.65
I have unsuccessfully tried to build it on a clean 18.04 with gcc 5.5
and self compiled boost 1.58
I have unsuccessfully tried to build it on a clean 18.04 with gcc 5.5
and self compiled boost 1.65
All attempts followed an exactly the same procedure.
Cause of this problem wasn't in Boost or CMakeLists.txt. It was actually a problem of CGAL calling find_package(Boost) again somewhere in their use protocol when include( ${CGAL_USE_FILE} ) was invoked, and ended up overriding Boost_LIBRARIES with links to it's own components, completely omitting what I've found previously.
Here is the reported issue
Solution is somewhat dirty as I'didn't patch CGAL up. All it took was changing the order when i call include( ${CGAL_USE_FILE} ) and placing it at the top.
find_package(CGAL COMPONENTS Core)
include( ${CGAL_USE_FILE} )
find_package(Boost REQUIRED COMPONENTS system timer filesystem log
program_options unit_test_framework)
Please NOTE that this is a quick fix and can result in further problems such as overriding Boost components required by CGAL!
Related
I have an undefined reference while trying to compile my Qt5 project.
I am usually pretty confident with using CMake but this time I have a strange error that I can't figure out:
undefined reference to boost::system::generic_category()
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to 'boost::system::generic_category()'
My configurations are:
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Qt5.15
cmake version 3.16.3
After typing whereis boost the outcome wasboost: /usr/include/boost and after applying the great power of dpkg -s libboost-dev | grep 'Version' :) the version is Version: 1.71.0.0ubuntu2
I don't understand what is happening, below an example of how my CMakeLists.txt is structured:
cmake_minimum_required (VERSION 3.1)
project(projectA)
set (OpenCV_DIR /home/to/opencv/build)
find_package( OpenCV REQUIRED )
find_package( Boost COMPONENTS system thread filesystem REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5Widgets)
find_package(Qt5PrintSupport)
#make them into headers
qt5_wrap_ui (UI_HDRS ${UI})
add_executable(projectA main/main.cpp ui/qdarkstyle/style.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (projectA Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport)
add_library(projectA_lib SHARED ${SRCS} ${UI_HDRS})
target_include_directories (projectA_lib PUBLIC "src/" "ui/")
link_directories(${Boost_LIBRARY_DIRS})
target_link_libraries (projectA_lib Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS})
I have searched and applied solutions I saw on all possible sources I was able to find such as:
This source but that didn't work.
Also from here it seems that this solution shall be applied:
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS system)
# the call to include_directories is now useless:
# the Boost::system imported target used below
# embeds the include directories
project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP Boost::system)
However that also didn't do any specific benefits to finding the solution.
Other posts I consulted were this, this one but no answer was provided.
This post was useful but that also didn't provide any advice that didn't already know.
Thanks for pointing to the right direction and trying to find a solution.
For the past few days I've been searching everywhere and can't seem to fix this problem or maybe I just didn't understand some answers.
I have an application (a game to be precise) built with SFML that I managed to compile on Linux with 2.4.2 version. I want to migrate to 2.5.1 and be able to compile on Windows so I changed the CMakeLists.txt file to this:
cmake_minimum_required(VERSION 3.0)
# There is no real need for C++17 or 14, but I like to use up-to-date versions
if(CMAKE_MAJOR_VERSION VERSION_GREATER 3.8.0)
set(CMAKE_CXX_STANDARD 17)
else()
set(CMAKE_CXX_STANDARD 14)
endif(CMAKE_MAJOR_VERSION VERSION_GREATER 3.8.0)
if (WIN32)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Binaries/)
endif(WIN32)
# Includes
include_directories(include)
# Source files
set(SOURCE_FILES
source/main.cpp
source/Game.cpp
)
# Assets
set(ASSETS
assets/Fonts/xolonium.ttf
)
# Debug Mode
project(HexDebug VERSION 1.0 LANGUAGES CXX)
set(CMAKE_BUILD_TYPE DEBUG) # This is ignored by MSVC
#SFML Package
find_package(SFML 2.5 CONFIG COMPONENTS system window graphics network audio REQUIRED)
if(SFML_FOUND)
set(SFML_STATIC_LIBRARIES TRUE)
if(WIN32)
set(SFML_USE_STATIC_STD_LIBS TRUE)
endif(WIN32)
endif(SFML_FOUND)
if(UNIX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Binaries/Debug/)
endif(UNIX)
foreach(asset ${ASSETS})
configure_file(${asset} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${asset} COPYONLY)
endforeach(asset)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_compile_options(${PROJECT_NAME} PRIVATE -g -Wall -O0 -D_DEBUG)
if(SFML_FOUND)
target_link_libraries(${PROJECT_NAME} sfml-system sfml-window sfml-graphics sfml-network sfml-audio)
endif(SFML_FOUND)
#Release Mode
project(Hex VERSION 1.0 LANGUAGES CXX)
set(CMAKE_BUILD_TYPE RELEASE) # This is ignored by MSVC
if(UNIX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Binaries/Release/)
endif(UNIX)
foreach(asset ${ASSETS})
configure_file(${asset} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${asset} COPYONLY)
endforeach(asset)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_compile_options(${PROJECT_NAME} PRIVATE "-O3")
if(SFML_FOUND)
target_link_libraries(${PROJECT_NAME} sfml-system sfml-window sfml-graphics sfml-network sfml-audio)
endif(SFML_FOUND)
The cmake works and generates the VS solution, that I can build with msbuild (note that I don't want to launch Visual Studio, but only use msbuild). But once I try to run it, it asks for dlls and I don't want that.
I thought that would be solved by linking statically (I don't know much about static or dynamic linking) but it still asks for them.
In this link they say that we don't need to include the ${SFML_INCLUDE_DIR} and ${SFML_LIBRARIES} anymore: https://en.sfml-dev.org/forums/index.php?topic=24070.0
How can I avoid that? I want to be completely independant from DLL's or at least be able to include the bin directory installed from SFML compiled and installed binaries (that I compiled, not the precompiled).
Thank you.
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 )
I am new in clion. on gcc i always use:
g++ bin/obj/main.o -o bin/main -lboost_filesystem -lboost_system -lcrypto
How to do it in clion?
It seems my CMakeList does not work:
cmake_minimum_required(VERSION 3.1)
project(motion_simulation)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(motion_simulation ${SOURCE_FILES})
link_libraries(lboost_filesystem)
link_libraries(lboost_system)
link_libraries(lcrypto)
Try including the keyword "CMake" into your search next time. This question is actually not CLion specific because CLion actually uses CMake as buildsystem.
CMake is very well documented, and you should be able to find a lot of answers regarding your problem.
You could first try to get rid of that "l":
link_libraries(boost_filesystem)
If that doesn't work you should take a look how the find_package() command works. http://www.cmake.org/Wiki/CMake:How_To_Find_Libraries
And here is a detailed explanation how to find Boost libs and include directory.
http://www.cmake.org/cmake/help/v3.0/module/FindBoost.html
As you are using CMake 3.1 you can use some more advanced features of CMake.
With CMAKE_CXX_STANDARD you can select which C++ version you want to use and CMake will select the corresponding compiler flags (see docs).
link_libraries is one possibility, but it has to be called before add_executable or add_library. The alternative is target_link_libraries which links only to a single target, but can also manage transitive dependencies (docs).
CMake comes with find_package modules for OpenSSL and Boost to find dependencies and with the option REQUIRED, you can ensure that they are found on the system. Boost also supports COMPONENTS to select which libraries you need.
In case you ever work on a system, where OpenSSL and Boost are not installed in /usr/, you can already use target_include_directories to specify where the headers for your executable is found. Like target_link_libraries, target_include_directories can work with transitive dependencies, in this case PRIVATE.
cmake_minimum_required(VERSION 3.1)
project(motion_simulation)
set(CMAKE_CXX_STANDARD 11)
find_package(Boost REQUIRED COMPONENTS filesystem system)
find_package(OpenSSL REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(motion_simulation ${SOURCE_FILES})
target_include_directories(motion_simulation PRIVATE ${Boost_INCLUDE_DIRS} ${OPENSSL_INCLUDE_DIR})
target_link_libraries( motion_simulation PRIVATE ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES})
I have searched the forums, and found relevant material, but I cannot apply any solution to this problem (most threads were applicable to windows/linux). My default cmake looks like this:
cmake_minimum_required(VERSION 2.8.4)
project(01___Shared_Pointers)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES
Point.cpp
Point.hpp
TestSharedPtr101.cpp)
FIND_PACKAGE(Boost)
IF (Boost_FOUND)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ADD_DEFINITIONS( "-DHAS_BOOST" )
ENDIF()
add_executable(01___Shared_Pointers ${SOURCE_FILES})
On my machine, boost is located at "Macintosh HD/opt/local/include/boost". I believe I am using version 1.57, which I installed via MacPorts. Is integration even possible on a Mac? Or am I wasting my time? Many thanks.