lundi 18 janvier 2021

Library and project including same header with different implementations

Suppose we have some static library Library that has some function void x() { foo(); } (let's say, located in library.h)

The definition void foo(); is in some header file functionality.h.

Furthermore, when we're building Library.lib, we define _BUILDING_LIBRARY (with -DBUILDING_LIBRARY or by whatever means).

Then suppose functionality.cpp looks like this:

#ifdef _BUILDING_LIBRARY
void foo() { do_x(); }
#else
void foo() { do_y(); }
#endif

The question is as follows:

How do I get a project that includes Library and calls x(), but also builds the file functionality.cpp (without _BUILDING_LIBRARY) to pick the desired implementation of foo()?

#include <library.h>
#include <functionality.h>

int main()
{
    x();
    foo();
}

Should execute

do_x();
do_y();

Aucun commentaire:

Enregistrer un commentaire