【发布时间】:2016-01-20 18:15:33
【问题描述】:
我在使用 gcc 时遇到了编译问题。
假设以下程序:
#include <stdio.h>
int test(const char *fname) {
FILE *fh = fopen(fname, "rb");
int tmp;
if (fread(&tmp, sizeof(tmp), 1, fh) < 1) {
tmp = 0;
}
fclose(fh);
return tmp;
}
int main(void) {
printf("%d\n", test("test.txt"));
return 0;
}
还有文件test.txt:
11111111111111111111111111111111...
当然,这个程序很笨,但它确实有效:
user@ubuntu:~/tmp/optxx$ gcc -O3 -Wall -Wextra test.c
user@ubuntu:~/tmp/optxx$ ./a.out
825307441
我们稍微修改一下(只给test-function添加一个属性):
#include <stdio.h>
int __attribute__((optimize("O0"))) test(const char *fname) {
FILE *fh = fopen(fname, "rb");
int tmp;
if (fread(&tmp, sizeof(tmp), 1, fh) < 1) {
tmp = 0;
}
fclose(fh);
return tmp;
}
int main(void) {
printf("%d\n", test("test.txt"));
return 0;
}
函数test 不应再优化。但是现在编译失败了:
user@ubuntu:~/tmp/optxx$ gcc -Wall -Wextra -O3 test.c
In file included from /usr/include/stdio.h:936:0,
from test.c:1:
In function ‘fread’,
inlined from ‘test’ at test.c:6:9:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:293:9: warning: call to ‘__fread_chk_warn’ declared with attribute warning: fread called with bigger size * nmemb than length of destination buffer
return __fread_chk_warn (__ptr, __bos0 (__ptr), __size, __n, __stream);
^
我收到警告,通常我用-Werror 编译,所以我不喜欢警告。
使用的gcc版本:
user@ubuntu:~/tmp/optxx$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.3.1-6ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.3.1 20160119 (Ubuntu 5.3.1-6ubuntu2)
很遗憾,我无法修复警告:(
也许有人知道为什么会这样?
-编辑-
一个(肮脏的)黑客也可以删除这个警告:)
-编辑-
可能我的 linux 出了点问题,因为它似乎对其他所有人都有效。 Anayway 一个肮脏的黑客/修复将是在 fread 调用之前添加此代码:
static size_t fread_wrapper(void *ptr, size_t size, size_t count, FILE *stream) {
return fread(ptr, size, count, stream);
}
#define fread(ptr, size, count, stream) fread_wrapper((ptr), (size), (count), (stream))
【问题讨论】:
-
gcc 4.4.5 对我来说没有错误。
-
不幸的是,
gcc 4.6.4也出现错误。我希望这不是 gcc 的错误。 -
使用 gcc 5.3.0 对我来说效果很好!
-
我在 RHEL 5 机器(Linux 主机名 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 EST 2008 x86_64 x86_64 x86_64 GNU/Linux)上尝试了 GCC 版本 4.1。 2, 4.5.1, 4.7.1, 5.2.0, 5.3.0 带有选项:
/path/to/version/bin/gcc -std=c99 -O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition so-34907503-b.c -o so-34907503-b' and after addingstatic` 在test之前(避免-Wmissing-prototypes),它编译没有问题。 -
在 gcc 4.6.3 上测试,重现没有问题
标签: c gcc optimization fread