Problematic clang code generation with -O0

。_饼干妹妹 提交于 2019-12-11 11:05:37

问题


The following snippet:

#include <string>
#include <iostream>

int main()
{
    std::string s = std::to_string(5);
    std::cout << s << std::endl;
    return 0;
}

Fails to link with Clang 3.6 on windows (accompanied with gcc 4.8.2 headers and libraries) when given the following options:

clang++ -std=c++11 -static -O0 bug.cpp

Please note that with -O2 the snippet compiles and links fine, so i suspect it may be some kind of clang bug.

EDIT 1: I forgot to put the link error:

F:/Programs/LLVM/bin/../lib/gcc/i686-w64-mingw32/4.8.2\libstdc++.a(string-inst.o):(.text$_ZNSs12_S_constructIPcEES0_T_S1_RKSaIcESt20forward_iterator_tag[__ZNSs12_S_constructIPcEES0_T_S1_RKSaIcESt20forward_iterator_tag]+0x0): multiple definition of
`char* std::string::_S_construct<char*>(char*, char*, std::allocator<char> const&, std::forward_iterator_tag)'
C:\Users\THEART~1\AppData\Local\Temp\bug-b5df09.o:(.text[__ZNSs12_S_constructIPcEES0_T_S1_RKSaIcESt20forward_iterator_tag]+0x0): first defined here
F:/Programs/LLVM/bin/../lib/gcc/i686-w64-mingw32/4.8.2\libstdc++.a(string-inst.o):(.text$_ZNSsC2IPcEET_S1_RKSaIcE[__ZNSsC2IPcEET_S1_RKSaIcE]+0x0): multiple definition of `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<char*>(char*, char*, std::allocator<char> const&)'
C:\Users\THEART~1\AppData\Local\Temp\bug-b5df09.o:(.text[__ZNSsC2IPcEET_S1_RKSaIcE]+0x0): first defined here
F:/Programs/LLVM/bin/../lib/gcc/i686-w64-mingw32/4.8.2/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\THEART~1\AppData\Local\Temp\bug-b5df09.o: bad reloc address 0x10 in section `.text[__ZSt9to_stringi]'
collect2.exe: error: ld returned 1 exit status
clang++.exe: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

EDIT 2: I've been asked why i added the static flag in the compilation flags. My purpose was to generate full static linked executables without the need of some gcc dlls (libstdc++6.dll libwinpthread.dll etc) so i have mostly been compiling with this command:

clang++ -std=c++11 -static -static-libstdc++ -static-libgcc -O0  bug.cpp

that i've been using with gcc too to generate full static executables. After some experimenting i noticed that the combination of -static and -O0 flags is the problematic one so i omitted the others to not create any confusion.

来源:https://stackoverflow.com/questions/28963402/problematic-clang-code-generation-with-o0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!