我只是想编译下面的示例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
如何解决此问题?请帮忙。
P.S。 我在互联网上找到了同样的问题而没有回答。 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 :: allocator&gt; :: empty()const”,引自: 在boost_practice.cpp.o中的_main ld:找不到架构x86_64的符号
答案 0 :(得分:3)
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>;