我只是想编译下面的示例 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;
}
它显示了同样的错误。
最佳答案
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/