Qt中实现条件编译

//test.pro
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.c

DEFINES += **Home**
#DEFINES += Ultimate 

include(deployment.pri)
qtcAddDeployment()
#include 

#ifdef Home
    #define LOG(s) printf("[%s: %d] %s",__FILE__,__LINE__,s)
#else
    #define LOG(s) NULL
#endif

#ifdef Ultimate
    void f()
    {
        printf("This is Ultimate version\n");
    }
#else
    void f()
    {

    }
#endif

int main(void)
{
    LOG("Enter main...\n");

    f();
    printf("1.Hi.\n");
    printf("2.Hello.\n");

#ifdef Ultimate
    printf("3.Bye.\n");
    printf("4.See U.\n");
#else
    printf("3.GoodBye.\n");
#endif

    LOG("Exit main...\n");

    return 0;
}

输出:
[..\test\main.c: 23] Enter main…
1.Hi.
2.Hello.
3.GoodBye.
[..\test\main.c: 36] Exit main…

若修改test.pro中的

//test.pro
...
#DEFINES += Home
DEFINES += Ultimate 
...

则输出为:
This is Ultimate version
1.Hi.
2.Hello.
3.Bye.
4.See U.

你可能感兴趣的:(QT)