Linux 如何让 CMake 找到我的替代 Boost 安装?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3016448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get CMake to find my alternative Boost installation?
提问by BD at Rivenhill
I have installed the most recent version of Boost in /usr/local
(with includes in /usr/local/include/boost
and libraries in /usr/local/lib/boost
) and I am now attempting to install Wt from source, but CMake (version 2.6) can't seem to find the Boost installation. It tries to give helpful suggestions about setting BOOST_DIR and Boost_LIBRARYDIR, but I haven't been able to get it to work by tweaking these variables.
我已经安装了最新版本的 Boost /usr/local
(包含 in/usr/local/include/boost
和库/usr/local/lib/boost
),我现在正在尝试从源代码安装 Wt,但 CMake(2.6 版)似乎找不到 Boost 安装。它试图提供有关设置 BOOST_DIR 和 Boost_LIBRARYDIR 的有用建议,但我无法通过调整这些变量来使其工作。
The most recent error message that I get is that it can't find the libraries, but it seems to indicate that it is using "/usr/local/include" for the include path, which isn't correct (and I can't seem to fix it). Is there a solution for this off the top of their head, or do I need to go mucking around inside CMake to figure it out?
我得到的最新错误消息是它找不到库,但它似乎表明它正在使用“/usr/local/include”作为包含路径,这是不正确的(我不能t似乎修复它)。是否有解决方案可以解决这个问题,还是我需要在 CMake 内部四处走动才能弄清楚?
采纳答案by BD at Rivenhill
After digging around in CMake and experimenting, I determined that CMake was unhappy with the fact that all of my Boost libraries were contained in /usr/local/lib/boost
and not /usr/local/lib
. Once I soft-linked them back out, the build worked.
在CMake的周围挖和实验后,我决定CMake的是不满意我所有的Boost库被包含在事实/usr/local/lib/boost
和没有/usr/local/lib
。一旦我将它们软链接回来,构建就成功了。
回答by ypnos
You should have a look at FindBoost.cmake
script, which handles Boost detection and setting up all Boost variables. It typically resides in /usr/share/cmake-2.6/Modules/
. In it, you will find documentation. For instance:
您应该查看FindBoost.cmake
脚本,它处理 Boost 检测并设置所有 Boost 变量。它通常驻留在/usr/share/cmake-2.6/Modules/
. 在其中,您会找到文档。例如:
# These last three variables are available also as environment variables:
#
# BOOST_ROOT or BOOSTROOT The preferred installation prefix for searching for
# Boost. Set this if the module has problems finding
# the proper Boost installation.
#
In contrast to BOOST_ROOT, the variables you are referring to are actually variables that are set by the FindBoost module. Note that you don't have to (and probably also don't want to) edit your CMake project configuration to set BOOST_ROOT. Instead, you should use the environment variable, e.g. calling
与 BOOST_ROOT 相比,您所指的变量实际上是由 FindBoost 模块设置的变量。请注意,您不必(也可能不想)编辑 CMake 项目配置来设置 BOOST_ROOT。相反,您应该使用环境变量,例如调用
# BOOST_ROOT=/usr/local/... ccmake .
# BOOST_ROOT=/usr/local/... ccmake .
回答by user1795638
I had a similar issue, CMake finding a vendor-installed Boost only, but my cluster had a locally installed version which is what Iwanted it to use. Red Hat Linux 6.
我有一个类似的问题,CMake 只找到供应商安装的 Boost,但我的集群有一个本地安装的版本,这是我想要它使用的版本。红帽 Linux 6.
Anyway, it looks like all the BOOSTROOT
, BOOST_ROOT
, and Boost_DIR
stuff would get annoyed unless one also sets Boost_NO_BOOST_CMAKE
(e.g add to cmd line -DBoost_NO_BOOST_CMAKE=TRUE
).
无论如何,看起来所有的BOOSTROOT
、BOOST_ROOT
和Boost_DIR
东西都会变得恼火,除非还设置了Boost_NO_BOOST_CMAKE
(例如添加到 cmd line -DBoost_NO_BOOST_CMAKE=TRUE
)。
(I will concede the usefulness of CMake for multiplatform, but I can still hate it.)
(我承认 CMake 对多平台的用处,但我仍然讨厌它。)
回答by Offirmo
There is a generic method to give CMake directions about where to find libraries.
有一种通用方法可以为 CMake 提供有关在哪里查找库的指导。
When looking for a library, CMake looks first in the following variables:
在查找库时,CMake 首先查找以下变量:
CMAKE_LIBRARY_PATH
andLD_LIBRARY_PATH
for librariesCMAKE_INCLUDE_PATH
andINCLUDE_PATH
for includes
CMAKE_LIBRARY_PATH
和LD_LIBRARY_PATH
图书馆CMAKE_INCLUDE_PATH
和INCLUDE_PATH
包括
If you declare your Boost files in one of the environment variables, CMake will find it. Example:
如果您在其中一个环境变量中声明 Boost 文件,CMake 会找到它。例子:
export CMAKE_LIBRARY_PATH="/stuff/lib.boost.1.52/lib:$CMAKE_LIBRARY_PATH"
export CMAKE_INCLUDE_PATH="/stuff/lib.boost.1.52/include:$CMAKE_INCLUDE_PATH"
If it's too cumbersome, you can also use a nice installing tool I wrote that will do everything for you: C++ version manager
如果太麻烦,你也可以使用我写的一个很好的安装工具,它会为你做一切:C++版本管理器
回答by Samsky
I also encountered the same problem, but trying the hints here didn't help, unfortunately.
我也遇到了同样的问题,但不幸的是,尝试这里的提示并没有帮助。
The only thing that helped was to download the newest version from the Boost page, compile and install it as described in Installing Boost 1.50 on Ubuntu 12.10.
唯一有帮助的是从 Boost 页面下载最新版本,编译并安装它,如在Ubuntu 12.10 上安装 Boost 1.50 中所述。
In my case I worked with Boost 1.53.
就我而言,我使用的是 Boost 1.53。
回答by Damien Kick
I was finally able to get what I wanted with
我终于能够得到我想要的
cmake -DCMAKE_INSTALL_PREFIX=$TARGET \
-DBoost_NO_BOOST_CMAKE=TRUE \
-DBoost_NO_SYSTEM_PATHS=TRUE \
-DBOOST_ROOT:PATHNAME=$TARGET \
-DBoost_LIBRARY_DIRS:FILEPATH=${TARGET}/lib
回答by jaor
Generally the most common mistake is not cleaning your build directoryafter adding new options. I have Boost installed from system packet manager. Its version is 1.49.
通常,最常见的错误是在添加新选项后没有清理构建目录。我从系统数据包管理器安装了 Boost。它的版本是 1.49。
I also downloaded Boost 1.53 and "installed" it under $HOME/installs
.
我还下载了 Boost 1.53 并在$HOME/installs
.
The only thing that I had to do in my project was to (I keep sources in my_project_directory/src
):
在我的项目中,我唯一要做的就是(我将资源保存在 中my_project_directory/src
):
cd my_project_directory
mkdir build
cd build
cmake -DCMAKE_INCLUDE_PATH=$HOME/installs/include -DCMAKE_LIBRARY_PATH=$HOME/installs/lib ../src
And that's it. Ta bum tss.
就是这样。Ta bum tss。
But if I'd make after cd build
-> cmake ../src
it would set Boost from the system path. Then doing cmake -DCMAKE_INCLUDE_PATH=$HOME/installs/include -DCMAKE_LIBRARY_PATH=$HOME/installs/lib ../src
would change nothing.
但是如果我在之后cd build
->cmake ../src
它将从系统路径设置 Boost。那么做cmake -DCMAKE_INCLUDE_PATH=$HOME/installs/include -DCMAKE_LIBRARY_PATH=$HOME/installs/lib ../src
就什么都改变不了。
You have to clean your build directory( cd build && rm -rf *
;) )
你必须清理你的构建目录( cd build && rm -rf *
;))
回答by hungptit
I had a similar issue, and I could use customized Boost libraries by adding the below lines to my CMakeLists.txt
file:
我有一个类似的问题,我可以通过将以下几行添加到我的CMakeLists.txt
文件中来使用自定义的 Boost 库:
set(Boost_NO_SYSTEM_PATHS TRUE)
if (Boost_NO_SYSTEM_PATHS)
set(BOOST_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../3p/boost")
set(BOOST_INCLUDE_DIRS "${BOOST_ROOT}/include")
set(BOOST_LIBRARY_DIRS "${BOOST_ROOT}/lib")
endif (Boost_NO_SYSTEM_PATHS)
find_package(Boost REQUIRED regex date_time system filesystem thread graph program_options)
include_directories(${BOOST_INCLUDE_DIRS})
回答by rickfoosusa
While configure could find my Boost installation, CMake could not.
虽然 configure 可以找到我的 Boost 安装,但 CMake 不能。
Locate FindBoost.cmake and look for LIBRARY_HINTS to see what sub-packages it is looking for. In my case it wanted the MPI and graph libraries.
找到 FindBoost.cmake 并查找 LIBRARY_HINTS 以查看它要查找的子包。就我而言,它需要 MPI 和图形库。
# Compute component-specific hints.
set(_Boost_FIND_LIBRARY_HINTS_FOR_COMPONENT "")
if(${COMPONENT} STREQUAL "mpi" OR ${COMPONENT} STREQUAL "mpi_python" OR
${COMPONENT} STREQUAL "graph_parallel")
foreach(lib ${MPI_CXX_LIBRARIES} ${MPI_C_LIBRARIES})
if(IS_ABSOLUTE "${lib}")
get_filename_component(libdir "${lib}" PATH)
string(REPLACE "\" "/" libdir "${libdir}")
list(APPEND _Boost_FIND_LIBRARY_HINTS_FOR_COMPONENT ${libdir})
endif()
endforeach()
endif()
apt-cache search ... I installed the dev packages since I was building code, and the dev package drags in all the dependencies. I'm not so sure that a standard Boost install needs Open MPI, but this is OK for now.
apt-cache search ... 因为我正在构建代码,所以我安装了 dev 包,并且 dev 包拖入了所有依赖项。我不太确定标准 Boost 安装是否需要 Open MPI,但现在可以了。
sudo apt-get install libboost-mpi-dev libboost-mpi-python-dev
sudo apt-get install libboost-graph-parallel-dev
回答by Cameron Lowell Palmer
The short version
简短的版本
You only need BOOST_ROOT
, but you're going to want to disable searching the system for your local Boost if you have multiple installations or cross-compiling for iOS or Android. In which case add Boost_NO_SYSTEM_PATHS
is set to false.
您只需要BOOST_ROOT
,但如果您有多个安装或 iOS 或 Android 的交叉编译,您将希望禁用搜索本地 Boost 的系统。在这种情况下,addBoost_NO_SYSTEM_PATHS
设置为 false。
set( BOOST_ROOT "" CACHE PATH "Boost library path" )
set( Boost_NO_SYSTEM_PATHS on CACHE BOOL "Do not search system for Boost" )
Normally this is passed on the CMake command-line using the syntax -D<VAR>=value
.
通常,这是使用语法在 CMake 命令行上传递的-D<VAR>=value
。
The longer version
更长的版本
Officially speaking the FindBoostpage states these variables should be used to 'hint' the location of Boost.
正式地说,FindBoost页面声明这些变量应该用于“提示”Boost 的位置。
This module reads hints about search locations from variables:
该模块从变量中读取有关搜索位置的提示:
BOOST_ROOT - Preferred installation prefix
(or BOOSTROOT)
BOOST_INCLUDEDIR - Preferred include directory e.g. <prefix>/include
BOOST_LIBRARYDIR - Preferred library directory e.g. <prefix>/lib
Boost_NO_SYSTEM_PATHS - Set to ON to disable searching in locations not
specified by these hint variables. Default is OFF.
Boost_ADDITIONAL_VERSIONS
- List of Boost versions not known to this module
(Boost install locations may contain the version)
This makes a theoretically correct incantation:
这是理论上正确的咒语:
cmake -DBoost_NO_SYSTEM_PATHS=TRUE \
-DBOOST_ROOT=/path/to/boost-dir
When you compile from source
从源代码编译时
include( ExternalProject )
set( boost_URL "http://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.bz2" )
set( boost_SHA1 "9f1dd4fa364a3e3156a77dc17aa562ef06404ff6" )
set( boost_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost )
set( boost_INCLUDE_DIR ${boost_INSTALL}/include )
set( boost_LIB_DIR ${boost_INSTALL}/lib )
ExternalProject_Add( boost
PREFIX boost
URL ${boost_URL}
URL_HASH SHA1=${boost_SHA1}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND
./bootstrap.sh
--with-libraries=filesystem
--with-libraries=system
--with-libraries=date_time
--prefix=<INSTALL_DIR>
BUILD_COMMAND
./b2 install link=static variant=release threading=multi runtime-link=static
INSTALL_COMMAND ""
INSTALL_DIR ${boost_INSTALL} )
set( Boost_LIBRARIES
${boost_LIB_DIR}/libboost_filesystem.a
${boost_LIB_DIR}/libboost_system.a
${boost_LIB_DIR}/libboost_date_time.a )
message( STATUS "Boost static libs: " ${Boost_LIBRARIES} )
Then when you call this script you'll need to include the boost.cmake script (mine is in the a subdirectory), include the headers, indicate the dependency, and link the libraries.
然后当你调用这个脚本时,你需要包含 boost.cmake 脚本(我的在 a 子目录中),包含头文件,指明依赖关系,并链接库。
include( boost )
include_directories( ${boost_INCLUDE_DIR} )
add_dependencies( MyProject boost )
target_link_libraries( MyProject
${Boost_LIBRARIES} )