Headers for C POSIX functions - c++

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.

Related

g++, Xerces 3.1, "memcpy was not declared in this scope"

In my makefile on this project, I am specifying 3 location for the compiler to find #includes.
INCLUDES=-I. -I/home/kelly/xerces/xerces-c-3.1.1/src -I/home/kelly/Utilities_New
When I compile the following sample code with the command line found in the comment:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <xercesc/util/XMLString.hpp>
using namespace std;
/*
g++ -I. -I/home/kelly/xerces/xerces-c-3.1.1/src -I/home/kelly/Utilities_New test.cpp
*/
int main(int argc, char* argv[])
{
cout << "this works" << endl;
}
In file included from /home/kelly/Utilities_New/string.h:5:0,
from /home/kelly/xerces/xerces-c-3.1.1/src/xercesc/framework/XMLBuffer.hpp:28,
from /home/kelly/xerces/xerces-c-3.1.1/src/xercesc/util/XMLString.hpp:26,
from test.cpp:7:
/home/kelly/Utilities_New/defs.h:26:21: fatal error: windows.h: No such file or directory
compilation terminated.
Clearly the compiler has decided that when it processes the Xerces #includes and finds string.h in the Utilities_New directory, it has found what it needs and stops there.
This other string.h was written by another programmer and I am attempting to use his library.
I thought that the standard locations were searched first. I'm a little lost here. I may be missing something super obvious to you all.
Also, wasn't there some rule about #include files that had <> vs. "" around them and where the compiler was supposed to look?
The standard locations are searched last.
#include "blah" is identical to #include <blah> unless blah is found in the same directory as the file that tries to include it. (And unless you use the rare and gcc-specific -iquote option).
For more information see here.

Can't find mkdir() function in dirent.h for windows

I am using dirent.h 1.20 (source) for windows in VC2013.
I can't find mkdir() in it.
How am I supposed to use it? Or can I create a directory somehow only using dirent.h?
simplest way that helped without using any other library is.
#if defined _MSC_VER
#include <direct.h>
#elif defined __GNUC__
#include <sys/types.h>
#include <sys/stat.h>
#endif
void createDir(string dir) {
#if defined _MSC_VER
_mkdir(dir.data());
#elif defined __GNUC__
mkdir(dir.data(), 0777);
#endif
}
Update: Since C++17, <filesystem> is the portable way to go. For earlier compilers, check out Boost.Filesystem.
The header you are linking to is effectively turning your (POSIX) dirent.h calls into (native) Windows calls. But dirent.h is about directory entries, i.e. reading directories, not creating ones.
If you want to create a directory (mkdir()), you need either:
A similar wrapping header turning your (POSIX) mkdir() call into the corresponding (native) Windows function calls (and I cannot point out such a header for you), or
use the Windows API directly, which might be pragmatic but would lead to a really ugly mix of POSIX and Windows functions...
// UGLY - these two don't belong in the same source...
#include <dirent.h>
#include <windows.h>
// ...
CreateDirectory( "D:\\TestDir", NULL );
// ...
Another solution would be to take a look at Cygwin, which provides a POSIX environment running on Windows, including Bash shell, GCC compiler toolchain, and a complete collection of POSIX headers like dirent.h, sys/stat.h, sys/types.h etc., allowing you to use the POSIX API consistently in your programming.
Visual Studio includes the <direct.h> header.
This header declares _mkdir and _wmkdir, which can be used to create a directory, and are part of the C libraries included with Visual Studio.
The other "easy" option would be to use Windows API calls as indicated by DevSolar.
You can use sys/types.h header file and use
mkdir(const char*) method to create a directory
Following is the sample code
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
if(!mkdir("C:mydir"))
{
printf("File created\n");
}
else
printf("Error\n");
}
mkdir is deprecated. Give #include <direct.h> as a header file. then write
_mkdir("C:/folder")

Use of #include <iostream.h>

I am working on an older project that still has the deprecated "#include iostream.h" inclusions. I understand that iostream.h is deprecated and should not be used, but some of the systems that this code has to run/compile on are old solaris machines running CC and do not have iostream available. My question is: how can I make my more modern g++ compiler accept the iostream.h inclusions.
EDIT: The compilier cannot find the iostream.h file so I am assuming that none of the .h versions of the library are available to g++.
The easiest solution is probably to create a local header file called iostream.h which just includes <iostream> and imports the namespace std. Then, in order for the compiler to allow #include <iostream.h> you add the local path to your include file search path. For g++, this works:
g++ -I local_folder [other flags] …
Incidentally, your remark about
… the deprecated "#include iostream.h"
isn’t quite correct: this isn’t deprecated because it has never been legal C++.
I'd take a step back and write another intermediate header you use everywhere instead that does something like:
#if defined(sun) || defined(__sun)
# if defined(__SVR4) || defined(__svr4__)
/* Solaris */
#include <iostream>
# else
/* SunOS */
#include "iostream.h"
# endif
#else
/* Sane, modern system */
#include <iostream>
#endif

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.

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