Search code examples
c++cvisual-studioprecompiled-headersunistd.h

Visual Studios, using using a precompiled header in C code


I'm importing a code I wrote on linux to Visual Studio and I'm replacing the unistd.h file with this one I found here on Stack Overflow:

#ifndef _UNISTD_H
#define _UNISTD_H    1

/* This is intended as a drop-in replacement for unistd.h on Windows.
 * Please add functionality as neeeded.
 * https://stackoverflow.com/a/826027/1202830
 */

#include <stdlib.h>
#include <io.h>
#include <getopt.h> /* getopt at: https://gist.github.com/ashelly/7776712 */
#include <process.h> /* for getpid() and the exec..() family */
#include <direct.h> /* for _getcwd() and _chdir() */

#define srandom srand
#define random rand

/* Values for the second argument to access.
   These may be OR'd together.  */
#define R_OK    4       /* Test for read permission.  */
#define W_OK    2       /* Test for write permission.  */
//#define   X_OK    1       /* execute permission - unsupported in windows*/
#define F_OK    0       /* Test for existence.  */

#define access _access
#define dup2 _dup2
#define execve _execve
#define ftruncate _chsize
#define unlink _unlink
#define fileno _fileno
#define getcwd _getcwd
#define chdir _chdir
#define isatty _isatty
#define lseek _lseek
/* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call e.g. closesocket(). */

#ifdef _WIN64
#define ssize_t __int64
#else
#define ssize_t long
#endif

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
/* should be in some equivalent to <sys/types.h> */
typedef __int8            int8_t;
typedef __int16           int16_t; 
typedef __int32           int32_t;
typedef __int64           int64_t;
typedef unsigned __int8   uint8_t;
typedef unsigned __int16  uint16_t;
typedef unsigned __int32  uint32_t;
typedef unsigned __int64  uint64_t;

#endif /* unistd.h  */

This includes the getopt.h and getopt.c files on github. I've included this with my main.cpp in the project. At the top of each code, I've added

#include "pch.h"

However, I get an error when I do this. "Precompiled header file is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C (or vice versa)". This occurs in getopt.c.

Now I've tried changing the settings to "Not using Precompiled Headers", but when I do so I get the error "'int8_t': redefinition; different basic types" in unistd.h line 48. Is there another precompiled header I'm supposed to use for c codes or a way to work around this? Thanks!

EDIT: Also, just to verify, I do have #pragma once at the top of each .h file.


Solution

  • First remove all those typedef definitions (int8_t, int16_t, etc..) from your unistd.h file. Those are already defined by including <stdint.h>, which is available on Windows. That will probably get you fixed after disabling precomp headers.

    As you've probably figured out, you can't mix precompiled headers with C and C++ at the same time. Several possible solutions:

    1. Build your "C" code in a separate static library (lib) with it's own precompiled header (e.g. "pch.h" and "pch.c"). Or just skip the precompiled header stuff altogether for this project. Then your EXE is built with the C++ code and links to your LIB of C code.

    2. Or simply rename your getopt.c to getopt.cpp. It will probably compile just fine with a quick fix up or two. Then bulid all your C++ code with precompiled headers together.