gpt4 book ai didi

c++ - g++ 有问题吗?还有 clang++ 吗?带有 -O0 和 -O1 选项的 g++ 编译代码的行为不同对于带有 -O0 和 -O2 的 clang++ 也是如此

转载 作者:太空狗 更新时间:2023-10-29 20:41:26 25 4
gpt4 key购买 nike

下面的代码命名为“main000.cpp”文件,在linux机器上用后续命令行编译和执行:

g++  -std=c++11 -m32 -O0  main000.cpp ; ./a.out

并正常执行

如果编译并执行将 O0 更改为 O1

g++  -std=c++11 -m32 -O1  main000.cpp ; ./a.out

不起作用并生成堆栈转储

任何人都知道为什么?

#include <stdio.h>
#include <string>

using namespace std;
typedef unsigned int Token;

const Token tokenBase=0x80000000;

enum classId{
classNoId=tokenBase,
classPrimitiveId,
};

struct classInfo{classId id;wstring name;};

classInfo classInfos[]={
{classNoId,L"classNoID"},
{classPrimitiveId,L"classPrimitiveId"},
};

int main(){
int id=classInfos[0].id;
return 0*id;
}

最佳答案

对于 -O1-O2,这对我来说运行良好

  • Clang 3.2 和 GCC 4.8.1 (Windows)
  • Clang 3.3 和 GCC 4.7.3 (Linux)

所以这可能是您设置的问题。我建议在 gdb 中运行它并获取 backtrace

并确保您没有使用定义的冲突标识符链接库。

关于c++ - g++ 有问题吗?还有 clang++ 吗?带有 -O0 和 -O1 选项的 g++ 编译代码的行为不同对于带有 -O0 和 -O2 的 clang++ 也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21356275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com