[英]VSCode: Set C/C++ preprocessor macro for static analysis
I am developing a library which lets user set a crucial type alias, or do it through preprocessor directives.我正在开发一个库,它允许用户设置一个关键的类型别名,或者通过预处理器指令来完成。 This type alias (or the directive) is undeclared in the library, by design.
这种类型的别名(或指令)在库中未按设计声明。 Thus, when developing my code I get annoying error messages and squiggles for this undeclared type.
因此,在开发我的代码时,我会收到烦人的错误消息和这种未声明类型的曲线。 This could be avoided if I declare a temporary type for it somewhere.
如果我在某处为它声明一个临时类型,则可以避免这种情况。 However, I do not like the idea of declaring it when I work with the code and then remove it when I publish it.
但是,我不喜欢在使用代码时声明它,然后在发布时将其删除。 It is also bug prone, since I could easily forget to remove it.
它也容易出错,因为我很容易忘记删除它。
My question is: Can I define preprocessor directives for VS Code's static analysis (IntelliSense? C/C++ Extension)?我的问题是:我可以为 VS Code 的 static 分析(IntelliSense?C/C++ 扩展)定义预处理器指令吗?
That would let me consider the analysis like what a well defined type alias would produce.这将让我将分析视为定义明确的类型别名会产生什么。 And avoid annoying error messages/squiggles.
并避免烦人的错误消息/曲线。
Minimal reproducable example:最小的可重现示例:
Online compiler example在线编译器示例
tCore.hpp tCore.hpp
#pragma once
#include <string>
// User is responsible of declaring the tCore type
// tCore interface methods
template<typename TCore>
std::string printImpl();
tPrint.hpp tPrint.hpp
#pragma once
#include <iostream>
class tPrint {
public:
tPrint() = default;
void print() const {
std::cout << printImpl<tCore>() << std::endl; // <-- Static analysis error here!
}
};
tFoo.hpp - tCore candidate tFoo.hpp - tCore 候选
#pragma once
#include <string>
#include "tCore.hpp"
struct tFoo {};
template<>
std::string printImpl<tFoo>() {
return "tFoo";
}
main.cpp主文件
#include <tFoo.hpp>
using tCore = tFoo;
int main() {
tPrint p{};
p.print(); // -> "tFoo"
return 0;
}
I found out it was IntelliSense causing the error through the C/C++ Extension .我发现是IntelliSense通过C/C++ Extension导致了错误。
I also found an option of adding compiler arguments to IntelliSense , which is exactly what I was looking for.我还找到了将编译器 arguments 添加到IntelliSense的选项,这正是我想要的。
Press F1 -> > C/C++: Edit Configurations (UI)
-> Scroll down to Defines
按 F1 ->
> C/C++: Edit Configurations (UI)
-> 向下滚动到Defines
c_cpp_properties.json
configurations has a field defines
which holds any compiler arguments. c_cpp_properties.json
配置有一个字段defines
,其中包含任何编译器 arguments。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.