C++ 如何解决编译器警告“隐式声明函数 memset”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2144617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:15:38  来源:igfitidea点击:

How to resolve compiler warning 'implicit declaration of function memset'

c++c

提问by hap497

My c code uses 'memset' and 'close'. And I have added:

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

#include <stdio.h>
#include <glib.h>
#include <stdlib.h>

But I still get these warnings:

但我仍然收到这些警告:

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'

Can you please tell me how can I resolve these warnings?

你能告诉我如何解决这些警告吗?

Thank you.

谢谢你。

回答by Alok Singhal

You need:

你需要:

#include <string.h> /* memset */
#include <unistd.h> /* close */

in your code.

在你的代码中。

References: POSIX for close, the C standard for memset.

参考资料: POSIX for close,C 标准 for memset.

回答by Tabitha

A good way to findout what header file you are missing:

找出丢失的头文件的好方法:

 man <section> <function call>

To find out the section use:

要找出该部分,请使用:

apropos <function call>

Example:

例子:

 man 3 memset
 man 2 send

Edit in response to James Morris:

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

  • Section | Description
  • 1 General commands
  • 2 System calls
  • 3 C library functions
  • 4 Special files (usually devices, those found in /dev) and drivers
  • 5 File formats and conventions
  • 6 Games and screensavers
  • 7 Miscellanea
  • 8 System administration commands and daemons
  • 节 | 描述
  • 1 一般命令
  • 2 系统调用
  • 3 C 库函数
  • 4 特殊文件(通常是设备,/dev 中的那些)和驱动程序
  • 5 文件格式和约定
  • 6 游戏和屏保
  • 7 杂项
  • 8 系统管理命令和守护进程

Source: Wikipedia Man Page

来源:维基百科手册页

回答by lazybios

memsetrequires you to import the header string.hfile. So just add the following header

memset需要你导入头string.h文件。所以只需添加以下标题

#include <string.h>
...

回答by rennnon

Try to add next define at start of your .c file:

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

#define _GNU_SOURCE

It helped me with pipe2 function.

它帮助我使用 pipe2 功能。

回答by Arrival

Old question but I had similar issue and I solved it by adding

老问题,但我有类似的问题,我通过添加解决了它

extern void* memset(void*, int, size_t);

or just

要不就

extern void* memset();

at the top of translation unit ( *.c file ).

在翻译单元( *.c 文件)的顶部。