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

  • SEARCH
  • Home
  • 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 8952009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:46:35+00:00 2026-06-15T13:46:35+00:00

I am creating a library (.lib) in c++ with Visual Studio 2008. I would

  • 0

I am creating a library (.lib) in c++ with Visual Studio 2008. I would like to set a variable to change the behaviour of the library depending on the variable. Simplifying a lot, something like this:

#ifdef OPTION1
i = 1;
#else
i = 0;
#endif

But the variable (in this case OPTION1) should not be defined in the library itself, but in the code that links to the library, so that just changing the definition of the variable I could obtain different behaviours from the program, but always linking to the same library.

Is this possible, and how? Or is there a more elegant way to achieve what I want?

  • 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-06-15T13:46:36+00:00Added an answer on June 15, 2026 at 1:46 pm

    To pull this off, the code which depends on the macro must be compiled as part of the code which links to the library, not as part of the library itself. The best you could do is something like this:

    In your public .h file:

    namespace LibraryPrivate {
    
    void functionForOptionSet();
    void functionForOptionUnset();
    
    }
    
    #ifdef OPTION1
    inline void dependentBehaviour() {
      LibraryPrivate::functionForOptionSet();
    }
    #else
    inline void dependentBehaviour() {
      LibraryPrivate::functionForOptionUnset();
    }
    #endif
    

    In you library’s .cpp file:

    namespace LibraryPrivate {
    
    void functionForOptionSet()
    { i = 1; }
    
    void functionForOptionUnset()
    { i = 0; }
    
    }
    

    That is, you have to implement both options in the library, but you can (partially) limit the interface based on the macro. Kind of something like what WinAPI does with char vs. wchar_t functions: if provides both SomeFunctionA(char*) and SomeFunctionW(wchar_t*) and then a macro SomeFunction which expands to one of those.

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

Sidebar

Related Questions

Visual Studio C++ 2005 Windows XP I am creating this DLL library. The DLL
I am using visual studio 2008 SP1. And I am creating a desktop application
Hopefully someone can help me out here. I'm using Visual Studio 2005 and creating
I'm trying to build a static library (lib.a file) for use in creating an
When creating a DLL with Visual C++ 2008 I have a couple of choices.
AFTER EDIT I get the this error 1> Creating library D:\Jni tester\Debug\Jni tester.lib and
Whenever I create a new project in Visual Studio 2010 and don't set the
I have installed Python 3k(C:\Python30) and Visual Studio Professional Edition 2008. I'm studying this
I'm creating a library of display objects for our application. They render the html
I am creating a library and even though it is working perfectly fine by

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.