控制 CPP 预处理器的执行
Controlling the executions of CPP Preprocessor
我正在使用标准CPP来预处理任何C/CPP文件。 我正在使用以下命令进行预处理:
cpp -idirafter <path_to_header_files> -imacros <sourcefile> <source_file> > <destination_file>
上面的命令正在将所有宏替换为头文件中定义的相应实现。
如果我想要一些包含特定字符串的特定宏(例如,ASSERT(不应该被 cpp 预处理器替换。 即,如果由某些头文件中定义的TC_ASSERT_EQ或TC_ASSERT_NEQ名称定义的任何宏不应替换为预处理器。
有什么方法可以控制吗?
您可以使用预定义的宏来解决此问题。您可以使用 -D 参数将不同的定义传递给 cpp 和 gcc/g++
例如,您的断言标头可能看起来像my_assert.h:
#ifndef __my_assert_h__
#define __my_assert_h__
#if defined ASSERT_DEBUG
#define my_assert(expr) my_assert(expr) /* please dont touch my assertions */
#else
#define my_assert(expr) ((void)0U) /* Your assertion macro here */
#endif /* ASSERT_xxx */
#endif /* #ifndef __my_assert_h__ */
您的源文件可以像以前一样。例如test.c:
#include "my_assert.h"
#include <stdio.h>
void foo (int p) {
my_assert(p==1);
puts("Tralalan");
}
int main (void) {
foo(1);
return 0;
}
使用上述技巧,您可以运行cpp -DASSERT_DEBUG test.c
虽然编译仍然像以前一样工作。gcc test.c
我已经使用 #pragma 毒TC_ASSERT_EQ语句来传递该行而无需预处理。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- #定义c-预处理器常量..我做错了什么
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 预处理器:插入结构名称中的前一个行号
- 如何在c++中实现处理器调度模拟器
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 在C++中对T*类型执行std::move的意外行为
- 使用QProcess执行命令,并将结果存储在QStringList中
- C/C++预处理器是否可以检测一些编译器选项
- 如何在没有信号的情况下从C++执行QML插槽
- C++预处理器同时执行#ifdef和#ifndef
- 在高通六边形处理器的word32上执行水平求和的最快方法是什么
- 控制 CPP 预处理器的执行
- 在32位和64位处理器上运行混合的mpi可执行文件
- 通过GUI的作业进程在同一个虚拟处理器上执行,或者通过命令行使用任何空闲的虚拟处理器
- 如何让C预处理器在编译过程中执行代码