[英]C header causes errors when included in some files but not others
The title pretty much says it all. 标题基本概括了所有内容。 The precise error message that seems to be the root of it is:
似乎是其根源的确切错误消息是:
util.h:4: error: expected declaration specifiers or '...' before 'size_t'
util.h:4:错误:预期的声明说明符或“ ...”在“ size_t”之前
The header in question is: 有问题的标题是:
#ifndef UTIL_H
#define UTIL_H
void print_array(void*, int, size_t, void (*)(void*));
extern void print_int(void*);
extern void print_float(void*);
#endif /* UTIL_H */
If I compile the following file with gcc -Wall -c util.c
the compiler silently creates an object file. 如果我使用
gcc -Wall -c util.c
编译以下文件,则编译器会静默创建目标文件。
#include <stdio.h>
#include "util.h"
void print_array(void* a, int length, size_t size, void (*print)(void*)) {
unsigned int i;
for (i = 0; i < length; i++) {
print(a + i*(unsigned int)size);
}
printf("\n");
}
void print_int(void* i) {
int* a = (int*) i;
printf(" %i ", *a);
}
void print_float(void* f) {
float* a = (float*) f;
printf(" %f ", *a);
}
If I include it with any other file, I get the aforementioned error and a bunch of other ones. 如果我将其包含在任何其他文件中,则会得到上述错误和许多其他错误。 The one I've provided comes first.
我提供的是第一个。 Everything I've googled about it says that it's the result of a syntax error on a previous line but it was happening when it was the first line in the file.
我用谷歌搜索过的所有内容都说这是前一行语法错误的结果,但是当它是文件的第一行时就发生了。 I can see that if I get this error knocked out, then all the other ones will go away as they have to do with
print_array
being called with the wrong number or type of arguments (which it isn't). 我可以看到,如果敲
print_array
错误,那么所有其他错误都会消失,因为它们与使用错误的数字或类型的参数(不是)调用了print_array
。
size_t
isn't defined until you include stddef.h
. 在包含
stddef.h
之前,不会定义size_t
。 Your header should probably include that first to guarantee it's defined. 您的标头可能应该首先包含该标头,以确保已定义标头。 (As it stands, you're just getting "lucky" and having other includes that will eventually define it included first, so it doesn't cause a problem.)
(就目前而言,您只是“幸运”,拥有其他包含项将最终将其定义为首先包含,因此不会造成问题。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.