-finline-functions破坏了我的代码

时间:2013-06-25 01:52:23

标签: c gcc

我注意到我的代码在-O3处断开,但在-O2处没有。我尝试逐个应用这些标志,然后我找到了

gcc -O1 -finline-functions code.c

符;和

gcc -O1 -fno-inline-functions code.c

作品。

通过“休息”,我的意思是它编译,但它运行不正确。

我应该在哪里查找此错误?我可以发布代码,但因为它的重量为1.5k SLOC,我宁愿不发布整个代码。

版本:

$ uname -a
Linux xxxx.xxxx 2.6.18-308.20.1.el5 #1 SMP x86_64 x86_64 x86_64 GNU/Linux
$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-54)

1 个答案:

答案 0 :(得分:3)

我弄明白了这个错误:

int foo() {
  return 5;
}
int bar() {
  foo(); // <-- no return statement!
}
int main() {
  printf("%d", bar());
}

即使bar()中没有返回语句,也请注意,值5已经在返回值寄存器中。 main()获取返回寄存器中的值,认为它来自bar,但实际上它来自foo。这是未定义的行为,但只有在编译器尝试内联bar()时才会爆炸。解决方案是在bar()

中返回

对于gcc来说,如果你打开-Wall

,它会发出警告
code.c:63: warning: control reaches end of non-void function

我通过手动将__attribute__ ((noinline))(感谢sjs!)应用于函数直到我的代码正常工作来找到这个