简体   繁体   English

如何在编译时关闭功能?

[英]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: 那是:

  1. Keep the code seperate. 保持代码分开。 Heck, you can even license the "free" code as open-source and still keep your business model. 哎呀,你甚至可以将“免费”代码作为开源许可,并且仍然保留你的商业模式。
  2. Make them phone home or verify some crypt key so that you know said person is licensed. 让他们打电话回家或验证一些密钥,以便您知道该人已获得许可。
  3. Make sure the rest of the code can live with its paid for guts removed. 确保其余的代码可以使用已删除的付费内容。 Comfort if necessary. 必要时舒适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在编译时获得多维 std::vector 的深度? - How can I get the depth of a multidimensional std::vector at compile time? 如何检查在编译时是否调用了模板化方法? - How can I check if a templated method was called at compile-time? 如果在编译时知道边缘条件,我怎么能忽略一个调用呢? - How can I elide a call if an edge condition is known at compile time? 如何确定函数在编译时是否被覆盖? - How can I determine whether a function was overridden at compile time? 如何在字符串中使用编译时常量__LINE__? - How can I use the compile time constant __LINE__ in a string? 如何在编译时使用 std::make_tuple? - How can I use std::make_tuple at compile time? 如何在编译时标记默认参数的所有使用 - How can I flag all uses of a default parameter at compile time 我怎样(在编译时)确定typename是否是函数指针typename? - How can I (at compile time) determine if a typename is a function pointer typename? 如何创建模板具有特定类型的编译时断言? - How can I create a compile time assertion that a template is of specific types? 如何在编译时生成密集的唯一类型ID? - How can I generate dense unique type IDs at compile time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM