如何在Qt Creator中为不同的构建设置定义常量

How to define constants in Qt Creator for different Build Settings?

本文关键字:构建 设置 常量 定义 Qt Creator      更新时间:2023-10-16

我正在开发与服务器连接的Qt应用程序。我有一个生产服务器和一个预生产服务器,所以它将是伟大的,能够改变URL的应用程序使用基于构建。

所以,总结一下,我需要应用程序像这样运行:

调试配置
//somewhere in a .cpp file
QString url = BASE_URL;
// url now is, for example, "preproduction_server.com"

发布配置

//in the same .cpp file
QString url = BASE_URL;
// url now is, for example, "production_server.com"

任何想法?我应该在.pro文件中定义变量并使用它们吗?将它们定义为环境变量?

最后我用QT_NO_DEBUG

这是我的代码现在的样子

#ifdef QT_NO_DEBUG
#define BASE_URL "production_server.com"
#else
#define BASE_URL "preproduction_server.com"
#endif

NDEBUG应该由qmake为发布版本定义,因此您可以使用:

#ifdef NDEBUG
#define BASE_URL "production_server.com"
#else
#define BASE_URL "preproduction_server.com"
#endif