标签: preprocessor-directive

VSCode:设置 C/C++ 预处理器宏进行静态分析

我正在开发一个库,它允许用户设置关键类型别名,或者通过预处理器指令来完成。根据设计,此类型别名(或指令)在库中未声明。因此,在开发代码时,我会收到这种未声明类型的烦人的错误消息和波形曲线。如果我在某处为其声明一个临时类型,则可以避免这种情况。但是,我不喜欢在使用代码时声明它,然后在发布代码时将其删除的想法。它也容易出现错误,因为我很容易忘记删除它。

我的问题是: 我可以为 VS Code 的静态分析(IntelliSense?C/C++ 扩展)定义预处理器指令吗?

这会让我考虑像定义良好的类型别名会产生什么一样的分析。并避免烦人的错误消息/曲线。


最小可重现示例:

在线编译示例

tCore.hpp

#pragma once

#include <string>

// User is responsible of declaring the tCore type

// tCore interface methods 
template<typename TCore>
std::string printImpl();
Run Code Online (Sandbox Code Playgroud)

tPrint.hpp

#pragma once

#include <iostream>

class tPrint {
  public:
    tPrint() = default;
      
    void print() const {
        std::cout << printImpl<tCore>() << std::endl; // <-- Static analysis error here!
    }
};
Run Code Online (Sandbox Code Playgroud)

tFoo.hpp - tCore 候选者

#pragma once

#include <string>
#include "tCore.hpp"

struct tFoo {};

template<>
std::string printImpl<tFoo>() {
    return "tFoo"; …
Run Code Online (Sandbox Code Playgroud)

c++ c-preprocessor preprocessor-directive visual-studio-code

3
推荐指数
1
解决办法
5255
查看次数

将 C printf() 调用包装在预处理器指令中

我正在尝试将 C 代码从一个平台移植到另一个平台并删除依赖项。

有一个名为的调试函数dbg_out,它打印出类似printf().

原型是void dbg_out(int dbgMask, const char *pFormat, ...);

这是一个调用示例:dbg_out((5, "SerialDriver : (%04x-%08lx) \n", id, sn));

我想将此函数映射到正常printf()调用,但遇到一些问题。

到目前为止我已经尝试过了

void dbg_out (int dbgMask, const char *pFormat, ...)
__attribute__((format(printf, 2, 3)))
;    
Run Code Online (Sandbox Code Playgroud)

我需要忽略第一个参数 dbgMask 并只考虑其他参数。

我更喜欢使用预处理器来定义包装器。我希望得到一些帮助。

c printf wrapper c-preprocessor preprocessor-directive

3
推荐指数
1
解决办法
250
查看次数

C++中的#ifdef 1是什么?

在C++中,我知道程序员#ifdef 0用来阻止代码运行,但在同一个项目中,我看到了很多#ifdef 1.这是否意味着代码总是运行?不幸的是代码没有编译所以我不能只运行和测试!

c++ macros conditional-compilation c-preprocessor preprocessor-directive

2
推荐指数
1
解决办法
5998
查看次数

可以禁用/排除预处理程序指令#include吗?

例如:如果我有两个.h文件

process1.h和process2.h

它们包含两个具有不同输出变量的函数.

process1.h:

function(int var)
 {
 return 2*var;
}
Run Code Online (Sandbox Code Playgroud)

process2.h:

function(int var)
 {
 return 10*var;
}
Run Code Online (Sandbox Code Playgroud)

可以在main.c中完成:

int main()
{
int a = 2;
#include "process1.h"
printf("%d",function(a));    //output is 4

EXCLUDE #INCLUDE "process1.h" ?????    <----can this be done in any way??
#include "process2.h"
printf("%d",function(a));    //output is 20

}
Run Code Online (Sandbox Code Playgroud)

c c-preprocessor preprocessor-directive

2
推荐指数
1
解决办法
1112
查看次数

#pragma warning - 特定于文件?

简单的问题,但我似乎无法在任何参考资料中找到答案.

如果我有#pragma warning()指令,编译器是否只为当前文件定义?或者它是通过我的项目中的其他文件中的#includes传播的?

这可能是一个更广泛的问题,不一定是#pragma特有的,但我特别担心的情况.

c c++ pragma c-preprocessor preprocessor-directive

2
推荐指数
1
解决办法
325
查看次数

C++ #if #elif #endif似乎不起作用

有人可以告诉我我做错了什么吗?

#include <iostream>
using namespace std;

int main() {

#define myvar B

#if myvar == A
        cout << "A" << endl;
#elif myvar == B
        cout << "B" << endl;
#else
        cout << "Neither" << endl;
#endif
}
Run Code Online (Sandbox Code Playgroud)

输出是A,但显然我期待B

c++ if-statement c-preprocessor preprocessor-directive

2
推荐指数
1
解决办法
3344
查看次数

#if预处理程序指令并不总是崩溃

假设我定义了两个#define预处理器指令:

#define TEST
#define TESTOFF
Run Code Online (Sandbox Code Playgroud)

现在我的代码在TEST和TESTOFF #if指令中组织,例如:

#if TEST
    ...
#endif

...MORE CODE...

#if TESTOFF
    ...
#endif
Run Code Online (Sandbox Code Playgroud)

通常情况下,一个#if区域(例如该#if TEST区域)变得可折叠而另一个#if TESTOFF区域(区域)不变.

由于这是一些可能从未遇到的奇怪现象,我正在附上有关问题的快照: 交替折叠的#if区域

有谁知道什么参数定义#if预处理器指令的这种行为行为?

c# visual-studio-2010 preprocessor-directive

2
推荐指数
1
解决办法
323
查看次数

没有参数的#define指令有什么作用?

在Apple的开源网站上,stdarg.h条目包含以下内容:

#ifndef _STDARG_H
#ifndef _ANSI_STDARG_H_
#ifndef __need___va_list
#define _STDARG_H
#define _ANSI_STDARG_H_
#endif /* not __need___va_list */
#undef __need___va_list
Run Code Online (Sandbox Code Playgroud)

如果第一个参数后面没有任何内容,那么#define语句会做什么?

c header-files c-preprocessor preprocessor-directive

2
推荐指数
1
解决办法
364
查看次数

将参数发送到#define

我想知道是否可以将参数发送到#define宏以选择不同的输出

例如:

#define Row(1) LPC_GPIO0
#define Row(2) LPC_GPIO3
#define Row(3) LPC_GPIO2
Run Code Online (Sandbox Code Playgroud)

然后在我的代码中,我创建了一个发送参数的循环

Row(x)
Run Code Online (Sandbox Code Playgroud)

c directive c-preprocessor preprocessor-directive

2
推荐指数
1
解决办法
69
查看次数

C++ MultiLine #if

I've been trying to google this for a while now but I can't seem to be able to find any clear answer if it can be done at all.

I wanted to know if it's possible to do a MultiLine #if statement in C++ in a similar way to this type of if

if (
   1 == 1 ||
   2 == 2 ||
   3 == 3
) {
   cout << "True\n";
}
Run Code Online (Sandbox Code Playgroud)

I was hoping for something along the lines …

c++ preprocessor-directive

2
推荐指数
2
解决办法
160
查看次数