Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7662045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:42:06+00:00 2026-05-31T13:42:06+00:00

I would like to ask if it is possible to use the entities defined

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:42:07+00:00Added an answer on May 31, 2026 at 1:42 pm

    #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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask if it's possible to use PHP in removing a
I would like to ask you if is possible to add a shadow to
I would like to ask if is it possible to extract the exact variable
I would like to ask if it is possible to copy/move all the objects
i would like to ask if it is possible to forbid pull-out of the
I'm a newbie in WPF and I would like to ask if it's possible
Possible Duplicate: JavaScript: Why the anonymous function wrapper? I would like to ask you
We're moving to Azure, I would like to ask - is it possible to
I would like to ask if there is a possible way to select all
I have a yes or no question & answer. I would like to ask

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.