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 3983962
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:42:44+00:00 2026-05-20T05:42:44+00:00

In an MSVC C++ program I have a part of code which I want

  • 0

In an MSVC C++ program I have a part of code which I want to enable or disable depending on a preprocessor definition

// 1.h
#ifdef MYOPTION
//...
#endif

But I find that it is quite dangerous when it is used in a .h file included in more than one compilation unit, as I can easily get inconsistent headers (I don’t want to define MYOPTION globally as it would require a complete recompilation each time I change it):

// 1.cpp
#define MYOPTION
#include "1.h"

// 2.cpp
#include "1.h"

Of course, it is much more complicated than this simplified example due to the chained header inclusion.

Is there a way to avoid such inconsistency, e.g. have a compile-time error without too much effort?

I thought of doing #define MYOPTION 0 or 1, but then I would have to write something like

#if MYOPTION == 1
//...
#elif !defined(MYOPTION)
#error ...
#endif

which looks too complicated… Maybe there is a better option?

  • 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-20T05:42:44+00:00Added an answer on May 20, 2026 at 5:42 am

    How about something like this: have 1.h define a dummy section in the obj with different options. This way, if MYOPTION is ever used inconsistently, the linker will issue a warning.

    1.h:

    #ifdef MYOPTION
    #pragma section("MYOPTION_GUARD",write)
    #else
    #pragma section("MYOPTION_GUARD",read)
    #endif
    
    namespace { __declspec(allocate("MYOPTION_GUARD")) int MYOPTION_guard; }
    

    Compiling with MYOPTION defined in a.cpp but not in b.cpp yields this linker warning (using VC 2008):

    b.obj : warning LNK4078: multiple 'MYOPTION_GUARD' sections found with different attributes (40300040)
    

    A consistent definition yields no linker warnings at all.

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

Sidebar

Related Questions

I have a static library *.lib created using MSVC on windows. The size of
I am reviewing the flags we have for our MSVC projects, and I can't
We have some nightly build machines that have the cuda libraries installed, but which
I am trying to build a program using the command scons compiler = msvc.
In MSVC, DebugBreak() or __debugbreak cause a debugger to break. On x86 it is
i did this in msvc 2005. typedef void (*cleanup_t)(); void func(cleanup_t clean) { cleanup_t();
I know MSVC can do this via a pragma message -> http://support.microsoft.com/kb/155196 Does gcc
I'm tyring to convert a MSVC project from VS 2005 to VS 2008. It
How do you get Pro*c to work within MSVC 6? In otherwords compile a
I'm trying to create a c++ library for use on windows/MSVC. My problem is

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.