如何在跨文件进行预处理时获取唯一的值 [英] How to get unique values at preprocessing across files

查看:156
本文介绍了如何在跨文件进行预处理时获取唯一的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PROBLEM



我需要一种使用预处理器指令生成唯一值的方法。目的是每次调用宏时,它将具有唯一的整数标识符。但它应该保留它的文件的价值。类似一个预处理器计数器,用于调用该函数的次数。



更多信息



我使用的宏是:

  #define LOG_MSG(a)log_msg(?)




  1. 'a'是用户想要打印的字符串。

  2. log_msg是用于在UART上打印消息的自定义函数

  3. 如果我需要帮助的部分,请使用'?'。

此宏仅在单个位置定义。在预处理阶段,'?'将被唯一标识符替换。我们正在这样做,以减少字符串带来的开销,因为此代码将在嵌入式设备上运行。在预处理之后,将提取标识符和相关的字符串,并创建一个将映射它们的表(这将在应用程序侧)。



由于这将跨多个文件使用,我想要为跨多个文件的每个用途生成一个唯一的标识符(整数不是字符串)(每个唯一字符串的标识符将是理想的,但不是必需的) / p>

任何想法?



如果有任何遗漏或不完整的信息,请提及



注意



__ COUNTER __ 是我的第一件事尝试,但它不会跨越文件。

__ FILE __ 会给我一个字符串,以失败的目的。



有些人提到使用唯一的文件标识符。但我不想静态地分配这些。我们正在使用CCS(它基于Eclipse Kepler构建)来构建此代码。所以我在想,我们可以添加一些东西到构建系统来做@embedded_guy提到的。任何人都知道如何?



谢谢

解决方案

您的解决方案与标准C预处理器无法实现,并且需要不同的解决方案。此外,提出的解决方案还不完整 - 单独生成UID不够,您需要能够将该UID与原始字符串相关联,并且还不清楚如何实现。



写一个单独的自定义预处理器似乎更简单,该预处理器在源中搜索 LOG_MSG(< some_string>)的实例,提取< some string> 并用预处理器生成的UID替换它,并构建一个字符串表供主机使用。



宏定义将是:

  #define LOG_MSG(a)log_msg(a)

但是在原始的未预处理代码 a 中将是一个文字字符串, log_msg()将定义 log_msg(int a)



然后执行预处理器将是必需的 - 编译前的步骤正确。大多数IDE支持预构建和预编译步骤,可用于集成此工具,或添加为例如make规则是很简单的。



任何尝试编译未预处理的代码将无法编译,因为该参数将是一个字符串字面值而不是整数,所以没有任何省略执行预处理。



您需要确保的唯一的事情是,您使用主机上与目标代码的特定构建相关联的字符串表 - 但是在任何情况下你都有这个问题。



请注意,只有当 a 是一个文字字符串 - 但是在任何情况下,最初提出的解决方案都是如此。您的预处理器可以检查并发出错误是文字字符串未通过。


PROBLEM

I need a way to generate unique values using a preprocessor directive. The aim is that each time the macro is called, it will have a unique integral identifier. But it should retain it's value across files. Kind of like a preprocessor counter for the number of times a call is made to the function.

FURTHER INFO

The macro I am using is:

#define LOG_MSG(a) log_msg(?)

  1. 'a' is a string that the user wants to print.
  2. log_msg is a custom function used to print a message on the UART
  3. The '?' if the part I need help with.

This macro would be defined at a single place only. During the preprocessing stage, the '?' would be replaced by a unique identifier. We are doing this to reduce the overhead that comes with strings as this code will run on an embedded device. After the preprocessing the identifiers and the related strings would be extracted and a table would be created that would map them (this would be on the application side).

Since this will be used across multiple files, I wanted a way to generate a unique identifier (integral not string) for every use across the multiple files(an identifier for every unique string would be ideal but not necessary).

Any ideas?

Please do mention if there is any missing or incomplete information

Notes

__COUNTER__ was the first thing I tried, but it doesn't hold across files.
__FILE__ would give me a string which defeats the purpose.

Some people mentioned using unique file identifiers. But I don't want to statically allocate these. We are using CCS(it's built on Eclipse Kepler) to build this code. So I was thinking that we could add something to the build system to do what @embedded_guy mentioned. Anyone know how to that?

Thanks

解决方案

I would suggest that your solution is unimplementable with the standard C preprocessor, and a different solution is required. Also the proposed solution is incomplete - generating a UID alone is insufficient, you need to be able to associate that UID with the original string, and it is not clear how that is to be achieved.

It seems it would be simpler to write a separate custom preprocessor that searches the source for instances of LOG_MSG( <some_string> ), extracts <some string> and replaces it with a UID generated by your preprocessor, and builds a string table for use by the host.

The macro definition would then be:

#define LOG_MSG(a) log_msg( a )

But while in the original un-preprocessed code a will be a literal string, log_msg() will be defined log_msg( int a ).

Execution of your preprocessor will then be a necessary pre-build step before compilation proper. Most IDEs support pre-build and pre-compile steps that can be used to integrate this tool, or adding as a make rule for example is simple enough.

Any attempt to compile un-preprocessed code will fail to compile because the parameter would be a string literal rather than an integer, so there is no danger of omitting to perform the pre-processing.

The only thing you will need to ensure than is that you use the string-table on the host that is associated with the specific build of the target code - but you had that problem in any case.

Note that it will only work when a is a literal string - but that is the case with the originally proposed solution in any case. Your pre-processor could check that and issue an error is a literal string were not passed.

这篇关于如何在跨文件进行预处理时获取唯一的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆