I've installed Boost using this command
sudo apt-get install libboost-all-dev
and I wrote this simple example in main.cpp
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello, world!" << std::endl;
return 0;
}
And in my CMakeLists.txt I have this:
cmake_minimum_required(VERSION 2.8)
find_package(Boost REQUIRED)
if(NOT Boost_FOUND)
message(SEND_ERROR "Failed to find Boost")
return()
else()
include_directories(${Boost_INCLUDE_DIR})
endif()
add_executable(main main.cpp)
CMake worked correctly, but after launching with make I got a few errors:
main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()'
How to correctly include boost in my CMakeLists.txt so that cmake will find libraries ?
You need to link against the boost libraries. FindBoost provides the variable Boost_LIBRARIES for this:
add_executable(main main.cpp)
target_link_libraries(main ${Boost_LIBRARIES})
For more information, see the FindBoost documentation. There's an example near the end.
main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()'
It's failing at the link step. You're not linking to the system library. You need to do that.
You're not running into any error with regard to CMake making use of boost. You just need to tell it that system needs to be linked in.
To add on to the previous answers, here is the list of Boost librairies you need to link. (as of Boost 1.65)
All other boost libraires can be used by simply including the header.
Related
I have been writing a visualisation tool using OpenGL. It has been compiling and linking just fine (using gcc 11.2.0) until I recently installed the Linux dependencies for OpenVDB (listed under Using UNIX apt-get). I am now getting linker errors that I have narrowed down to the inclusion of the <execution> header file:
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `tbb::detail::d1::execution_slot(tbb::detail::d1::execution_data const&)':
main.cpp:(.text._ZN3tbb6detail2d114execution_slotERKNS1_14execution_dataE[_ZN3tbb6detail2d114execution_slotERKNS1_14execution_dataE]+0x18): undefined reference to `tbb::detail::r1::execution_slot(tbb::detail::d1::execution_data const*)'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `tbb::detail::d1::current_thread_index()':
main.cpp:(.text._ZN3tbb6detail2d120current_thread_indexEv[_ZN3tbb6detail2d120current_thread_indexEv]+0x12): undefined reference to `tbb::detail::r1::execution_slot(tbb::detail::d1::execution_data const*)'
The above is from the following minimal example.
main.cpp:
#include <execution>
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(test)
set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)
add_executable(test main.cpp)
I have tried linking TBB with my project like so:
# Find and link TBB.
find_package(TBB REQUIRED)
include_directories(${TBB_INCLUDE_DIRS})
link_directories(${TBB_LIBRARY_DIRS})
add_definitions(${TBB_DEFINITIONS})
if(NOT TBB_FOUND)
message("Error: TBB not found")
endif(NOT TBB_FOUND)
add_executable(test main.cpp)
target_link_libraries(test ${TBB_LIBRARIES})
...and also adding the -ltbb linker flag (as per the answer to this post) using
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ltbb")
However, this does not solve the issue.
What I find particularly strange is that I didn't use to have to link against tbb despite having included the <excution> header all along. Only after installing the OpenVDB dependencies has this become an issue.
Can anyone advise me on how to solve this (either by appropriately linking tbb, or not having to link it at all, as was the case before)? Any form of insight would be much appreciated.
I have managed to successfully link tbb as follows:
find_package(TBB REQUIRED COMPONENTS tbb)
add_executable(test main.cpp)
target_link_libraries(test tbb)
Although I am still clueless as to why I suddenly needed to link with tbb, considering I previously didn't need to.
Uninstalling libtbb-dev also works and dont require editing the CMakeFiles.txt.
I am trying to create a simple example using the boost library. I can successfully use CMake for the initial setup and it finds boost.
using the following code in CMakeLists.txt:
cmake_minimum_required(VERSION 3.18)
project(edge_detector)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(edge_detector main.cpp)
target_include_directories(edge_detector PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(edge_detector ${Boost_LIBRARIES})
However when I try to build the project using make or CMake --build . boost is not found and I am met with this error:
I am not sure what I am missing, I would be grateful for any help
Your include directive must include a file not a directory.
Replace
#include <boost/algorithm/string>
with
#include <boost/algorithm/string.hpp>
I have a minimal example of Boost serialization where I try to save an integer in a binary archive file
Here is main.cpp:
#include <iostream>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
int main() {
int t = 0;
std::ofstream file("Test.bin");
boost::archive::binary_oarchive archive(file);
archive << t;
file.close();
return 0;
}
and here is the CMake file:
cmake_minimum_required(VERSION 3.15)
project(Test)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost REQUIRED serialization)
add_executable(Test main.cpp)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(Test ${Boost_LIBRARIES})
endif()
When I try to run this program in CLion, I get a large list of undefined reference errors as shown here:
https://pastebin.com/8uX9MZFf
I have setup Boost using vcpkg package manager. I'm compiling using Mingw-w64. The CMake file loads without errors (only a warning that says "New Boost version may have incorrect or missing dependencies and imported targets," though I've heard this warning isn't of concern, as it just means the current version of CMake isn't aware of the newest version of Boost).
I've tried to look for solutions to this everywhere, but I can't seem to find anything that works here. Any help would be appreciated.
I'm using cmake 3.15.3, boost 1.73.0 and mingw-w64 6.0.
EDIT
I uninstalled and reinstalled Boost without using the package manager, and tried getting the serialization library again. In this context, CMake runs into errors saying it can't find Boost with serialization (Though it can find Boost alone). I set Boost_DEBUG to ON and looked at the output, and noticed the following things:
_boost_COMPILER = "-mgw81" (guessed)
CMake seems to guess that the compiler I used to compile boost was mgw81. I'm guessing it got the 8.1 from my gcc version, which is correct.
Searching for SERIALIZATION_LIBRARY_RELEASE: boost_serialization-mgw81-mt-x64-1_73;boost_serialization-mgw81-mt-x64;...
As a result of that compiler selection, it searches for a file with "-mgw81" in the name. The problem is that the library files generated when I built boost are named like so:
libboost_serialization-mgw8-mt-x64-1_73.a
This says "-mgw8" instead of "-mgw81". I don't know how to correct CMake or build boost in such a way that this conflict doesn't happen. I've tried rebuilding boost with toolset=gcc-8.1 instead of toolset=gcc, but I still get "-mgw8" in the library file names.
EDIT 2
I found the solution to the above issue. I've posted it below.
After realizing that the issue was what I mentioned in EDIT, I looked further into how that issue could be resolved, and I found out you can manually set the compiler that is used to search through the variable Boost_COMPILER.
I changed my CMake file to the following:
cmake_minimum_required(VERSION 3.15)
project(Test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/boost_1_73_0")
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/boost_1_73_0/libs")
set(Boost_DEBUG ON)
set(Boost_COMPILER -mgw8)
set(Boost_ARCHITECTURE -x64)
set(BOOST_ROOT C:/boost)
set(BOOST_INCLUDEDIR C:/boost/include/boost-1_73/boost)
set(BOOST_LIBRARYDIR C:/boost/lib)
set(BOOST_NO_SYSTEM_PATHS ON)
find_package(Boost REQUIRED serialization)
add_executable(Test main.cpp)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(Test ${Boost_LIBRARIES})
endif()
I believe the critical changes here were setting Boost_COMPILER and Boost_ARCHITECTURE. I realized Boost_ARCHITECTURE needed to be set from this question: Linking boost in CLion project.
With this CMake file, my main.cpp file from above ran properly.
I have a question for people who work with CMakeList.txt in C++. I want to use Podofo project (a project to parse & create pdf).
So my main function is simple as:
#include <iostream>
#include <podofo/podofo.h>
int main() {
PoDoFo::PdfMemDocument pdf;
pdf.Load("/Users/user/path/to.pdf");
int nbOfPage = pdf.GetPageCount();
std::cout << "Our pdf have " << nbOfPage << " pages." << std::endl;
return 0;
}
My CMakeList.txt is:
cmake_minimum_required(VERSION 3.7)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(untitled ${SOURCE_FILES})
But I am stuck with this error:
/usr/local/include/podofo/base/PdfEncrypt.h:44:10: fatal error: 'openssl/opensslconf.h' file not found
#include <openssl/opensslconf.h
I tried to include with find_package, find_library .. setting some variables but I do not find the way.
My env is:
macOS
Clion
Podofo installed via home-brew in /usr/local/podofo
OpenSSL installed via home-brew in /usr/local/opt/openssl
Thanks by advance community !!
find_package is the correct approach; you find details about it here.
In your case, you should add these lines:
find_package(OpenSSL REQUIRED)
target_link_libraries(untitled OpenSSL::SSL)
If CMake doesn't find OpenSSL directly, you should set the CMake variable OPENSSL_ROOT_DIR.
I have followed this answer and used Boost.Multiprecision to use high accuracy floating point numbers (examples).
main.cpp
#include <iostream>
#include <boost/multiprecision/mpfr.hpp> // Defines the Backend type that wraps MPFR
int main(int argc, char** argv)
{
namespace mp = boost::multiprecision; // Reduce the typing a bit later...
typedef mp::number<mp::mpfr_float_backend<300> > my_float;
my_float a, b, c; // These variables have 300 decimal digits precision
return 0;
}
However, I have a serious problem for compilation of this code as I receive the following error:
/usr/include/boost/multiprecision/mpfr.hpp:15:18:
fatal error: mpfr.h: No such file or directory
Even installing libgmp3-dev and gmplib did not work.
What is wrong?
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project (main)
# Libraries
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.54.0 COMPONENTS filesystem regex system thread date_time wave)
if(NOT Boost_FOUND)
message( FATAL_ERROR "Boost 1.54.0 not found." )
endif()
include_directories(SYSTEM ${Boost_INCLUDE_DIR})
# Flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wfatal-errors -std=c++11")
include_directories(${Boost_INCLUDE_DIRS})
# pre executable commands
add_executable(main
main.cpp
)
# Link
target_link_libraries(main ${Boost_LIBRARIES})
target_link_libraries(main ${CMAKE_THREAD_LIBS_INIT})
If you want to use the MPFR backend you've got to install it separately, make sure it is built, and make sure its headers are in your compiler's INCLUDE path and its binaries (libraries) are in your linker's command line.
(MPFR is not GMP.)
You must include the libraries for both gmp and mfr in your cmake file.
In qmake use: LIBS += -lgmp -lmpfr
If you do not know how in cmake, just let qmake make a cmake file, I do not use cmake, but I know it is similar, but forgot how to do it, or I would just do it and post the results, sorry, but of all the things I have forgotten over the years, like where did I put my Memories; but like them, this post is Old, so anyone finding it might find this helpful.
This will satisfy the includes below, and a few more:
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/mpfr.hpp>
#include <boost/multiprecision/gmp.hpp>