无法使用_FILE_OFFSET_BITS = 64进行编译 [英] Cannot compile with _FILE_OFFSET_BITS = 64

查看:137
本文介绍了无法使用_FILE_OFFSET_BITS = 64进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

10 #define _FILE_OFFSET_BITS 64

11

12 int main(无效){

13 int fd;

14

15 if((fd = open(" file.hole",O_WRONLY | O_CREAT,0664))< 0){

16 perror("" ;打开:);

17退出(-1);

18}

19

20 if(写(fd," a",1)!= 1){

21 perror(写:);

22退出(-1 );;

23}

24

25 if(lseek(fd,4 * 1024 * 1024 * 1024,SEEK_SET)== -1 ){//整数

溢出

26 perror(" lseek:");

27退出(-1);

28}

29

30 if(写(fd," A",1)!= 1){

31 perror(写:);

32退出(-1);

33}

34
35 close(fd);

36

37退出(0);

38}

在这个exa中嗯,虽然我明确定义了_FILE_OFFSET_BITS 64,但是我们在lseek64的手册中得到警告整数溢出

,我看到了:

------------------------------------------------- ------------------------------------------------ <无线电通信/>
lseek

原型:


off_t lseek(int fd,off_t offset,int whence);


库例程lseek()使用类型off_t。这是一个32位体系结构上的32位有符号类型的b
,除非有一个与b / b
一起使用


#define _FILE_OFFSET_BITS 64

在这种情况下它是64位签名类型。

---------------- -------------------------------------------------- -------------------------------

为什么我收到这样的警告?

10 #define _FILE_OFFSET_BITS 64
11
12 int main(void) {
13 int fd;
14
15 if ((fd = open("file.hole", O_WRONLY | O_CREAT, 0664)) < 0) {
16 perror("open:");
17 exit(-1);
18 }
19
20 if (write(fd, "a", 1) != 1) {
21 perror("write:");
22 exit(-1);
23 }
24
25 if (lseek(fd, 4*1024*1024*1024, SEEK_SET) == -1) { // integer
overflow
26 perror("lseek:");
27 exit(-1);
28 }
29
30 if (write(fd, "A", 1) != 1) {
31 perror("write:");
32 exit(-1);
33 }
34
35 close(fd);
36
37 exit(0);
38 }
In this example, although I explicitly define _FILE_OFFSET_BITS 64, i
get the warning"integer overflow"
in the manual of lseek64, I saw this:
-------------------------------------------------------------------------------------------------
lseek
Prototype:

off_t lseek(int fd, off_t offset, int whence);

The library routine lseek() uses the type off_t. This is a
32-bit signed type on 32-bit architectures, unless one com-
piles with

#define _FILE_OFFSET_BITS 64

in which case it is a 64-bit signed type.
-------------------------------------------------------------------------------------------------
SO why i got such warning?

推荐答案

2007年12月31日星期一14:25:10 +0800,Scott.zhou写道:
On Mon, 31 Dec 2007 14:25:10 +0800, Scott.zhou wrote:

25 if(lseek(fd,4 * 1024 * 1024 * 1024,SEEK_SET)== -1){//整数

溢出


In这个例子,虽然我明确定义了_FILE_OFFSET_BITS 64,但是我们获得了警告整数溢出。
25 if (lseek(fd, 4*1024*1024*1024, SEEK_SET) == -1) { // integer
overflow

In this example, although I explicitly define _FILE_OFFSET_BITS 64, i
get the warning"integer overflow"



4是一个int。 1024是一个int。 1024是一个int。 1024是一个int。当你将
乘以整数时,得到一个int,结果必须在

和int的范围内。即使该int稍后将转换为另一种类型。你首先需要将4转换为更宽的类型,然后开始相乘。

4 is an int. 1024 is an int. 1024 is an int. 1024 is an int. When you
multiply ints, you get an int, and the result must be within the range of
an int. Even if that int would later be converted to another type. You
first need to convert the 4 to a wider type, and then start multiplying.




" Scott.zhou" < rw ***** @ gmail.com写信息

news:fl ********** @ news.cn99.com ...

"Scott.zhou" <rw*****@gmail.comwrote in message
news:fl**********@news.cn99.com...

10 #define _FILE_OFFSET_BITS 64

11

12 int main(void){

13 int fd ;

14

15 if((fd = open(" file.hole",O_WRONLY | O_CREAT,0664))< 0){

16 perror(open:);

17退出(-1);

18}

19

20 if(write(fd," a",1)!= 1){

21 perror(write:);

22退出(-1);

23}

24

25 if(lseek(fd,4 * 1024 * 1024 * 1024) ,SEEK_SET)== -1){//整数

溢出
10 #define _FILE_OFFSET_BITS 64
11
12 int main(void) {
13 int fd;
14
15 if ((fd = open("file.hole", O_WRONLY | O_CREAT, 0664)) < 0) {
16 perror("open:");
17 exit(-1);
18 }
19
20 if (write(fd, "a", 1) != 1) {
21 perror("write:");
22 exit(-1);
23 }
24
25 if (lseek(fd, 4*1024*1024*1024, SEEK_SET) == -1) { // integer
overflow



编译器无法自动转换为64位类型。因此,通过将其中一个常量设置为64位类型,使其显式化为
。这使整个

表达式为64位。


= 4 * 1024 * 1024 * 1024LL //由LL后缀构成的64位常量。

No automatic conversion to 64 bit type is done by compiler. So make it
explicit by making one of the constants as 64 bit type. This makes entire
expression 64 bit.

=4*1024*1024*1024LL // 64 Bit constant made by LL suffix.


Scott.zhou大致写道:
Scott.zhou wrote roughly:

lseek64(fd,4 * 1024 * 1024 * 1024,SEEK_SET)//整数溢出
lseek64(fd, 4*1024*1024*1024, SEEK_SET) // integer overflow



其他人已经解释了发生了什么,但我会建议另一种方法

无法使用C99的长型将表达式转换为

off_t,它将自动成为正确的64位类型,无论您使用C89还是C99,无论是否使用$ b $。

Others already explained what is happening, but I''d suggest another approach
that works without C99''s long long type simply cast the expression to
off_t, which will automatically be the correct 64 bit type, regardless of
whether you use C89 or C99.


在这个例子中,虽然我明确地定义了_FILE_OFFSET_BITS 64,但是我们获得了警告整数溢出。 [...]
In this example, although I explicitly define _FILE_OFFSET_BITS 64, i
get the warning"integer overflow" [...]



我希望你也能理解问题的来源!这一点已经与lseek()无关,而是与C处理算术的方式无关,特别是它没有突然切换到更大的整数类型。


Uli

I hope you also understand where the problem comes from! The point has
nothing to do with lseek() but rather with how C handles arithmetic, in
particular that it doesn''t suddenly switch to a bigger integer type.

Uli


这篇关于无法使用_FILE_OFFSET_BITS = 64进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆