首页 > 解决方案 > 仅在编译器开始构建时才为真的宏

问题描述

是否可以true在构建时设置宏,否则设置为false?我意识到有宏可以检测构建配置(例如调试/发布),但我正在寻找一个在编译器开始构建时设置的宏。如果宏非常特定于特定编译器,那也没关系。

用例是通过简化一些复杂的宏来帮助智能感知。自动完成功能不适用于复杂的宏。然而,简化的宏在运行时非常慢。

标签: c++macrosintellisense

解决方案


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.


推荐阅读