Related questions
How can I detect the operating system in C/C++?
How can I find out what operating system I am runni
It is possible to solve your problem using autoconf tool to discover the presence of unistd.h
and then would add a #define HAVE_UNISTD_H 1
line in a generated config.h
if unistd.h
was found, but I find that autoconf is a little hard to use, and is very out-dated.
If by any chance you are using cmake, you can solve it the same way. You could create a config.h.in
containing something like this:
#ifndef CONFIG_H
#define CONFIG_H
#cmakedefine HAVE_UNISTD_H 1
#endif
And your project's CMakeLists.txt
would look like this:
project(MyApp)
include(CheckIncludeFiles)
check_include_file("unistd.h" HAVE_UNISTD_H)
configure_file(config.h.in config.h @ONLY)
add_executable(${PROJECT_NAME} main.c)
and then to generate from the command line:
cmake . -G"Unix Makefiles"
or generate Xcode project (OSX only):
cmake . -G"Xcode"
or generate a visual studio 2013 solution project (Windows only):
cmake . -G"Visual Studio 12 2013 Win64"
cmake generators = epic win
If your operating system is POSIX, then your generated config.h
should look like this:
#ifndef CONFIG_H
#define CONFIG_H
#define HAVE_UNISTD_H 1
#endif
Otherwise, it will look like that:
#ifndef CONFIG_H
#define CONFIG_H
/* #undef HAVE_UNISTD_H */
#endif
And then you are free to your your trusted generated config.h
:
#include "config.h"
#if HAVE_UNISTD_H
# include
#endif
int main (int argc, const char * argv[])
{
#if defined(_POSIX_VERSION)
/* POSIX code here */
#else
/* non-POSIX code here */
#endif
return 0;
}