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

问题描述 投票:2回答:2

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

gcc -Wall -ansi -o test test.c  

我回来了

test.c: In function ‘main’:
test.c:12: warning: implicit declaration of function ‘getresuid’
test.c:14: warning: implicit declaration of function ‘seteuid’

当我编译它没有-ansi开关

gcc -Wall -o test test.c 

我在终端上看到了

test.c: In function ‘main’:
test.c:12: warning: implicit declaration of function ‘getresuid’

我想用-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;
}

环境:

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

c linux gcc compiler-warnings gcc-warning
2个回答
7
投票

getresuid()seteuid()是GNU扩展函数,添加

#define _GNU_SOURCE

在包含所有标题之前,或在GCC选项中添加-D_GNU_SOURCE

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


3
投票

我通过添加#include <unistd.h>解决了

© www.soinside.com 2019 - 2024. All rights reserved.