answersLogoWhite

0


Best Answer

Conditional compilation is used to exclude code segments from specific builds. For instance, in debug mode you might include additional code such as an assertion macro which you wouldn't really want in the release code.

The following example declares and defines an ASSERT() macro when the program is compiled in debug mode (when DEBUG is defined). In non-debug mode (when DEBUG is not defined), all calls to ASSERT() will be ignored during compilation because there is no implementation provided.

#ifdef DEBUG

#define ASSERT(x) \

if(!(x)) \

{ \

std::cout << "ERROR!! Assert " << #x << " failed\n"; \

std::cout << "on line " << __LINE__ << std::endl; \

std::cout << "in file " << __FILE__ << std::endl; \

}

#else

#define ASSERT( x )

#endif _DEBUG

Within your code you can call ASSERT() to ensure your invariants are true whilst in debug mode:

int x=1, y=1;

ASSERT( x == y );

// .. remainder of code...

In debug mode, the compiler expands the code for you, just as if you'd written the following:

int x=1, y=1;

if(!( x==y ))

{

std::cout << "ERROR!! Assert " << #x << " failed\n";

std::cout << "on line " << __LINE__ << std::endl;

std::cout << "in file " << __FILE__ << std::endl;

}

// .. remainder of code...

But in release mode there is no macro to expand, so the assert is effectively ignored:

int x=1; y=1

// .. remainder of code...

Other uses of conditional compilation include catering for UNICODE or MCBS encoding of strings. If your program uses one or the other, you can use macros to ensure the appropriate string handling calls and declarations are made.

They are also used to "guard" include files to ensure an included file is never included more than once during compilation. This is important when several files include the same file for the purpose of syntax checking. During compilation, however, only one copy of the file needs to be physically included. If multiple inclusions were not guarded against, the compiler would try to include several declarations of the same functions and, even though they all come from the exact same file, the compiler treats them as separate entities and immediately stops the compilation. The same thing occurs when a function is declared twice in two separate files.

// myclass.h

#ifndef _myclass_h_ // First time around, this will not be defined.

#define _myclass_h_ // Now it is defined.

// Declarations (with or without implementations) go here.

#endif _myclass_h_ // Marks the end of the inclusion.

When the compiler "sees" this file for the first time, it will be included because _myclass_h_ would be undefined first time around. But all subsequent inclusions will be ignored because _myclass_h_ was defined during the first inclusion.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is Conditional Compilation in C plus plus Any simple example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is platform dependency of c plus plus?

C++ has no platform dependency. If a compiler exists for a platform (and few don't) code can be written for that platform. Where platforms have different methods to do the same thing, conditional compilation can be used to cater for those differences, thus the same source code can be compiled on any platform simply by changing the definitions used by the conditional compilation directives. For instance, a program that caters for Unix and Windows platforms might contain the following conditional compilation: #ifdef __unix__ #include &lt;unistd.h&gt; #elif defined _WIN32 #include &lt;windows.h&gt; #endif The definition of __unix__ and _WIN32 must be mutually exclusive.


What is compilation in c plus plus?

Compilation is the process of translating source files into object files.


When do preprocessor directives execute in c plus plus?

Preprocessing is the first stage of compilation, where macros are expanded, conditional compilation established and code replaced according to the specified directives. The resulting code produces intermediate source files which are then compiled by the main compilation process. Your IDE may include options to retain these intermediate files so you may examine them.


What is go-to unconditional in c plus plus?

An unconditional goto is a goto that has no associated conditional expression. The following example demonstrates conditional and unconditional goto statements. int x=rand(); if (x) goto label_1; // conditional goto (when x is non-zero) else goto label_2; // conditional goto (when x is zero) label_1: // ... goto label_3; // unconditional goto (jump past label_2) label_2: // ... label_3: // ...


What are unconditional statements in c plus plus?

Unconditional statements are statements that are invoked unconditionally. Conditional statements have a controlling expression, while unconditional statements do not. For example: void f (bool b) { if (b==true) do_something(); // conditional statement (controlled by the expression b==true) do_something_else(); // unconditional (executes regardless of b's value) }

Related questions

Why is c plus plus language more portable than c language?

They are equally portable. Conditional compilation is supported by both languages.


What is platform dependency of c plus plus?

C++ has no platform dependency. If a compiler exists for a platform (and few don't) code can be written for that platform. Where platforms have different methods to do the same thing, conditional compilation can be used to cater for those differences, thus the same source code can be compiled on any platform simply by changing the definitions used by the conditional compilation directives. For instance, a program that caters for Unix and Windows platforms might contain the following conditional compilation: #ifdef __unix__ #include &lt;unistd.h&gt; #elif defined _WIN32 #include &lt;windows.h&gt; #endif The definition of __unix__ and _WIN32 must be mutually exclusive.


What is compilation in c plus plus?

Compilation is the process of translating source files into object files.


When do preprocessor directives execute in c plus plus?

Preprocessing is the first stage of compilation, where macros are expanded, conditional compilation established and code replaced according to the specified directives. The resulting code produces intermediate source files which are then compiled by the main compilation process. Your IDE may include options to retain these intermediate files so you may examine them.


What is go-to unconditional in c plus plus?

An unconditional goto is a goto that has no associated conditional expression. The following example demonstrates conditional and unconditional goto statements. int x=rand(); if (x) goto label_1; // conditional goto (when x is non-zero) else goto label_2; // conditional goto (when x is zero) label_1: // ... goto label_3; // unconditional goto (jump past label_2) label_2: // ... label_3: // ...


What is 400x14 Plus 500x21?

Simple 5,600 Plus 1,100 Example:5,600+1,100=6,700


Conditional if 2x plus 3 equals 293 then x equals 145 converse if x equals 145 then 2x plus 3 equals 293?

the converse of this conditional is true


What is the conditional formation constant for the reaction between EDTA and Ca2 plus at pH equals 10?

The conditional constant= 1.8*1010


What are unconditional statements in c plus plus?

Unconditional statements are statements that are invoked unconditionally. Conditional statements have a controlling expression, while unconditional statements do not. For example: void f (bool b) { if (b==true) do_something(); // conditional statement (controlled by the expression b==true) do_something_else(); // unconditional (executes regardless of b's value) }


What is 602 plus 2?

It's an example of a simple problem in addition. The solution is: 604


What is 360 plus 360?

It's a simple example of an addition problem. The solution is: 720


Types of compilation in c plus plus?

There is only one type: the one that creates an object module from a source file.