Pregunta

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.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top