编译时间错误检查? [英] Compile Time Error Checking?

查看:87
本文介绍了编译时间错误检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在编译时对字符串进行错误检查的方法,

如果字符串不是正确的长度则有编译器

抛出一个错误。


我正在使用一个嵌入式软件,需要为每个设备单独构建

,以便设备序列号包含在

程序存储器。为此,C应用程序必须使用

编译分配给源代码文件中变量的序列号。

我想在内部提供编译时错误检查.c文件

如果可能的话,如果字符串的长度不正确,那么构建过程将失败,并且不存在
序列号错误的可执行文件。


有没有人听说过这个或做过这种事情?任何建议都会非常感激。


谢谢!

I am looking a way to do error checking on a string at compile time,
and if the string isn''t the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!

推荐答案

9月4日上午9:07,Bryan Crouse< crou ... @ ieee.orgwrote:
On Sep 4, 9:07 am, Bryan Crouse <crou...@ieee.orgwrote:

我正在寻找一种做错误的方法在编译时检查一个字符串,

如果字符串不正确长度,则编译器

抛出一个错误。


我正在使用嵌入式软件,每个设备需要单独构建

,以便设备序列号包含在

程序存储器中。为此,C应用程序必须使用

编译分配给源代码文件中变量的序列号。

我想在内部提供编译时错误检查.c文件

如果可能的话,如果字符串的长度不正确,那么构建过程将失败,并且不存在
序列号错误的可执行文件。


有没有人听说过这个或做过这种事情?任何建议都会非常感激。


谢谢!
I am looking a way to do error checking on a string at compile time,
and if the string isn''t the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!



哎呀......关于编译时断言的文章可以在这里找到:
http://www.ddj.com/architect/184401873

Whoops... An article on compile time assertions can be found here:
http://www.ddj.com/architect/184401873


Bryan Crouse写道:
Bryan Crouse wrote:

我正在寻找一种在编译时对字符串进行错误检查的方法,

如果字符串是然后正确的长度有编译器

抛出一个错误。


我正在使用一个需要单独构建的嵌入式软件
每个设备
,以便设备序列号包含在

程序存储器中。为此,C应用程序必须使用

编译分配给源代码文件中变量的序列号。

我想在内部提供编译时错误检查.c文件

如果可能的话,如果字符串的长度不正确,那么构建过程将失败,并且不存在
序列号错误的可执行文件。


有没有人听说过这个或做过这种事情?任何建议都会非常感激。


谢谢!
I am looking a way to do error checking on a string at compile time,
and if the string isn''t the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!



说序列号有9个职位:


char SerialNumnber [] =" 12345678" ;;


然后,声明一个像这样的1位数组


int m [sizeof(SerialNumber)== 9];


如果sizeof(SerialNumber)!= 8,表达式将产生

零,你不能声明一个负数

大小或零的数组。


---------- ----------------------------------


我希望它有效!

Say the serial number has 9 positions:

char SerialNumnber[] = "12345678";

Then, declare an array of 1 position like this

int m[sizeof(SerialNumber) == 9];

If sizeof(SerialNumber) != 8 the expression will yield
zero, and you can''t declare an array of negative
size or zero.

--------------------------------------------

I hope it works!


Bryan Crouse写于09/04/07 10:07,:
Bryan Crouse wrote On 09/04/07 10:07,:

我正在寻找一个在编译时对字符串进行错误检查的方法,

如果字符串的长度不正确则编译器

抛出错误。


我正在使用嵌入式软件,需要单独构建

设备,以便设备序列号包含在

程序存储器中。为此,C应用程序必须使用

编译分配给源代码文件中变量的序列号。

我想在内部提供编译时错误检查.c文件

如果可能的话,如果字符串的长度不正确,那么构建过程将失败,并且不存在
序列号错误的可执行文件。


有没有人听说过这个或做过这种事情?任何建议都会非常感激。
I am looking a way to do error checking on a string at compile time,
and if the string isn''t the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.



这里有一个可怕的黑客:


char serial [] =" ..." ;; / *应为42个字符* /


/ *如果以下行产生错误,则表示

*表示`serial''(上图)没有预期的

*长度。不要理会编译器发出的错误文本

*消息;问题是

*`serial''的定义。

* /

静态char假[(sizeof serial == 42 + 1)* 2 - 1];


如果序列号长度确实为42个字符(加上一个

,则为''\0''' ),假[1]是一个合法的数组声明。

如果长度不是42,你会得到假[-1]和

错误信息。 br />

但在我看来,你在错误的地方攻击问题

。这个hack可以检查长度,并且可以扩展以检查其他一些东西,但是它不是很容易(或可维护)得到的b $ b更彻底

验证。如果你有一个规则,比如第一个

两个字符是大写字母,后面跟着五个

数字和三个字母或六个数字和两个字母信件,

后跟......那么这种技术将比它的价值更加麻烦。相反,考虑安排你的构建

程序,以便序列号由程序验证

然后使用该编号运行构建。 (例如,

它可能会将经过验证的数字写入一个小的.c文件,然后将其编译并与其他文件链接,或者它可能会

使用`-DSERIAL = AB1234ZX999'选项编译所有内容,或者

这样的东西。)我想你会发现更多

可靠而不是告诉构建者编辑文件serial.c

然后重建产品。确保没有其他人在尝试

同时构建它......


-
呃********* @ sun.com


这篇关于编译时间错误检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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