[英]How can I gate features at compile time?
I have a C++ application I'm developing. 我正在开发一个C ++应用程序。 I want to release a free version and a paid version, with the production version adding features on top of the existing free version. 我想发布免费版本和付费版本,生产版本在现有免费版本之上添加功能。
What's the best way to compile features depending on the release type? 根据发布类型编译功能的最佳方法是什么? Ideally I would pass a flag to CMake so while building the binary it would also include any code I need in the production version. 理想情况下,我会将标志传递给CMake,因此在构建二进制文件时,它还将包含我在生产版本中需要的任何代码。
You answered your question yourself :) Pass a flag to your compiler and in your code gate the features with: 你自己回答了你的问题:)将一个标志传递给你的编译器,并在代码中将这些功能传递给:
#ifdef YOUR_FLAG
//your extra logic
#elif
//in case YOUR_FLAG is not defined
#endif
I used exactly the same approach in a game I developed - in free version ads are enabled and in paid version that code gets excluded by preprocessor. 我在我开发的游戏中使用了完全相同的方法 - 在免费版本广告中启用,在付费版本中,代码被预处理器排除。
Also you can use run-time checks in case it's more appropriate for some specific task, but as it's mentioned in comments to the question - code is there and tampering with memory (flipping your flag) will possible allow to gain access to restricted features. 此外,您可以使用运行时检查,以防它更适合某些特定任务,但正如在问题的评论中提到的那样 - 代码存在并且篡改内存(翻转您的标志)将允许访问受限制的功能。
You add them as licensed plugins. 您将它们添加为许可插件。
That is: 那是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.