如何解决编译器警告'隐式声明函数memset'

hap*_*497 32 c c++

我的c代码使用'memset'和'close'.我补充说:

#include <stdio.h>
#include <glib.h>
#include <stdlib.h>
Run Code Online (Sandbox Code Playgroud)

但我仍然得到这些警告:

main.c:259: warning: implicit declaration of function ‘memset’
main.c:259: warning: incompatible implicit declaration of built-in function ‘memset’
main.c:268: warning: implicit declaration of function ‘close’
main.c:259: warning: incompatible implicit declaration of built-in function ‘close’
Run Code Online (Sandbox Code Playgroud)

你能告诉我怎样才能解决这些警告?

谢谢.

Alo*_*hal 72

你需要:

#include <string.h> /* memset */
#include <unistd.h> /* close */
Run Code Online (Sandbox Code Playgroud)

在你的代码中.

参考文献:POSIX for close,C标准memset.


Tab*_*tha 29

找出缺少的头文件的好方法:

 man <section> <function call>
Run Code Online (Sandbox Code Playgroud)

要查找该部分使用:

apropos <function call>
Run Code Online (Sandbox Code Playgroud)

例:

 man 3 memset
 man 2 send
Run Code Online (Sandbox Code Playgroud)

编辑以回应詹姆斯莫里斯:

  • 部分| 描述
  • 1常规命令
  • 2系统调用
  • 3 C库函数
  • 4特殊文件(通常是设备,在/ dev中找到的)和驱动程序
  • 5文件格式和约定
  • 6游戏和屏保
  • 7杂记
  • 8系统管理命令和守护程序

来源:维基百科人页

  • 也许解释为什么第3节和第2节? (2认同)
  • 通过给我们一个通用的解决方案而不仅仅是给出解决方案来教学+1 (2认同)

laz*_*ios 15

memset要求您导入头string.h文件.所以只需添加以下标题即可

#include <string.h>
...
Run Code Online (Sandbox Code Playgroud)


小智 5

尝试在.c文件的开头添加下一个定义:

#define _GNU_SOURCE
Run Code Online (Sandbox Code Playgroud)

它帮助我使用pipe2功能.