c++ - clang++ 3.2 链接器找不到 C++ stdlib

标签 c++ linker clang undefined-reference libc++

我已经在我的 Ubuntu 13.04 机器上安装了 clang 3.2,并编译和构建了 libc++,一切就绪。 但是,当我尝试链接我的(非常简单的)代码时,链接器报告对 std::cout 等的引用未定义。

如果有人能建议我如何解决这个问题,我将不胜感激——我已经尝试了所有我能想到的方法。

命令和输出在这里:

$ clang++ -v -stdlib=libc++ -lpthread -ldl sqlite3/sqlite3.o src/world.o -o bin/world

Ubuntu clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)

Target: x86_64-pc-linux-gnu

Thread model: posix

 "/usr/bin/ld" -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o bin/world /usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../.. -L/lib -L/usr/lib -lpthread -ldl sqlite3/sqlite3.o src/world.o -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crtn.o

src/world.o: In function `main':

/home/douglivesey/work/home/cpp/clang/biots/src/world.cpp:17: undefined reference to `std::cout'

/home/douglivesey/work/home/cpp/clang/biots/src/world.cpp:17: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'

/home/douglivesey/work/home/cpp/clang/biots/src/world.cpp:17: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'

/home/douglivesey/work/home/cpp/clang/biots/src/world.cpp:17: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'

src/world.o: In function `__cxx_global_var_init':

/usr/include/c++/4.7.3/iostream:74: undefined reference to `std::ios_base::Init::Init()'

/usr/include/c++/4.7.3/iostream:74: undefined reference to `std::ios_base::Init::~Init()'

clang: error: linker command failed with exit code 1 (use -v to see invocation)

make: *** [bin/world] Error 1

最佳答案

错误显示来自 GCC 的 libstdc++ 的 header 和符号,表明 world.o 是使用 -stdlib=libstdc++ 构建的,但您正在链接 -stdlib=libc++ 这是不兼容的。

您需要始终使用相同的 -stdlib 选项。

关于c++ - clang++ 3.2 链接器找不到 C++ stdlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16770751/

相关文章:

c++ - boost::format 和自定义打印标准容器

compiler-errors - Fortran & Matlab 链接—— undefined symbol

iphone - 用于 iOS 开发的 LLVM 与 GCC

c++ - stringstream 字符串后有一个单词的奇怪行为

c++ - 如何将结构元素设置为所需的偏移量

c++ - 为什么这会收敛到 3 而不是 pi?

c++ - 无法分配对象,因为它的复制赋值运算符被隐式删除错误

c++ - 如何将动态 dll 转换为静态库?

ios - Xcode 5 在静态库中找不到 armv7s 架构,即使文件和 lipo 说它在那里

c++ - 如何在 Bazel 中禁用 C/C++ `-Werror` 构建错误? (又名 : how to turn OFF specific warnings already turned on by `-Wall -Werror` )