日期:2014-05-16  浏览次数:20673 次

关于_sysctl,man给出的example我编译不了。
大家看看,我的系统是FC6
#include   <linux/unistd.h>
#include   <linux/types.h>
#include   <linux/sysctl.h>

_syscall1(int,   _sysctl,   struct   __sysctl_args   *,   args);
int   sysctl(int   *name,   int   nlen,   void   *oldval,   size_t   *oldlenp,void   *newval,   size_t   newlen)
{
struct   __sysctl_args   args={name,nlen,oldval,oldlenp,newval,newlen};
      return   _sysctl(&args);
}
#define   SIZE(x)   sizeof(x)/sizeof(x[0])
#define   OSNAMESZ   100
char   osname[OSNAMESZ];
int   osnamelth;
int   name[]   =   {   CTL_KERN,   KERN_OSTYPE   };
main()
{
osnamelth   =   sizeof(osname);
if   (sysctl(name,   SIZE(name),   osname,   &osnamelth,   0,   0))
perror( "sysctl ");
else
printf( "This   machine   is   running   %*s\n ",   osnamelth,   osname);
return   0;
}

居然提示:expected   declaration   specifiers   or   '... '   before   '_sysctl '
在红帽子下面提示_sysctl未定义,请高手解答

------解决方案--------------------
在我的suse中在include最后加上一行
#include <errno.h>
后编译成功。就是简单的编译:gcc -o test test.c

_syscall1(int, _sysctl, struct __sysctl_args *, args);
这一行本身是一个宏,展开后就是对_sysctl的声明。

你确定一下这个文件是否存在:
/usr/include/linux/sysctl.h