标准::字符串::空的未定义符号错误;Mac OS High Sierra 上的 c++ 标准方法链接错误

Undefined symbol error for std::string::empty; c++ standard method linking error on Mac OS High Sierra

本文关键字:错误 标准 High Sierra c++ 链接 方法 OS 上的 字符串 未定义      更新时间:2023-10-16

我只是想编译下面的示例 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++,但它是一样的。(在OS X上与clang++链接会产生许多未找到符号的错误(

我也检查了我的提升库,该库是由brew安装的。它是用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;
}

它显示了相同的错误。

建筑的未定义符号x86_64: "std::__1::basic_string, std::__1::分配器>::empty(( const",引用自: _main in boost_practice.cpp.o LD:在建筑x86_64中找不到符号

https://github.com/rttrorg/rttr/issues/43https://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>;
相关文章: