c++ - std::string::empty 的 undefined symbol 错误; Mac OS High Sierra 上的 c++ 标准方法链接错误

标签 c++ macos boost clang linker-errors

我只是想编译下面的示例 cpp 文件; http://apolukhin.github.io/Boost-Cookbook/#Chapter01-recipe7-part1

// Contains boost::bind and placeholders.
#include <boost/bind.hpp>

// Utility stuff required by samples.
#include <boost/array.hpp>
#include <algorithm>
#include <functional>
#include <string>
#include <cassert>

void sample2() {
    const boost::array<std::string, 3> v = {{"We ", "are", " the champions!"}};

    const std::size_t count0 = std::count_if(v.begin(), v.end(),
                                             [](const std::string& s) { return s.empty(); }
    );
    const std::size_t count1 = std::count_if(v.begin(), v.end(),
                                             boost::bind(&std::string::empty, _1)
    );

    assert(count0 == count1);
}

int main () {
    sample2();
    return 0;
}

但是我得到了这样的链接错误;

c++ -I/usr/local/opt/boost/include --std=c++14 boost_practice.cpp
Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::empty() const", referenced from:
      sample2() in boost_practice-bc38e6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

当我用 -v 选项调用时,结果是

c++ -v -stdlib=libc++ -I/usr/local/opt/boost/include --std=c++11 boost_practice.cpp
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.13.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name boost_practice.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 305 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0 -I /usr/local/opt/boost/include -stdlib=libc++ --std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/eugene/project/private/cpp_practice/src -ferror-limit 19 -fmessage-length 100 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.13.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/_9/fqnfrxq56sj6gg95cc6nk1100000gn/T/boost_practice-bc871d.o -x c++ boost_practice.cpp
clang -cc1 version 9.0.0 (clang-900.0.39.2) default target x86_64-apple-darwin17.3.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/opt/boost/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.13.0 -o a.out /var/folders/_9/fqnfrxq56sj6gg95cc6nk1100000gn/T/boost_practice-bc871d.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::empty() const", referenced from:
      sample2() in boost_practice-bc871d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我也试过 -stdlib=libc++ 但还是一样。 ( linking with clang++ on OS X generates lots of symbol not found errors )

我也检查了 brew 安装的 boost 库。它是用 libc++ 构建的。

boost: stable 1.66.0 (bottled), HEAD
Collection of portable C++ source libraries
https://www.boost.org/
/usr/local/Cellar/boost/1.65.1 (12,681 files, 432.1MB) *
   Built from source on 2017-11-17 at 14:24:23 with: --c++11

我该如何解决这个问题?请帮忙。

附注 我在网上找到了同样的问题,没有答案。 https://tutel.me/c/programming/questions/45887506/c11+with+clang+on+macos+1012

#include <vector>
#include <functional>
#include <string>

int main() {
    std::vector<std::string> str_values = {"abc", "bde"};
    auto count2 = std::count_if(str_values.begin(), str_values.end(),
                                std::mem_fn(&std::string::empty));
    (void)count2;
    return 0;
}

它显示了同样的错误。

Undefined symbols for architecture x86_64: "std::__1::basic_string, std::__1::allocator >::empty() const", referenced from: _main in boost_practice.cpp.o ld: symbol(s) not found for architecture x86_64

最佳答案

https://github.com/rttrorg/rttr/issues/43 https://github.com/rttrorg/rttr/blob/master/src/rttr/detail/misc/standard_types.cpp

我已经找到了 rttr 的 standard_types.cpp 源文件中所述的答案。

// explicit instantiation of std::string needed, otherwise we get a linker error with clang on osx
// thats a bug in libc++, because of interaction with __attribute__ ((__visibility__("hidden"), __always_inline__)) in std::string
template class std::basic_string<char>;

关于c++ - std::string::empty 的 undefined symbol 错误; Mac OS High Sierra 上的 c++ 标准方法链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48273190/

相关文章:

c++ - 如何将流读入 Concurrency::streams::streambuf<uint8_t> 缓冲区;在 C++ 中

c++ - 如何安全地比较两个无符号整数计数器?

javascript - 尝试在 OSX 10.6 上的 JSTestDriver 中使用 --browser 时权限被拒绝

c - openSSL mac undefined symbol

macos - Git: 'rebase' 不是 git 命令。见 'git --help'

c++ - 为什么 C++ boost 包只包含 .hpp 文件?

python - 无法构建 boost python 库( fatal error : pyconfig. h:没有这样的文件或目录)

c++ - 在构造函数中初始化 const 字段

c++ - 动态加载库中的静态 C++ 对象是否在 dlopen() 返回之前初始化?

c++ - 从 2D C 列表创建 boost.geometry.model.polygon