簡體   English   中英

使用cmake 2.8.8鏈接Boost庫

[英]linking boost libaries using cmake 2.8.8

我在通過stackoverflow和其他論壇搜索的cmake-baed Boost鏈接問題上苦苦掙扎,但無濟於事。

我正在已經安裝了Boost(我知道完整路徑)的群集上編譯C ++程序套件(除了Boost之外,還取決於其他庫)。 在編譯時使用cmake獲得奇怪的鏈接錯誤以進行Boost增強后,我想到了首先編譯一個非常簡單的boost-cmake示例來解決該問題。

代碼如下。

#include <boost/program_options/options_description.hpp>
#include <boost/program_options/option.hpp>
using namespace std;
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {

po::options_description desc("Allowed options");
desc.add_options()
    ("help", "produce help message")
    ;

return 0;
}

我正在使用以下CMakeLists.txt文件進行構建。

cmake_minimum_required(VERSION 2.8)
set(Boost_INCLUDE_DIR /software/apps/boost/1.55.0/build06/include) 
set(Boost_LIBRARY_DIR /software/apps/boost/1.55.0/build06/lib)

find_package(Boost COMPONENTS program_options REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})

message("Boost include dir is ${Boost_INCLUDE_DIR}")
message("Boost library dir is ${Boost_LIBRARY_DIR}")
message("Boost libraries are at ${Boost_LIBRARIES}")

add_executable(main main.cpp)
target_link_libraries( main ${Boost_LIBRARIES} )

當我使用cmake進行編譯時,得到以下輸出

    x_ikrul@ikramu build]$  emacs ../CMakeLists.txt
Display localhost:39.0 unavailable, simulating -nw
[x_ikrul@triolith1 build]$ rm -rf *
[x_ikrul@triolith1 build]$ cmake ..
-- The C compiler identification is GNU 4.7.2
-- The CXX compiler identification is GNU 4.7.2
-- Check for working C compiler: /software/apps/comp_wrapper/gnu/gcc
-- Check for working C compiler: /software/apps/comp_wrapper/gnu/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /software/apps/comp_wrapper/gnu/c++
-- Check for working CXX compiler: /software/apps/comp_wrapper/gnu/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
Boost  found.
Found Boost components:
program_options
Boost include dir is /usr/include
Boost library dir is /software/apps/boost/1.55.0/build06/lib
Boost libraries are at optimized;boost_program_options-mt-shared;debug;boost_program_options-mt-shared-debug
-- Configuring done
-- Generating done
-- Build files have been written to: /home/x_ikrul/Downloads/boost_example/build

當我“制作”下面給出的代碼時,問題就來了。

    x_ikrul@ikramu build]$make 
Scanning dependencies of target main
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
make[2]: *** No rule to make target `/usr/lib64/lib64/libboost_program_options-mt.so.5', needed by `main'.  Stop.
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2

如您所見,盡管CMake將boost的lib目錄顯示為/software/apps/boost/1.55​​.0/build06/lib,但在鏈接時卻很奇怪,但它指向的是另一個不存在的目錄。

有人遇到這樣的錯誤嗎? 集群運行的是CentOS版本6.5,boost(我指向的地方)是1.55.0,而我使用的cmake是2.8.8。

您沒有正確設置提示路徑。

FindBoost模塊文檔

該模塊從變量讀取有關搜索位置的提示:

BOOST_ROOT首選安裝前綴(或BOOSTROOT)

BOOST_INCLUDEDIR首選包含目錄,例如<prefix>/include

BOOST_LIBRARYDIR首選庫目錄,例如<prefix>/lib

Boost_NO_SYSTEM_PATHS設置為ON以禁用在這些提示變量未指定的位置中搜索。 默認為OFF

...

並將搜索結果永久保存在CMake緩存條目中:

Boost_INCLUDE_DIR包含Boost標頭的目錄

Boost_LIBRARY_DIR包含Boost庫的目錄

因此,您的行:

set(Boost_INCLUDE_DIR /software/apps/boost/1.55.0/build06/include) 
set(Boost_LIBRARY_DIR /software/apps/boost/1.55.0/build06/lib)

實際上並沒有設置提示變量-您想要設置BOOST_INCLUDEDIRBOOST_LIBRARYDIR 可能還有Boost_NO_SYSTEM_PATHS

最好不要在您的CMakeLists.txt文件中進行硬編碼,因為這是不可移植的。 而是在調用CMake時在命令行上傳遞它們:

cmake . -DBOOST_INCLUDEDIR=/software/apps/boost/1.55.0/build06/include -DBOOST_LIBRARYDIR=/software/apps/boost/1.55.0/build06/lib -DBoost_NO_SYSTEM_PATHS=ON

如果將Boost_DEBUG設置為ON ,則還可以更好地了解發生了什么。

順便說一句,你不應該需要link_directories調用,因為到了Boost庫的完整路徑在傳遞target_link_libraries電話。

這個stackoverflow答案建議在cmake命令行上添加-DBoost_NO_BOOST_CMAKE=ON

這似乎是由於某些CMake版本無法與某些Boost版本配合使用而造成的。

我同意用戶Fraser所說的。 這些行沒有意義

set(Boost_INCLUDE_DIR /software/apps/boost/1.55.0/build06/include) 
set(Boost_LIBRARY_DIR /software/apps/boost/1.55.0/build06/lib)

因為CMake使用這些變量存儲Boost搜索的結果。 在他的答案中閱讀更多有關它的內容。

暫無
暫無

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

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