15

I am including a third party header and source file into my project.

At the top of the header there is this:

#if defined(WIN32) || defined(WIN16)
#ifndef MSDOS
#define MSDOS
#endif
#endif

#include <stdio.h>
#include <stdlib.h>
#ifndef MSDOS
#include <unistd.h>
#endif
#include "des.h"

The problem is that #if defined(WIN32) fails and the compilation fails when trying to #include unistd.h which I don't want to do.

I have third party project that works with this header file i.e. WIN32 is defined and it doesn't try to include In Visual Studio I did "Go To Definition" on "WIN32" and was taken to the following definition in WinDefs.h.

#define WIN32

I'm not sure this is where its getting WIN32 definition from, as the third party project does not appear to include "WinDefs.h".

So my problem is, how can I get WIN32 to be defined in my current new project?

BeeBand
  • 10,953
  • 19
  • 61
  • 83

6 Answers6

10

Depends on your project setup. WIN32 is defined inside the windows header files, but you can pass it to the compiler as well ("-DWIN32" for gcc for example). Try it and see whether it compiles.

Axel
  • 13,939
  • 5
  • 50
  • 79
  • 3
    In visual studio, you can enter this via the project properties in the "extra preprocessor directives" or something. – André Caron Feb 22 '11 at 15:51
  • This helped me! Thanks! The property is called "Preprocessor Definitions" and WIN32 must be in there (for WIN32 projects). – frogpelt Feb 06 '15 at 15:03
7

Visual Studio has the built-in define _WIN32. mingw-gcc has WIN32 and _WIN32 built-in so the project was likely tested using gcc. You might add


#if defined(_WIN32) && !defined(WIN32)
#define WIN32
#endif

or just add a -DWIN32 to the CFLAGS.

patthoyts
  • 32,320
  • 3
  • 62
  • 93
7

Check your includes. I am guessing that the third party header is included prior to the windows.h. So, in your main.cpp or equal it should be

#include <windows.h> // this will also include windefs.h
#include <thirdParty.h>

and not the other way around.

Hope that helps.

default
  • 11,485
  • 9
  • 66
  • 102
0

For those seeking answers to the

where is WIN32 defined

part of the questions, I've found it defined in:

minwindef.h

ole2.h

Note, I have no confidence that these are the only places it's defined. I expect there are probably other files where it's defined. Nevertheless, I thought this might help some people.

Community
  • 1
  • 1
user3731622
  • 4,844
  • 8
  • 45
  • 84
0

Some WIN32 defined in the compiler . Just like this,If you use the gcc for windows , WIN32 is defined . If you use the gcc for linux , WIN32 is not defined :)
So , the macros is a switch. You can define it to use somethine , and not define it to unuse something.

15 小
  • 1
  • 1
0

You can simply include the windows header files (windows.h) before including the third party header - as you already found out WIN32 is defined there but technicaly it could be defined anywhere (so if the third party project is not including the windows headers check if it's being defined in the compiler project settins directly).

BTW there is also a _WIN32 define that is set by the compiler, it's possibly a better idea to look for this define if checking if the code is being compiled under windows;

floyd73
  • 1,240
  • 9
  • 12