annena
annena

Reputation: 941

How to make Visual Studio ignore some lines of code when compiling for one config but does not ignore when in other configs

I have a Visual Studio project in C++ where I need to build for 2 different configs/platforms. Each build config (let's say A and B) is using a different library. There is one cpp file in my project that uses one particular function (1 line of code) that is only available in config A and not config B. This causes compilation error when compiling config B.

I have checked out the use of #ifdef but that would need some edits whenever build config is switched.

Can anyone advise an elegant way to enable config B to ignore only this line while config A compiles this as usual? Thanks!

Upvotes: 0

Views: 370

Answers (1)

Seva Alekseyev
Seva Alekseyev

Reputation: 61398

In project properties, under C++/Preprocessor, introduce some config specific #defines - say, AY and BEE. Make sure you add them for both Debug and Release flavors.

Then use #ifdef...#endif in the source for config specific lines.

Example:

#ifdef BEE
    int a = 0;
#else
    int a = 1;
#endif

An alternative approach involves introducing multiple, configuration specific source files, and excluding some of them from build in one configuration, but not in the other.

In other environments, the same can be achieved by providing extra #defines via the compiler command line - -D MYSYMBOL for GCC. MSVC internally supports that, too.

Upvotes: 2

Related Questions