Crazyjavahacking
Crazyjavahacking

Reputation: 9677

How to use #ifdef entities as part of functions in header files

I would like to ask if it is possible to use the entities defined in #ifdef block in header files. To be clear, I have following code:

#ifdef WIN32
    #include <winsock2.h>

    #define SOCKET_HANDLE     SOCKET
    #define CONNECTION_HANDLE SOCKET
#endif


SOCKET_HANDLE createServerSocket(const char* hostAddress, short port);

I am Java developer and this seems completely fine for me. However compiler has a problem with this.

Can you explain why is that code a problem? Also how can I force to compile it. (The idea is to have generic interface and conditional compilation to determine real types according to running platform at compile time.)

Thanks

Upvotes: 0

Views: 3374

Answers (3)

freespace
freespace

Reputation: 16701

#ifdef is an conditional preprocessor statement. Much like if statements they need something to evaluate so one branch or another can be taken.

In the case of #ifdef, it is evaluating whether or not the given macro is defined. e.g.

#ifdef BLAH
/* true branch */
#else
/* false branch */
#endif

Checks to see if the BLAH macro is defined. Based on the result the code in either the true or false branch is included and compiled, the others discarded.

In your particular case, the usage of #ifdef is quite clearly to ensure the headers and socket types appropriate to operating system compiling the code is used (ignoring for the moment cross compiling).

For the code in your question, you want to check for the macro _WIN32 and/or _WIN64. These are implicitly defined by most compilers targeting the Windows operating system. Note that in VC++ _WIN32 is defined for 32bit and 64bit Windows, while _WIN64 is only defined for 64bit Windows. See this discussion for more details.

Edit

Having noticed your changes to the question and the exact error message you posted in a comment, the problem is trifold:

  1. WIN32 is the wrong macro. It should be _WIN32 or _WIN64. See above.
  2. Because of 1, the #defines and #include is never seen by the compiler, and thus SOCKET_HANDLE and CONNECTION_HANDLE are both undefined.
  3. Because of 2, when the compiler sees SOCKET_HANDLE createServerSocket(const char* hostAddress, short port); it doesn't recognise SOCKET_HANDLE and exits with an error

The solution is to fix the macro you are checking, changing it from WIN32 to _WIN32 or _WIN64. Further you should provide an #else branch in the event operating system detection completely fails, e.g. assume you are compiling on $mostlikelyos and #define and #include appropriately.

Upvotes: 1

Pavan Manjunath
Pavan Manjunath

Reputation: 28525

#ifdef SOMETHING

SOMETHING is missing in your code

Read about pre-processing directives here

http://msdn.microsoft.com/en-us/library/3sxhs2ty(v=vs.80).aspx

EDIT : After your edit to the question, I tried this out in Dev Cpp. It works fine as WIN32 is defined for me. Even _WIN32 is defined in my IDE. Both work well

Upvotes: 1

hmjd
hmjd

Reputation: 121961

There is missing token after the #ifdef. In this case it looks like you want to build for Windows:

#ifdef WIN32

You may need an #else branch when compiling on other platforms. For example:

#ifdef WIN32
    #include <winsock2.h>

    #define SOCKET_HANDLE     SOCKET
    #define CONNECTION_HANDLE SOCKET
#else
    #define SOCKET_HANDLE     int
    #define CONNECTION_HANDLE int
#endif

Upvotes: 3

Related Questions