找到off_t大小

时间:2014-03-26 14:50:08

标签: c gcc large-files

我们一直在使用从autotools复制的trick来确定off_t大小以及我们是否需要定义_FILE_OFFSET_BITS = 64。然而,这个技巧似乎在最近的gcc(> = 4.6)中失败了。这是代码:

#include <sys/types.h>

int main(int argc, char **argv)
{
  /* Cause a compile-time error if off_t is smaller than 64 bits */
#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
  int off_t_is_large[ (LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1 ];  
  return 0;
}

来自我的debian / amd64&amp; debian / ppc安装在这里是我得到的(而不是编译时错误):

$ gcc-4.6 -O0 -m32 -o valid.o valid.c 
valid.c: In function ‘main’:
valid.c:7:3: warning: left shift count >= width of type [enabled by default]
valid.c:7:3: warning: left shift count >= width of type [enabled by default]
valid.c:7:3: warning: left shift count >= width of type [enabled by default]
valid.c:7:3: warning: left shift count >= width of type [enabled by default]

所以我的问题,我可以简单地用以下代码替换这个代码:

#include <sys/types.h>

int main(int argc, char **argv)
{
  /* Cause a compile-time error if off_t is smaller than 64 bits */
  int off_t_is_large[ sizeof(off_t) >= 8 ? 1 : -1 ];  
  return 0;
}

(额外)问题:这是gcc中的回归,还是初始代码只是依赖于破损的功能?

1 个答案:

答案 0 :(得分:3)

子表达式(off_t) 1 << 62)6.5.7p3

发生冲突
    "[...] If the value of the right operand is [...] greater 
    than or equal to the width of the promoted left operand, 
    the behavior is undefined." 

ref