简体   繁体   English

C标头包含在某些文件中而不是其他文件时会导致错误

[英]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.

相关问题 C程序找不到某些包含的头文件,尽管它们存在 - C program can't find some included header files though they exist 当我尝试同时包含一些 header 文件时,如何修复错误 E0003、C1014? - How can i fix errors E0003,C1014 that occurred when i try to include some header files simultaneously? C文件包含的所有头文件的列表 - list of all header files included by a C file 多个应用程序 header 文件在 C 中包含错误 - Multiple application header files included error in C 错误:C中的类型未知。包含所有必需的头文件的原因可能是什么? - Error: unknown type in C. What can be the reason when all required header files are included? 当包含非标准标题时,YouCompleteMe不会显示错误 - YouCompleteMe doesn't show errors when non standard header is included 包含C ++ / C头文件的实现在哪里? - Where is the implementation of included C++/C header files? C header 文件是否包含在 Linux Z50484C19F1AFDAF3841A0D821ZED3 中并由他们维护? - Are C header files included in the Linux kernel and maintained by them? 关于 header 文件和 C 中的错误的 2 个问题 - 2 questions about header files and errors in C 包含在多个源文件中的头文件中的 C 预处理器 #error - C preprocessor #error in header file included in multiple source files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM