brew 2.5.0安装Boost 1.59失败求助(macOS 10.15.6)
Homebrew经常会把不再维护、存在安全隐患或者兼容性问题的旧版本公式标记为禁用,boost@1.59就是这种情况。结合你的环境(brew 2.5.0、macOS 10.15.6、已安装Boost 1.73.0),我整理了两个靠谱的解决办法:
方法一:通过本地旧版Formula文件安装
Homebrew的Formula仓库历史中保留着boost@1.59未被禁用时的版本,我们可以手动创建对应的Formula文件来完成本地安装:
- 先创建一个临时目录存放Formula文件,避免污染其他目录:
mkdir ~/temp-boost-install && cd ~/temp-boost-install - 在当前目录新建
boost@1.59.rb文件,将以下内容复制进去:class BoostAT159 < Formula
desc "Collection of portable C++ source libraries"
homepage "https://www.boost.org/"
url "https://boostorg.jfrog.io/artifactory/main/release/1.59.0/source/boost_1_59_0.tar.bz2"
sha256 "6aa9a5c6a4ca1016edd0ed1178e3cb8e6e3d00b010b807f7184e0dea6e38693f"bottle do
cellar :any
sha256 "d0e5a64e1b672f86c3957d7f0342d6f8d4c5a813b71f8692a7e6a9e7d6e3215f" => :el_capitan
sha256 "8a52f8a4d370688652b858665d7f91679b47083293950a373a8035538353e029" => :yosemite
sha256 "c1b17072712343d78d0c794a250b3602a10f88b889a9849a3b3077d9b2b0c30a" => :mavericks
endkeg_only :versioned_formula
depends_on "icu4c"
def install
# Force boost to compile with the desired compiler
open("user-config.jam", "a") do |file|
file.write "using darwin : : #{ENV.cxx} ;\n"
end# Fix error: no member named 'signbit' in the global namespace if ENV.compiler == :clang && MacOS.version >= :mavericks inreplace "boost/math/special_functions/sign.hpp", "using std::signbit;", "#ifdef __GLIBCXX__\nusing std::signbit;\n#else\nextern \"C++\" {\nfloat signbit(float);\ndouble signbit(double);\nlong double signbit(long double);\n}\n#endif" end args = ["--prefix=#{prefix}", "--disable-debug", "--disable-dependency-tracking", "--with-libraries=atomic,chrono,context,coroutine,date_time,exception,filesystem,graph,graph_parallel,iostreams,locale,log,math,mpi,program_options,random,regex,serialization,signals,system,test,thread,timer,wave", "--with-icu"] system "./bootstrap.sh", *args system "./b2", "install"end
test do
(testpath/"test.cpp").write <<-EOS.undent
#include <boost/algorithm/string.hpp>
#include
int main() {
std::string str = "Hello World!";
boost::to_upper(str);
return str != "HELLO WORLD!";
}
EOS
system ENV.cxx, "test.cpp", "-o", "test", "-L#{lib}", "-lboost_system"
system "./test"
end
end - 执行本地安装命令:
brew install ./boost@1.59.rb - 安装完成后,若需要优先使用该版本,可设置环境变量(可添加到
~/.zshrc或~/.bash_profile中持久生效):export PATH="#{HOMEBREW_PREFIX}/opt/boost@1.59/bin:$PATH"export LDFLAGS="-L#{HOMEBREW_PREFIX}/opt/boost@1.59/lib"export CPPFLAGS="-I#{HOMEBREW_PREFIX}/opt/boost@1.59/include"
方法二:手动编译安装Boost 1.59
手动编译能更灵活地控制安装路径,完全避免和已有的Boost 1.73.0冲突:
- 去Boost官方网站下载1.59.0版本的源码压缩包(格式为tar.bz2),放到本地目录
- 解压源码包:
tar -xjvf boost_1_59_0.tar.bz2 && cd boost_1_59_0 - 初始化bootstrap脚本,指定独立的安装路径(比如
~/local/boost-1.59,不会影响系统或brew的现有版本):./bootstrap.sh --prefix=~/local/boost-1.59 - 执行编译和安装:
./b2 install - 使用时,在编译项目时明确指定Boost的路径,例如:
g++ your_code.cpp -o your_program -I~/local/boost-1.59/include -L~/local/boost-1.59/lib -lboost_system
注意:不管用哪种方法安装1.59版本,都要确保在编译项目时明确指定版本路径,避免编译器默认调用已安装的1.73.0版本Boost库。
内容的提问来源于stack exchange,提问作者梁雨生

