使用O2的clang ++的未定义引用 [英] Undefined reference with clang++ with O2

查看:204
本文介绍了使用O2的clang ++的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目上尝试使用CLang 3.4和libc ++,但在发布模式下出现奇怪的链接错误:

I'm trying CLang 3.4 and libc++ on a project and I get strange linking errors in release mode:

/home/wichtounet/dev/eddic/src/ast/Operator.cpp:17: error: undefined reference to
'std::__1::basic_ostream<char, std::__1::char_traits<char>>&
 std::__1::operator<< <char, std::__1::char_traits<char>, std::__1::allocator<char>>(
   std::__1::basic_ostream<char, std::__1::char_traits<char>>&,
   std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&
 )'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

一切正常。该程序在调试模式下正确链接,但是在使用O2时无法正常链接。在O0,O1和Os中,一切正常,但在O2,O3和Os中却没有链接。我也在LTO模式下尝试过,并且工作正常。

Everything compiles fine. The program links correctly in debug mode, but doesn't when I use O2. In O0, O1 and Os everything works fine, but it doesn't links in O2, O3, Os. I also tried in LTO mode and it works fine.

我尝试了两个版本的libc ++,但无济于事。

I tried two versions of libc++ but to no avail.

代码对我来说似乎并不糟糕:

The code does not seems bad to me:

std::ostream& ast::operator<< (std::ostream& stream, ast::Operator op){
    std::string value = "asd";
    return stream << value;
}

但是我没有一个简单的例子可以引起问题。

but I haven't a simple example causing the problem.

clang ++用于构建和链接代码。我使用 -std = c ++ 1y -stdlib = libc ++进行编译,并使用相同的内容和一些库内容进行链接。

clang++ is used to both build and links the code. I used "-std=c++1y -stdlib=libc++" to compile and the same plus some library stuff to link.

可能会导致什么?

编辑:在释放模式下完全调用链接步骤:

Full invocation of link step in release mode:

clang++ -v -use-gold -Iinclude -std=c++1y -stdlib=libc++ -Wextra -Wall -Qunused-arguments -Wuninitialized -Wsometimes-uninitialized -Wno-long-long -Winit-self -Wdocumentation -pedantic -isystem /home/wichtounet/build/modular-boost//include -L /home/wichtounet/build/modular-boost//lib -lboost_program_options -g -DLOGGING_DISABLE -DNDEBUG -O3 -march=native -fvectorize -fslp-vectorize-aggressive -fomit-frame-pointer -o release/bin/eddic "TONS OF DOT O"
clang version 3.4 (tags/RELEASE_34/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
Selected GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2
 "/usr/bin/x86_64-pc-linux-gnu-ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o release/bin/eddic /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/crtbegin.o -L/home/wichtounet/build/modular-boost//lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2 -L/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/../../../../x86_64-pc-linux-gnu/lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/../../.. -L/lib -L/usr/lib -lboost_program_options "TONS OF DOT O" -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/crtend.o /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.2/../../../../lib64/crtn.o
src/ast/Operator.cpp:15: error: undefined reference to 'std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)


推荐答案

我在使用g ++长期编译的代码中遇到了这个问题,尝试使用clang ++进行编译。相同的症状:对std :: __ 1 :: basic_ostream< ...>的未定义引用在从编译器选项中删除-O2时神奇地消失了。我发现的所有类似报告都是关于错误地使用clang而不是clang ++来编译C ++代码,但对我而言并非如此:我使用clang ++。

I was having this problem with code that has long compiled with g++, attempting to compile with clang++. Same symptoms: undefined references to std::__1::basic_ostream<...> that magically went away when -O2 was removed from the compiler options. All similar reports I was finding were about erroneously using clang instead of clang++ to compile C++ code, but that wasn't the case for me: I was using clang++.

I终于能够确定问题(对我而言)是在我的标头中使用 #include< iosfwd> (仅具有iostream转发声明,以减少编译时间) .h)文件声明一个类,但未在实现该类的相应源(.cpp)文件中使用 #include< iostream>

I was finally able to determine that the issue (for me) was using #include <iosfwd> (which has iostream forward declarations only, to reduce compile times) in my header (.h) file declaring a class but not using #include <iostream> in the corresponding source (.cpp) file implementing the class.

一旦我将 #include< iostream> 添加到源文件并重新编译,这些未定义的引用错误就消失了。

Once I added the #include <iostream> to the source file and recompiled, these undefined reference errors went away.

这篇关于使用O2的clang ++的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆