.h文件在C中的异常使用 [英] Unusual usage of .h file in C
问题描述
在阅读有关过滤的文章时,我发现使用 .h
文件有些奇怪-使用它来填充系数数组:
#define N 100 //过滤顺序
float h [N] = {#include f1.h}; //插入过滤器
float x [N]的系数;
float y [N];
short my_FIR(short sample_data)
{
float结果= 0;
for(int i = N-2; i> = 0; i--)
{
x [i + 1] = x [i];
y [i + 1] = y [i];
}
x [0] =(float)sample_data;对于(int k = 0; k
{
结果=结果+ x [k] * h [k];
}
y [0] =结果;
回报((空)结果);
}
所以,通常使用 float h [N] = {#include f1.h};
这样吗?
<像 #include
这样的href = https://en.wikipedia.org/wiki/C_preprocessor rel = noreferrer>预处理器指令只是在做一些文本替换(请参见
但是, #include
应该具有其#
作为其行的第一个非空白字符。因此,您将进行编码
float h [N] = {
#include f1.h
};
原始问题没有 #include
单独出现,所以代码有误。
这不是正常练习,但它是允许的练习。在这种情况下,我建议使用 .h
以外的其他扩展名,例如使用 #include f1.def
或 #include f1.data
...
请您的编译器向您显示预处理的表单。使用 GCC 使用 gcc -C -E -Wall yoursource.c>进行编译。 yoursource.i
,然后用编辑器或寻呼机查看生成的 yoursource.i
<我实际上更喜欢在自己的源文件中包含这些数据。因此,我建议使用以下方法生成一个自包含的
h-data.c
文件。某些工具,例如 GNU awk (因此,文件 h-data.c
将以 const float h [345] = {
开头,并以}结尾;
。 。)如果它是常量数据,最好将其声明为
const float h []
(这样它就可以位于<$ c $之类的只读段中。 c> .rodata (在Linux上)。另外,如果嵌入的数据很大,则编译器可能会花一些时间(无用地)对其进行优化(然后您可以在不进行优化的情况下快速编译 h-data.c
)。 / p> During reading article about filtering, I've found some strange using of .h
file - use it for filling array of coefficients:
#define N 100 // filter order
float h[N] = { #include "f1.h" }; //insert coefficients of filter
float x[N];
float y[N];
short my_FIR(short sample_data)
{
float result = 0;
for ( int i = N - 2 ; i >= 0 ; i-- )
{
x[i + 1] = x[i];
y[i + 1] = y[i];
}
x[0] = (float)sample_data;
for (int k = 0; k < N; k++)
{
result = result + x[k]*h[k];
}
y[0] = result;
return ((short)result);
}
So, is it normal practice to use float h[N] = { #include "f1.h" };
this way?
Preprocessor directives like #include
are just doing some textual substitution (see the documentation of GNU cpp inside GCC). It can occur at any place (outside of comments and string literals).
However, a #include
should have its #
as the first non-blank character of its line. So you'll code
float h[N] = {
#include "f1.h"
};
The original question did not have #include
on its own line, so had wrong code.
It is not normal practice, but it is permitted practice. In that case, I would suggest using some other extension than .h
e.g. use #include "f1.def"
or #include "f1.data"
...
Ask your compiler to show you the preprocessed form. With GCC compile with gcc -C -E -Wall yoursource.c > yoursource.i
and look with an editor or a pager into the generated yoursource.i
I actually prefer to have such data in its own source file. So I would instead suggest to generate a self-contained h-data.c
file using e.g. some tool like GNU awk (so file h-data.c
would start with const float h[345] = {
and end with };
...)
And if it is a constant data, better declare it const float h[]
(so it could sit in read-only segment like .rodata
on Linux). Also, if the embedded data is big, the compiler might take time to (uselessly) optimize it (then you could compile your h-data.c
quickly without optimizations).
这篇关于.h文件在C中的异常使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!