免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10173 | 回复: 9
打印 上一主题 下一主题

__USE_GNU的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-12 23:23 |只看该作者 |倒序浏览
我写了个小程序,中间有如下的代码行:
fd=open(argv[1],O_RDONLY|O_NOATIME);
编译的时候总是报错
[root@localhost 03-cap_fowner]# gcc -o test test.c
test.c: In function 'main':
test.c:23: error: 'O_NOATIME' undeclared (first use in this function)
test.c:23: error: (Each undeclared identifier is reported 'O_NOATIME'only once
test.c:23: error: for each function it appears in.)
应该是不认识'O_NOATIME'这个常量
我在程序中定义了:
#define __USE_GNU 1
也引用了:
#include <fcntl.h>
在编译的时候加上选项:
[root@localhost 03-cap_fowner]# gcc -o test test.c -D__USE_GNU
test.c:1:1: warning: "__USE_GNU" redefined
<command line>:1:1: warning: this is the location of the previous definition
test.c: In function 'main':
test.c:23: error: 'O_NOATIME' undeclared (first use in this function)
test.c:23: error: (Each undeclared identifier is reported only once
test.c:23: error: for each function it appears in.)
结果还是一样的错!
请指教一下这是怎么弄的阿!??
我的系统是RHEL 5.1  gcc版本 :4.1..2

论坛徽章:
0
2 [报告]
发表于 2008-12-13 00:24 |只看该作者
我在ubuntu下编译没问题

论坛徽章:
0
3 [报告]
发表于 2008-12-13 00:30 |只看该作者
程序里 #define __USE_GNU 1 和编译时加 -D__USE_GNU=1 只需要其中一个。

论坛徽章:
0
4 [报告]
发表于 2008-12-13 00:53 |只看该作者

回复 #1 basten54188 的帖子

我的版本不支持O_NOATIME。但是从网上的一些资料可以看到:

O_NOATIME
    (Since Linux 2.6. Do not update the file last access time when
    the file is read(2). This flag is intended for use by indexing or
    backup programs, where its use can significantly reduce the amount
    of disk activity. This flag may not be effective on all
    filesystems. One example is NFS, where the server maintains the
    access time.

The O_NOATIME, O_NOFOLLOW, and O_DIRECTORY flags are Linux-specific.
One may have to define the _GNU_SOURCE macro to get their definitions.

来自:https://mailman.cs.tut.fi/pipermail/aide/2005-August/000149.html

通过这个来看,应该是2.6.8以后的内核才支持的功能。

来自:http://sourceware.org/ml/libc-hacker/2004-08/msg00068.html

这个是04年的帖子,说明04年以后发布的C库才支持。

我的系统里:

#ifdef __USE_GNU
# define O_DIRECT    040000 /* Direct disk access.  */
# define O_DIRECTORY    0200000 /* Must be a directory.  */
# define O_NOFOLLOW 0400000 /* Do not follow links.  */
#endif

对应的头文件为: /usr/include/bits/fcntl.h

不知道你的系统里是否支持,可以通过fcntl.h这个头文件判断。
同时注意到其前后的 #ifdef ,在你的代码中把那个宏#define一下就应该可以。

论坛徽章:
0
5 [报告]
发表于 2008-12-13 10:25 |只看该作者

回复 #4 samon_fu 的帖子

说的很对!我都知道!但在我系统里面就是不行!

我系统里面/usr/include/fcntl.h里面有#include <bits/fcntl.h>的include
/usr/include/bits/fcntl.h中也有
#ifdef __USE_GNU
# define O_DIRECT    040000 /* Direct disk access.  */
# define O_DIRECTORY    0200000 /* Must be a directory.  */
# define O_NOFOLLOW 0400000 /* Do not follow links.  */
#endif
我把他们注释掉,程序就可以编译了!
/*#ifdef __USE_GNU*/
# define O_DIRECT    040000 /* Direct disk access.  */
# define O_DIRECTORY    0200000 /* Must be a directory.  */
# define O_NOFOLLOW 0400000 /* Do not follow links.  */
/*#endif*/

但是这样问题被回避了,无论是在源文件中加#include<fcntl.h>,还是加#define __USE_GNU 1,或者编译时加选项-D__USE_GNU都不能编译成功,也就是说__USE_GNU貌似不能被定义??!

很诡异!谁能指教??

[ 本帖最后由 basten54188 于 2008-12-13 10:27 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2008-12-13 11:28 |只看该作者
hi
换成-D_GNU_SOURCE试试?

论坛徽章:
0
7 [报告]
发表于 2008-12-13 12:22 |只看该作者
谢谢 !这个成!

论坛徽章:
0
8 [报告]
发表于 2008-12-13 19:13 |只看该作者

回复 #7 basten54188 的帖子

说的不对,删掉了

之前理解有误,不好意思,请看下面的链接吧。

http://gcc.gnu.org/ml/fortran/2005-10/msg00365.html

[ 本帖最后由 samon_fu 于 2008-12-13 19:43 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2010-01-28 13:08 |只看该作者
我也遇到这个问题
估计是内核版本不支持 但是我是编译busybox的时候产生的  我想把它解决 不想屏蔽

论坛徽章:
0
10 [报告]
发表于 2010-01-28 13:15 |只看该作者
后来发现是fcntl.h中
#ifdef __USE_GNU
# define O_DIRECT     040000    /* Direct disk access.    */
# define O_DIRECTORY    0200000    /* Must be a directory.     */
# define O_NOFOLLOW    0400000    /* Do not follow links.     */
# define O_NOATIME 01000000 /* Do not set atime. */
#endif


原来是需要定义__USE_GNU,不过编译是加入-D__USE_GNU也没用,结果一样,最后发现是在编译是加入-D_GNU_SOURCE就行了,比如:gcc -o open open.c -D_GNU_SOURCE

可能内核版本低 导致 fcntl.h中 没有定义O_NOATIME  一般添加就OK 了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP