PeakGen
PeakGen

Reputation: 22995

Compiling Only one part of if-else

Please have a look at the following code

#include <iostream>
using namespace std;

    int main (){
        #if true
            int fd = 0;

        #else
            int dd =0;
        #endif
        cout<<fd;
        //cout<<dd;

        system("pause");
        return 0;
    }

In here, I am trying to compile only one block. If 'if' compiled, then 'else' should not, like wise. But this is not working. Please help.

Upvotes: 0

Views: 102

Answers (1)

bikram990
bikram990

Reputation: 1115

For conditional compiling the code must look something like this:

#include <iostream>
using namespace std;

int main (){
    #ifdef Somedef
        int fd = 0;
        cout<<fd;
    #else
        int dd =0;
        cout<<dd;
    #endif
    system("pause");
    return 0;
}

And then while compiling the code give command line argument for the define for eg: gcc -DSomedef test.c -o test to include the code and remove the command line define for removing the code

Note: You have checked "#if true" for conditional compiling that statement will always be true and hence will include that part of the code while compiling.

Upvotes: 3

Related Questions