[c++] How to resolve compiler warning 'implicit declaration of function memset'

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

#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.

This question is related to c++ c

The answer is


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

#define _GNU_SOURCE

It helped me with pipe2 function.


memset requires you to import the header string.h file. So just add the following header

#include <string.h>
...

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

Source: Wikipedia Man Page


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 ).


You need:

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

in your code.

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