通过自制程序安装时如何使用boost?

问题描述 投票:0回答:2

这可能是一个非常基本的问题,但这是我第一次遇到这样的问题。我使用的是 M1 macOS Big Sur。我正在尝试为我的程序运行 boost 库。我已使用

arch -arm64 brew install boost
在我的设备上安装了 boost。

当我尝试编译以下程序时:

#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
 
int128_t boost_product(long long A, long long B)
{
    int128_t ans = (int128_t)A * B;
    return ans;
}
 
int main()
{
    long long first = 98745636214564698;
    long long second = 7459874565236544789;
    cout << "Product of " << first << " * " << second
         << " = \n"
         << boost_product(first, second);
    return 0;
}

$ g++ --version
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: arm64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

我收到以下错误:

boost.cpp:1:10: fatal error: 'boost/multiprecision/cpp_int.hpp' file not found
#include <boost/multiprecision/cpp_int.hpp>
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

这是我进入时看到的

/opt/homebrew/lib: 

cmake                                  libboost_log-mt.dylib                  libboost_serialization.a               libgettextsrc-0.21.dylib
gcc                                    libboost_log.a                         libboost_serialization.dylib           libgettextsrc.dylib
gettext                                libboost_log.dylib                     libboost_stacktrace_addr2line-mt.a     libgmp.10.dylib
libasprintf.0.dylib                    libboost_log_setup-mt.a                libboost_stacktrace_addr2line-mt.dylib libgmp.a
libasprintf.a                          libboost_log_setup-mt.dylib            libboost_stacktrace_addr2line.a        libgmp.dylib
libasprintf.dylib                      libboost_log_setup.a                   libboost_stacktrace_addr2line.dylib    libgmpxx.4.dylib
libboost_atomic-mt.a                   libboost_log_setup.dylib               libboost_stacktrace_basic-mt.a         libgmpxx.a
libboost_atomic-mt.dylib               libboost_math_c99-mt.a                 libboost_stacktrace_basic-mt.dylib     libgmpxx.dylib
...         

当我 cd 进入 cmake 时,我看到有一堆

Boost_...
前缀的文件。

看起来我的电脑上有boost,但是我如何在我的程序中使用它并用g++编译它?

c++ macos boost homebrew
2个回答
2
投票

我发现自制程序正在将文件添加到

/opt/homebrew/Cellar/boost/1.79.0_1/include/boost
。一旦确定了位置,我就简单地用带有flag的代码进行编译

g++ -I/opt/homebrew/Cellar/boost/1.79.0_1/include/boost ... 

并确保我的包含语句引用了适当的 .hpp 文件。


0
投票

Homebrew 安装的 Boost 库位于

/opt/homebrew/Cellar/boost/1.85.0
(您可以通过运行
brew info boost
检查路径)。这意味着您需要将此路径显式添加到编译器中,如下所示:


注意:你必须使用你自己的路径而不是我的路径,否则会导致错误!


clang++ a.cc -I/opt/homebrew/Cellar/boost/1.85.0/include -L/opt/homebrew/Cellar/boost/1.85.0/lib -std=c++11

# or use g++
g++ a.cc -I/opt/homebrew/Cellar/boost/1.85.0/include -L/opt/homebrew/Cellar/boost/1.85.0/lib -std=c++11

每次要编译时都写出这些长路径是很乏味的,即使是在 Makefile 或 CMake 中也是如此。更好的方法是配置 pkg-config 以供将来使用:

  1. boost.pc
    中创建一个名为
    /opt/homebrew/Cellar/boost/1.85.0/boost.pc
    的文件,其中包含以下内容:
prefix=/opt/homebrew/Cellar/boost/1.85.0
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: Boost
Description: Boost C++ Libraries
Version: 1.85.0
Libs: -L${libdir}
Cflags: -I${includedir}
/* Libs: -L${libdir} -lboost_system -lboost_filesystem -lboost_graph */
  1. export PKG_CONFIG_PATH="/opt/homebrew/Cellar/boost/1.85.0"
    添加到您的 shell 脚本(例如,
    .bash_profile
    .zshrc
    ),然后重新加载您的 shell。

  2. 如果配置正确,

    pkg-config --cflags --libs boost
    将返回
    -I/opt/homebrew/Cellar/boost/1.85.0/include -L/opt/homebrew/Cellar/boost/1.85.0/lib

现在,每当您想使用 Boost 库时,只需添加

$(pkg-config --cflags --libs boost)
,如下所示:

clang++ a.cc $(pkg-config --cflags --libs boost) -std=c++11
# or use g++
g++ a.cc $(pkg-config --cflags --libs boost) -std=c++11

或者在 Makefile 中:

PKGCFG := pkg-config
BOOST_CFLAGS := $(shell $(PKGCFG) --cflags boost)
BOOST_LIBS := $(shell $(PKGCFG) --libs boost)

$(EXEC): $(OBJ)
    $(CXX) $(CXXFLAGS) $(OBJ) -o $@ $(BOOST_LIBS)

%.o: %.cc
    $(CXX) $(CXXFLAGS) $(BOOST_CFLAGS) -c $< -o $@

希望这对您有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.