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

Time:01-06

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?

CodePudding user response:

According to the documentation for FindBoost, you can 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 .

Alternatively, 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:

  • Related