为什么 g++ 与 -O3 段错误与以下 LLVM 库代码
Why g++ with -O3 segfault with the following LLVM library code
所以我想使用 llvm::Twine
字符串块 class。
我有以下示例:
#include <llvm/ADT/Twine.h>
#include <iostream>
int main()
{
llvm::Twine twine1 = llvm::Twine("aaaa") + "bbbb" + "cccc" + "dddd";
llvm::Twine twine2 = llvm::Twine(twine1) + "dddd" + "eeee";
std::cout << twine1.str() << std::endl;
std::cout << twine2.str() << std::endl;
return 0;
}
它以 clang++
和 -O3
运行,g++
和 -O0
运行,但在 g++
和 -O3
中出现段错误。我从 3.4-3.9 尝试了这段代码部分不同版本的 clang 库,并尝试使用 g++ 4.8.4
、g++ 4.8.5
和 mingw-5.3.0
.
您需要 llvm 库和 link -lLLVMSupport -lLLVMCore
的代码以及来自 llvm-config --ldflags
的其他代码
看起来您正在尝试使用 gcc
编译一些代码,并且 link 使用不同编译器构建的库;即 llvm
.
不同的编译器通常会使用不同的internal ABI。通常,由一个 C++ 编译器编译的代码不能 link 与由不同编译器构建的代码编辑,除非有明确的 ABI 兼容性保证。 TMK,gcc和llvm之间有none。即使实际的 link 可能成功,结果也不会具有定义的行为。
事实上,由一个版本的 gcc 构建的代码通常无法 link使用由不同版本的 gcc 构建的代码(许多版本的 gcc 确实有一定的 ABI 兼容性保证,但不是每一个其他版本的 gcc)。
A Twine is not intended for use directly and should not be stored, its
implementation relies on the ability to store pointers to temporary
stack objects which may be deallocated at the end of a statement.
Twines should only be used accepted as const references in arguments,
when an API wishes to accept possibly-concatenated strings.
换句话说,Twine 对象不拥有它的部分,所以它们在语句结束时被销毁。
正确的用法是:
#include <llvm/ADT/Twine.h>
#include <iostream>
void bar(const llvm::Twine& twine1, const llvm::Twine2& twine2){
std::cout << twine1.str() << std::endl;
std::cout << twine2.str() << std::endl;
}
void foo(const llvm::Twine& twine1){
bar(twine1, twine1 + "dddd" + "eeee");
}
int main()
{
foo(llvm::Twine("aaaa") + "bbbb" + "cccc" + "dddd");
return 0;
}
所以我想使用 llvm::Twine
字符串块 class。
我有以下示例:
#include <llvm/ADT/Twine.h>
#include <iostream>
int main()
{
llvm::Twine twine1 = llvm::Twine("aaaa") + "bbbb" + "cccc" + "dddd";
llvm::Twine twine2 = llvm::Twine(twine1) + "dddd" + "eeee";
std::cout << twine1.str() << std::endl;
std::cout << twine2.str() << std::endl;
return 0;
}
它以 clang++
和 -O3
运行,g++
和 -O0
运行,但在 g++
和 -O3
中出现段错误。我从 3.4-3.9 尝试了这段代码部分不同版本的 clang 库,并尝试使用 g++ 4.8.4
、g++ 4.8.5
和 mingw-5.3.0
.
您需要 llvm 库和 link -lLLVMSupport -lLLVMCore
的代码以及来自 llvm-config --ldflags
看起来您正在尝试使用 gcc
编译一些代码,并且 link 使用不同编译器构建的库;即 llvm
.
不同的编译器通常会使用不同的internal ABI。通常,由一个 C++ 编译器编译的代码不能 link 与由不同编译器构建的代码编辑,除非有明确的 ABI 兼容性保证。 TMK,gcc和llvm之间有none。即使实际的 link 可能成功,结果也不会具有定义的行为。
事实上,由一个版本的 gcc 构建的代码通常无法 link使用由不同版本的 gcc 构建的代码(许多版本的 gcc 确实有一定的 ABI 兼容性保证,但不是每一个其他版本的 gcc)。
A Twine is not intended for use directly and should not be stored, its implementation relies on the ability to store pointers to temporary stack objects which may be deallocated at the end of a statement. Twines should only be used accepted as const references in arguments, when an API wishes to accept possibly-concatenated strings.
换句话说,Twine 对象不拥有它的部分,所以它们在语句结束时被销毁。
正确的用法是:
#include <llvm/ADT/Twine.h>
#include <iostream>
void bar(const llvm::Twine& twine1, const llvm::Twine2& twine2){
std::cout << twine1.str() << std::endl;
std::cout << twine2.str() << std::endl;
}
void foo(const llvm::Twine& twine1){
bar(twine1, twine1 + "dddd" + "eeee");
}
int main()
{
foo(llvm::Twine("aaaa") + "bbbb" + "cccc" + "dddd");
return 0;
}