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

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.

You need:
#include <string.h> /* memset */
#include <unistd.h> /* close */
in your code.
References: POSIX for close, the C standard for memset.

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

memset requires you to import the header string.h file. So just add the following header
#include <string.h>
...

Try to add next define at start of your .c file:
#define _GNU_SOURCE
It helped me with pipe2 function.

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

Related

Clang/gcc use different c++ standard for header and main

I was using CImg for picture processing, which don't compile for C++11 or above:
CImg.h:48902:22: error: there are no arguments to 'pclose' that depend on a template parameter, so a declaration of 'pclose' must be available [-fpermissive]
But my program which includes CImg.h does use some C++11 feature. Can I use different C++ standard to process these two files? CImg.h itself is a header+code so I have to #include that.
Here is my test program:
#define cimg_display 0
#include "CImg.h"
int main(){}
CImg.h comes from the lastest version from http://cimg.eu/.

Trying to port a game to Linux, calling XInitThreads gets me a syntax error

Here's how the offending line looks like in the code:
#ifdef SFML_SYSTEM_LINUX
XInitThreads();
#endif
I've invoked the requisite libraries at the beginning of the code as follows:
#ifdef SFML_SYSTEM_LINUX
#include <X11/Xlib.h>;
#include <unistd.h>
#include <pwd.h>
#include <iostream>
#include <stdlib.h>
#endif
It gives me this error in Code::Blocks:
error: expected constructor, destructor, or type conversion before ‘;’ token
This error references line 111 in the code, which is the XInitThreads call in the first function I quoted above.
Am I missing something? I've tried setting the linker to include the X11 library, but so far it hasn't changed anything.
EDIT: Here's more code from the beginning of game.cpp - http://pastebin.com/7D2WLRM7
It seems that you're trying to "call" this function outside of any function or method block.
Your compiler can't understand what you're trying to do. Move these lines into Game::Init perhaps?

wcscpy_s not working for Win32 Combo Box

I'm trying to create a combo box in Win32 by following this msdn tutorial.
When I implement step 2 and try to compile, I get the following error:
error: 'wcscpy_s' was not declared in this scope
wcscpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Planets[k]);
I've included the following header files, hoping to solve this issue:
#include <string.h>
#include <wchar.h>
#include <windows.h>
#include <CommCtrl.h>
#include <math.h>
#include <objbase.h>
Can someone help me understand why I'm getting this error? Thanks in advance.
From cpp-reference
As with all bounds-checked functions, wcscpy_s is only guaranteed to be available if STDC_LIB_EXT1 is defined by the implementation and if the user defines STDC_WANT_LIB_EXT1 to the integer constant 1 before including wchar.h.

Headers for C POSIX functions

Where or how can I find the correct C headers to include in a C++ program to obtain the declaration of C functions declared in a POSIX compliant environment?
I'm asking this because I needed to use the open() system call in my C++ program for my purposes, so I initially tried to include the headers mentioned in the online documentation about open() (in the SYNOPSIS section), which are sys/stat.h and fcntl.h. However when trying to compile, the compiler complained that open() was not declared. After a search on google, I found that another possibility was unistd.h. I tried using that header and the program compiled. So I went back to the POSIX documentation to read more about unistd.h to check if open() was mentioned there, but I could not find anything about it.
What am I doing wrong? Why is there this discrepancy between the POSIX documentation and my GCC environment?
On my Linux Debian/Sid, the man 2 open page states:
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
So you need to include all three above files. And open is declared in /usr/include/fcntl.h but needs declaration from the other two includes.
And the following test file
/* file testopen.c */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int
testopen (void)
{
return open ("/dev/null", O_RDONLY);
}
compiles with gcc -Wall -c testopen.c without any warnings.

Compiler error: memset was not declared in this scope

I am trying to compile my C program in Ubuntu 9.10 (gcc 4.4.1).
I am getting this error:
Rect.cpp:344: error: ‘memset’ was not declared in this scope
But the problem is I have already included in my cpp file:
#include <stdio.h>
#include <stdlib.h>
And the same program compiles fine under Ubuntu 8.04 (gcc 4.2.4).
Please tell me what am I missing.
You should include <string.h> (or its C++ equivalent, <cstring>).
Whevever you get a problem like this just go to the man page for the function in question and it will tell you what header you are missing, e.g.
$ man memset
MEMSET(3) BSD Library Functions Manual MEMSET(3)
NAME
memset -- fill a byte string with a byte value
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <string.h>
void *
memset(void *b, int c, size_t len);
Note that for C++ it's generally preferable to use the proper equivalent C++ headers, <cstring>/<cstdio>/<cstdlib>/etc, rather than C's <string.h>/<stdio.h>/<stdlib.h>/etc.