警告:隐式声明函数'getresuid'(和'seteuid')

Igo*_*gor 3 c linux gcc compiler-warnings gcc-warning

我想摆脱警告.当我编译源代码时

gcc -Wall -ansi -o test test.c  
Run Code Online (Sandbox Code Playgroud)

我回来了

test.c: In function ‘main’:
test.c:12: warning: implicit declaration of function ‘getresuid’
test.c:14: warning: implicit declaration of function ‘seteuid’
Run Code Online (Sandbox Code Playgroud)

当我编译它没有-ansi开关

gcc -Wall -o test test.c 
Run Code Online (Sandbox Code Playgroud)

我在终端上看到了

test.c: In function ‘main’:
test.c:12: warning: implicit declaration of function ‘getresuid’
Run Code Online (Sandbox Code Playgroud)

我想用-ansi开关并摆脱警告.我怎样才能实现目标?

/*  this is the test.c */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#define __USE_GNU 1
#define __USE_BSD 1

int main()
{
   static uid_t euid, ruid, suid;

   getresuid(&ruid, &euid, &suid);  

   seteuid(getuid()); 

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

环境:

CentOS 6.3 32位
gcc版本4.4.7 20120313(Red Hat 4.4.7-3)(GCC)

Yu *_*Hao 8

getresuid()并且seteuid()是GNU扩展功能,添加

#define _GNU_SOURCE
Run Code Online (Sandbox Code Playgroud)

包括所有标题之前,或添加-D_GNU_SOURCEGCC选项.

你不应该__USE_GNU直接定义宏,它应该只在glibc内部使用.

  • 它输出相同的警告,至少对我来说是这样。我所做的是“#include &lt;unistd.h&gt;” (2认同)