1

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?

  • Build&install as boost as described here: https://www.boost.org/doc/libs/1_81_0/more/getting_started/unix-variants.html (Note: not the windows version, since you seem to be using mingw). Then all you need to do before `find_package` is to add the install path to the `CMAKE_PREFIX_PATH` variable (the boost installation comes with cmake configuration scripts). – fabian Jan 05 '23 at 17:15
  • Hey! Finally the CMAKE_PREFIX_PATH solved my problem! That was the finishing touch I needed! Thank you both fabian and francesco <3 – Debtanu Gupta Jan 05 '23 at 18:01

1 Answers1

1

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:

francesco
  • 7,189
  • 7
  • 22
  • 49
  • Hello @francesco, thank you for your answer, but I'm so sorry, I'm unable to get that exactly where I need to add this code. I want to use this part -> "cmake -DBOOST_ROOT=/path/to/boost". Can you please kindly elaborate that exactly where I need to do the changes? – Debtanu Gupta Jan 05 '23 at 16:49
  • @DebtanuGupta See expanded answer. – francesco Jan 05 '23 at 17:02
  • If you've installed boost properly on the system, it provides cmake configuration scripts... No need to use the find module... – fabian Jan 05 '23 at 17:16
  • Hey, I've done these changes - set(BOOST_ROOT "C:/boost"), set(Boost_NO_SYSTEM_PATHS OFF), set(Boost_USE_MULTITHREADED ON), set(Boost_USE_STATIC_LIBS ON), find_package(Boost 1.76.0 REQUIRED). But still Qt Creator is unable to find the path of "" in main.cpp file. And thank you for the side remarks <3 – Debtanu Gupta Jan 05 '23 at 17:49
  • Hello @fabian, how can I know that whether I installed boost properly or not? But yes, there is a folder named as "C:\boost\lib64-msvc-14.2\cmake" at my pc. Should I check something more? – Debtanu Gupta Jan 05 '23 at 17:56
  • @DebtanuGupta The cmake configuration files should be there. Having installed boost 1.76 at path `.../boost` the file `.../boost/lib/cmake/Boost-1.76.0/BoostConfig.cmake` should be there as well as a `BoostConfigVersion.cmake` file in the same dir that won't reject the compiler you're using in the consuming project. The part may indicate some potential issues with mingw gcc use, but regarding these kinds of issues I cannot speak from experience, since I've only used MSVC to target windows... – fabian Jan 05 '23 at 19:07