• 0

ifdef WIN32


Question

I'm writing some code that needs to compile on both Windows and Unix. I created an empty project in Visual C++ 2008 and added this code :

#ifdef WIN32
#include <winsock.h>
#else
#include <unistd.h>
#endif

This doesn't compile as WIN32 isn't defined and VS is trying to include unistd.h.

I went to project properties -> C++ -> Preprocessor and added to "Preprocessor Definitions" : "/DWIN32", which I guessed is suppose to define WIN32 globally across the project? But this does not work.

Of course I can hard-code #define WIN32 1 but that defeats the whole purpose of it, I want this to be at the project level not in the code so that it compiles without modification in Linux.

How can I change the project so that WIN32 is defined? Thanks.

Link to comment
https://www.neowin.net/forum/topic/776618-ifdef-win32/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Next time you should create an empty Win32 project instead of an empty project, and you'd get all the defines automatically.

But to answer your question, /D is a command line option for the compiler. If you're going to use that, you would have to add it to the advanced command line as /D "WIN32"

If you add it under preprocessor definitions you don't put in the command line option, you just put in the things you want defined separated by a semicolon like this: WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)

Link to comment
https://www.neowin.net/forum/topic/776618-ifdef-win32/#findComment-591040674
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.