CMake is not including boost directories into it's generated projects - c++

I have been trying to create a CMake project and then compiling it with Visual Studio 2015. When I Generate the Project Files, however, boost isn't included. Here is the relevant output from CMake upon generation:
Boost version: 1.62.0
Found the following Boost libraries:
system
thread
chrono
date_time
atomic
Configuring done
Generating done
And the paths are all correct. Where should CMake put the include directories, into VC++ Directories?
Where could the build system be going wrong?
The actual CMakeLists.txt is as follows:
#MultiTracker Application
cmake_minimum_required (VERSION 3.1)
project(MultiTracker)
#Additional CMake search modules
#Require C++11
set (CMAKE_CXX_STANDARD 11)
message(STATUS "Generating Makefile for MultiTracker")
file(GLOB SRC_FILES *.cpp)
#Find and link boost
SET(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED system thread)
add_executable(MultiTracker ${SRC_FILES})
#Link internal libraries
#Link 3rd party libraries
target_link_libraries(MultiTracker ${Boost_LIBRARIES})
#The native OS thread library
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(MultiTracker Threads::Threads)
Main.cpp
//System Includes
#include <iostream>
#include <cstdlib>
//Library Includes
#include <boost/program_options.hpp>
#include <boost/format.hpp>
//Local Includes
int main(int iArgc, char *cpArgv[])
{
std::string confName = "Conf.json", outFileName, inFileName;
//setup the program options
boost::program_options::options_description oPODescriptions("Available options");
oPODescriptions.add_options()
("help", "help message")
("confFile", boost::program_options::value<std::string>(&confName)->default_value("pclConf.json"), "Name of the configuration file to use");
boost::program_options::variables_map mapVars;
try
{
boost::program_options::store(boost::program_options::parse_command_line(iArgc, cpArgv, oPODescriptions), mapVars);
boost::program_options::notify(mapVars);
}
catch (std::exception &e)
{
std::cerr << e.what() << std::endl;
return 2;
}
//print the help message
if (mapVars.count("help"))
{
std::cout << "Stack Overflow Test: " << oPODescriptions << std::endl;
return ~0;
}
std::cout << "Press enter to exit" << std::endl;
std::cin.get();
}
Thanks!

You should also include in your question HOW did you managed the Boost dependency in your CMakeLists.txt file.
If you used find_package(Boost) (see reference here),
you should add Boost_INCLUDE_DIRS to your target project include directories,
and Boost_LIBRARIES to the libraries linked by your project.
See a very similar question and answer here: How do you add boost libraries in CMakeLists.txt
Added after question has been edited
you are missing:
target_include_directories(MultiTracker PRIVATE ${Boost_INCLUDE_DIRS})
(not sure about "PRIVATE" in your case)

Related

How can I pass the directory of boost library to CMakeLists.txt file for compilation?

I'm a newcomer in the world of CMake and Qt.
I want to create a small Qt application which can count factorial of any number. To do this in C++, I use boost library.
Generally, when I write C++ code with boost, I compile it in this way -
g++ MyCode.cpp -I "C:\boost" -o MyCode.exe
Now I'm trying to do the same in Qt. Here is the code of CMakeLists.txt :-
cmake_minimum_required(VERSION 3.14)
project(FirstProject-ConsoleApplication LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
# find_package(Boost 1.76.0 COMPONENTS ...)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(progname file1.cxx file2.cxx)
target_link_libraries(progname ${Boost_LIBRARIES})
endif()
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
add_executable(FirstProject-ConsoleApplication
main.cpp
)
target_link_libraries(FirstProject-ConsoleApplication Qt${QT_VERSION_MAJOR}::Core)
install(TARGETS FirstProject-ConsoleApplication
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
And this is the code for main.cpp file [This is a console application] :-
#include <QCoreApplication>
#include <QDebug>
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp> // 'boost/multiprecision/cpp_int.hpp' file not found
using namespace std;
using boost::multiprecision::cpp_int; // Use of undeclared identifier 'boost'
cpp_int Factorial(int number) // Unknown type name 'cpp_int'
{
cpp_int num = 1;
for (int i = 1; i <= number; i++)
num = num * i;
return num;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << Factorial(50) << "\n";
qInfo() << Factorial(100) << "\n";
return a.exec();
}
I want to know, how can I pass the directory of my boost folder to this CMakeLists.txt file so that the program gets built smoothly?
As suggested in this comment, it should be sufficient to add to the variable CMAKE_PREFIX_PATH the path where Boost is installed. Then find_package should be able to find Boost.
Alternatively, according to the documentation for FindBoost, you can also hint the location of Boost by setting the variable BOOST_ROOT. For example, assuming you run cmake from the path where CMakeList.txt is locate, you run
cmake -DBOOST_ROOT=/path/to/boost .
(The final "." indicate that CMakeList.txt is to be found in the current directory)
Or, you can indicate the location of the libraries and headers with the variables BOOST_LIBRARYDIR and BOOST_INCLUDEDIR
cmake -DBOOST_INCLUDEDIR=/path/to/boost/include -DBOOST_LIBRARYDIR=/path/to/boost/lib .
Instead of giving this variable on the command line, you could set these variables directly inside the CMakeList.txt file, adding before find_package
SET(BOOST_ROOT "/path/to/boost/")
or
SET(BOOST_INCLUDEDIR "/path/to/boost/include")
SET(BOOST_LIBRARYDIR "/path/to/boost/lib")
A couple of side remarks:
#include <bits/stdc++.h> should be avoided.
using namespace std is considered bad practice.

Boost with CMakeLists on Visual Studio

I'm trying to run some code with boost, but i can't include any boost file, like "boost/timer/timer.hpp". My CMakeLists contains
cmake_minimum_required(VERSION 3.10)
project(Converter)
find_package(Boost)
include_directories(${BOOST_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARIES})
add_executable(Converter converter.cpp)
TARGET_LINK_LIBRARIES(Converter ${Boost_LIBRARIES})
message("boost lib: ${Boost_LIBRARY_DIRS}, inc: ${Boost_INCLUDE_DIR}")
CMake answer
My cpp file contains
#include <iostream>
#include <boost/timer/timer.hpp>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
And when i am trying to build it, there is a error: "Cannot open include file 'boost/timer/timer.hpp'"
You are using a wrong non existing variable here. To set the include Boost directories to your project you need to use Boost_INCLUDE_DIRS, the case of the variable matters. And your link directories should be set to Boost_LIBRARY_DIRS.
cmake_minimum_required(VERSION 3.10)
project(Converter)
find_package(Boost COMPONENTS timer)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
add_executable(Converter converter.cpp)
target_link_libraries(Converter PUBLIC ${Boost_LIBRARIES})
message("boost lib: ${Boost_LIBRARY_DIRS}, inc: ${Boost_INCLUDE_DIR}")
Your small project can be further simplified by using the imported targets Boost::headers as follows:
cmake_minimum_required(VERSION 3.10)
project(Converter)
find_package(Boost COMPONENTS timer REQUIRED)
add_executable(Converter converter.cpp)
target_link_libraries(Converter PUBLIC Boost::headers Boost::timer)
message("boost lib: ${Boost_LIBRARY_DIRS}, inc: ${Boost_INCLUDE_DIRS}")

Undefined reference with boost asio ssl [duplicate]

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.

How can I find installed Boost library on ubuntu using CMake?

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.

Error on building a Wt project. Cannot open include file: 'boost/any.hpp'

Wt v. 3.2.2 and boost libraries v. 1.47 had succesfully installed in my computer and no errors occured in the installation process. Some simple Wt and Boost examples were compiled and ran correctly in the testing process. I use CMake, configured for MSVC 2008, to create the build files for my own Wt projects.
However, when I try to build my own project, I get this error (Cannot open include file: 'boost/any.hpp'). As I saw, boost/any.hpp is included in Wt/WApplication header file.
For further help, my CMakeLists.txt files contents are:
CMakeLists.txt placed on project directory:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(WT_EXAMPLE)
SET (WT_CONNECTOR "wthttp" CACHE STRING "Connector used (wthttp or wtfcgi)")
ADD_SUBDIRECTORY(source)
CMakeLists.txt placed on source directory:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(WT_INSTALL_DIR "C:/Program Files/WT/boost_1_47")
SET(BOOST_INSTALL_DIR "C:/Program Files/boost")
ADD_EXECUTABLE(
GOP.wt
Main.C
)
SET(WT_LIBS
optimized wthttp debug wthttpd
optimized wt debug wtd)
SET(BOOST_LIBS
boost_signals boost_regex boost_thread boost_filesystem boost_system
boost_random boost_date_time boost_program_options)
TARGET_LINK_LIBRARIES (
GOP.wt
${WT_LIBS} ${BOOST_LIBS} ${SYSTEM_LIBS}
)
LINK_DIRECTORIES (
${WT_INSTALL_DIR}/lib/
${BOOST_INSTALL_DIR}/lib/
)
INCLUDE_DIRECTORIES(${WT_INSTALL_DIR}/include)
INCLUDE_DIRECTORIES(${BOOST_INSTALL_DIR}/include)
As I saw in the CMakeCache.txt placed on Wt build directory, paths to boost libraries were found, but ...what about this line?
//The directory containing a CMake configuration file for Boost.
Boost_DIR:PATH=Boost_DIR-NOTFOUND
I asked this question on Wt support forum but I didn't get an answer for about 24 hours...
Update: I found that any.hpp is placed on C:\Program Files\boost\boost_1_47\boost\spirit\home\support\algorithm\any.hpp. So, i suspect that there's a concept with the path that searches any.hpp (it's not directly included in boost directory).
Problem solved. I did a new Wt build, I placed and rebuild boost in a new path and I wrote CMakeLists for this project with more caution.
To include <boost/any.hpp>
For example
#include <boost/any.hpp>
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<boost::any> some_values;
some_values.push_back(10);
const char* c_str = "Hello there!";
some_values.push_back(c_str);
some_values.push_back(std::string("Wow!"));
std::string& s = boost::any_cast<std::string&>(some_values.back());
s += " That is great!\n";
std::cout << s;
}
we only need a simple cmake file like
# Defines AppBase library target.
project(recipe_01)
cmake_minimum_required(VERSION 3.5)
include(GNUInstallDirs)
set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS 14)
message(FATAL_ERROR "app requires c++14 or newer")
elseif(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
find_package(Boost 1.60 REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main)