簡體   English   中英

如何將boost庫的目錄傳遞給CMakeLists.txt文件進行編譯?

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

我是CMake和Qt世界的新人。

我想創建一個小的 Qt 應用程序,它可以計算任何數字的階乘。 要在 C++ 中執行此操作,我使用 boost 庫。

一般我用boost寫C++代碼的時候都是這樣編譯的——

g++ MyCode.cpp -I "C:\boost" -o MyCode.exe

現在我正在嘗試在 Qt 中做同樣的事情。這是 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})

這是 main.cpp 文件的代碼 [這是一個控制台應用程序]:-

#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();
}

我想知道,如何將我的 boost 文件夾的目錄傳遞給這個 CMakeLists.txt 文件,以便程序順利構建?

如此評論中所建議的,將安裝 Boost 的路徑添加到變量CMAKE_PREFIX_PATH中應該就足夠了。 那么find_package應該可以找到Boost。

或者,根據FindBoost的文檔,您還可以通過設置變量BOOST_ROOT來提示 Boost 的位置。 例如,假設您從 CMakeList.txt 所在的路徑運行 cmake,您運行

cmake -DBOOST_ROOT=/path/to/boost .

(最后的“.”表示在當前目錄下找到CMakeList.txt)

或者,您可以使用變量BOOST_LIBRARYDIRBOOST_INCLUDEDIR指示庫和標頭的位置

cmake -DBOOST_INCLUDEDIR=/path/to/boost/include -DBOOST_LIBRARYDIR=/path/to/boost/lib .

您可以直接在 CMakeList.txt 文件中設置這些變量,而不是在命令行上提供此變量,在find_package之前添加

SET(BOOST_ROOT "/path/to/boost/")

或者

SET(BOOST_INCLUDEDIR "/path/to/boost/include")
SET(BOOST_LIBRARYDIR "/path/to/boost/lib")

一些旁注:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM