c++ - 仅在编译器开始构建时才为真的宏
问题描述
是否可以true
在构建时设置宏,否则设置为false
?我意识到有宏可以检测构建配置(例如调试/发布),但我正在寻找一个在编译器开始构建时设置的宏。如果宏非常特定于特定编译器,那也没关系。
用例是通过简化一些复杂的宏来帮助智能感知。自动完成功能不适用于复杂的宏。然而,简化的宏在运行时非常慢。
解决方案
You can set a macro in your IDE and don't use it in the build process
#ifdef IDE_MACRO
#define SIMPLE_MACROS
#else
#define COMPLEX_MACROS
#endif
In Visual Studio Code you can set
"configurations": [
{
...
"defines":[
"IDE_MACRO"
]
}
]
in
.vscode/c_cpp_properties.json
defines A list of preprocessor definitions for the IntelliSense engine to use while parsing files. Optionally, use = to set a value, for example VERSION=1.
This only works if compileCommands is not set
compileCommands (optional) The full path to the compile_commands.json file for the workspace. The include paths and defines discovered in this file will be used instead of the values set for includePath and defines settings. If the compile commands database does not contain an entry for the translation unit that corresponds to the file you opened in the editor, then a warning message will appear and the extension will use the includePath and defines settings instead.
As you can read in the quote you can also use compile_commands.json
to set macros and IntelliSense will consider them.
In Eclipse CDT it's in C/C++ Build -> Build Variables. There you can set a Variable IDE_MACRO
. Now Eclipse CDT uses SIMPLE_MACROS
instead of COMPLEX_MACROS
. In the build process the macro IDE_MACRO
is not defined and the compiler uses COMPLEX_MACROS
.
推荐阅读
- javascript - Javascript mm:ss 格式的倒数计时器,文本输入受限
- javascript - 使用 $route.path 加载背景图像在页面加载时间方面的表现如何
- android - 以编程方式在 android studio 中生成 JAVA/KOTLIN 类
- typescript - 打字稿功能扩展被破坏
- python - 没有循环或列表理解的元组中的 Numpy 访问元素
- r - 如何从 lme4 中提取公式的随机效应部分
- python - 如何将 Python 代码编译成共享库以在 C# 应用程序(如 Unity)中使用?
- .net - Travis CI:面向 .NET Core、.NET Framework 和 .NET Standard
- javascript - 如何设置等于窗口 URL 的变量?
- c - 我制作了两个代码,但我无法解释为什么这两个代码不同并产生不同的结果