【问题标题】:C++: get Boost working; questions on include path and linking libraryC++:让 Boost 工作;有关包含路径和链接库的问题
【发布时间】:2013-05-10 06:18:51
【问题描述】:

我试图使用 Boost.Tokenizer 库。在我的PROG.cpp 中,我有以下内容:

#include <boost/tokenizer.hpp>

我的Makefile 最初是这样的

CXX = g++-4.8
CXXFLAGS = ## some irrelevant flags
LDFLAGS = ## some irrelevant flags
SOURCES = PROG.cpp
OBJECTS = $(SOURCES:.cpp=.o)
TARGETS = PROG

$(TARGETS) : $(OBJECTS)
        $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

## other targets

无法编译,因为找不到boost/tokenizer.hpp:

fatal error: boost/tokenizer.hpp: No such file or directory

然后我手动将 boost 包含路径添加到 CXXFLAGS:

-I/opt/local/include/

(这是来自 MacPorts 的路径。)

然后我尝试包含 Tokenizer 库,但在 /opt/local/lib/ 中我有 libboost_atomic-mt.dylib、libboost_chrono-mt.dylib 等,但没有像 tokenizer 这样的。我当时比较迷茫。我想这仍然行不通,因为图书馆没有链接。令人惊讶的是,该程序完美地构建、链接和运行。

所以我现在真的很困惑。以下是一些问题:

(1) 我没有明确链接到 boost,所以链接器将 boost 视为标准库?

(2) 如果 boost 被视为标准,为什么标头不是标准的?

(3) 为什么有libboost_atomic-mt.dylib、libboost_chrono-mt.dylib等,却没有tokenizer? tokenizer属于哪个动态库?

我对@9​​87654337@链接机制不是很熟悉;说到 boost,这是我第一个使用 boost 的程序。 非常感谢详细的解释。提前致谢!

供参考,这是我gcc -print-search-dirs提取的:

install: /usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/
programs: =/usr/gcc-4.8.0/libexec/gcc/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/libexec/gcc/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/libexec/gcc/x86_64-apple-darwin12.3.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../../x86_64-apple-darwin12.3.0/bin/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../../x86_64-apple-darwin12.3.0/bin/
libraries: =/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../../x86_64-apple-darwin12.3.0/lib/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../../x86_64-apple-darwin12.3.0/lib/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../:/lib/x86_64-apple-darwin12.3.0/4.8.0/:/lib/:/usr/lib/x86_64-apple-darwin12.3.0/4.8.0/:/usr/lib/

【问题讨论】:

    标签: c++ boost linker include


    【解决方案1】:

    大多数 boost 库只是头文件,如果您查看 .hpp 文件,您将不会只看到类的声明,就像您在头文件中所期望的那样,而是看到整个实现。这就是为什么对于 90% 的 boost 库,您无需担心链接,只需担心包含。

    但是,对于一些库、序列化程序和其他一些库来说,对于标头包含方法来说,污染代码太多了,这是不合理的。我确信对于何时将实现包含在标头中以及何时不包含,有一个更好、更严格的定义。

    http://www.boost.org/boost-build2/doc/html/bbv2/faq/header-only-libraries.html

    这是另一个关于它的问题: Why are not all boost libraries header-only?

    附言 通常,最好将 boost 库分开,并在您的 makefile 中执行以下操作:

    编译: CXXFLAGS += -I/path/to/boost/include

    对于链接: LDPATH += -L/path/to/boost/lib

    这使得升级升级版本变得更加容易,因为您只需在一个地方更改路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-06
      • 2011-03-26
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多