简体   繁体   English

为什么使用带有以下LLVM库代码的带有-O3 segfault的g ++

[英]Why g++ with -O3 segfault with the following LLVM library code

So I want to use the llvm::Twine string chunk class. 所以我想使用llvm::Twine字符串块类。

I have the following sample: 我有以下示例:

#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;
}

It runs with clang++ with -O3 and g++ with -O0 but segfault with g++ with -O3 . 它运行与clang++-O3g++-O0但与段错误g++-O3 I tried this code parts different versions of clang library from 3.4-3.9 and tried with g++ 4.8.4 , g++ 4.8.5 and mingw-5.3.0 . 我尝试了这段代码部分的3.4-3.9版本的clang库,并尝试了g++ 4.8.4g++ 4.8.5mingw-5.3.0

You need the llvm library and link the code with -lLLVMSupport -lLLVMCore and the others from llvm-config --ldflags 您需要llvm库,并将代码与-lLLVMSupport -lLLVMCore以及其他来自llvm-config --ldflags

From Twine documentation : Twine文档中

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. Twine不能直接使用,也不应该存储,它的实现依赖于存储指向临时堆栈对象的指针的能力,该指针可以在语句的末尾释放。 Twines should only be used accepted as const references in arguments, when an API wishes to accept possibly-concatenated strings. 仅当API希望接受可能串联的字符串时,才应将Twines用作参数中的const引用。

In the other words, Twine object doesn't own its parts, so they are destroyed at the end of statement. 换句话说,Twine对象不拥有其各个部分,因此它们在语句末尾被销毁。

A correct usage would be: 正确的用法是:

#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;
}  

It looks like you are attempting to compile some code with gcc , and link it with libraries that were built with a different compiler; 看起来您正在尝试使用gcc编译一些代码,并将其链接到使用其他编译器构建的库。 namely llvm . llvm

Different compilers will often use different internal ABI s. 不同的编译器通常会使用不同的内部ABI Generally, code compiled by one C++ compiler cannot be linked with code that was built a different compiler, unless there's an explicit ABI compatibility guarantee. 通常,除非有明确的ABI兼容性保证,否则一个C ++编译器编译的代码不能与其他编译器构建的代码链接。 TMK, there is none, between gcc and llvm. TMK,在gcc和llvm之间没有。 Even though the actual link might succeed, the results will not have a defined behavior. 即使实际链接可能成功,结果也不会具有定义的行为。

In fact, code built by one version of gcc will often not be linkable with code built with a different version of gcc (many versions of gcc do have certain ABI compatibility guarantees, but not with every other version of gcc). 实际上,由一个版本的gcc构建的代码通常无法与由另一个版本的gcc构建的代码链接(许多版本的gcc确实具有某些ABI兼容性保证,但不能与所有其他版本的gcc兼容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM