Matt Guerrette
Matt Guerrette

Reputation: 24

using #ifdef block inside exported C++ function in DLL

I'm trying to use an #ifdef switch inside a function that I export to a DLL

The issue im running into is that my .EXE project can't change the ifdef defines. Its like they are pre compiled into the DLL and can't be changed. Is this correct? Can I not use #ifdef inside a C++ function that is exported to a DLL.

example:

void API func()
{
   #ifdef I_WANT_THIS
      cout << "I want this" << endl;      
   #else
      cout << "I dont want this" << endl;
   #endif
}

If I defined nothing when building the DLL and then in the .EXE project I try and define, I_WANT_THIS it doesn't actually apply to the cpp object file linked in the library. I assume this is how it should be, but I feel like I should be able to do this...

Upvotes: 1

Views: 792

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597215

Yes, they are pre-compiled into the DLL's code. The EXE cannot change them. To do what you are attempting, you will have to either:

  1. pass an extra parameter into the function:

    void API func(bool param)
    {
        if (param)
            cout << "I want this" << endl;      
        else
            cout << "I dont want this" << endl;
    }
    
  2. export two separate functions:

    void API doThis()
    {
       cout << "I want this" << endl;      
    }
    
    void API doThat()
    { 
        cout << "I want that" << endl;      
    } 
    

Upvotes: 0

mukunda
mukunda

Reputation: 2995

You need to make that kind of functionality switchable during runtime. DLLs are compiled code and any preprocessing like that is already performed when they are built.

Upvotes: 1

Related Questions