C 编译错误(预期的声明说明符或 'size_t' 之前的“...”)
C compile error (expected declaration specifiers or '...' before 'size_t')
我想将外部库包含到我的项目中,但我遇到了一些问题。
我的项目结构:
Project folder/ --- sources/ --- main.c
--- libs/ --- Queue.c
--- Sllist.c
--- headers/ --- main.h
--- libs/ --- Queue.h
--- Sllist.h
main.c:
#include "main.h"
#include "Queue.h"
QUEUE q = {0};
int main(void)
{
return 0;
}
main.h:
#ifndef MAIN_H__
#define MAIN_H__
#endif
Queue.c:
#define NDEBUG // if defined then delete assert checks
#include <string.h>
#include <assert.h>
#include "Queue.h"
int QueueAdd(QUEUE *Queue,
int Tag,
void *Object,
size_t Size)
{
// many code
}
// Also many code
Queue.h:
#ifndef QUEUE_H__
#define QUEUE_H__
#include "Sllist.h"
typedef struct
{
#ifndef NDEBUG
int CheckInit1;
#endif
SLLIST *HeadPtr;
SLLIST *TailPtr;
size_t NumItems; // line 49
#ifndef NDEBUG
int CheckInit2;
#endif
} QUEUE;
int QueueAdd(QUEUE *Queue,
int Tag,
void *Object,
size_t Size); // line 59
// Also many code
#endif
Sllist.c:
#define NDEBUG // if defined then delete assert checks
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "Sllist.h"
int SLAdd(SLLIST **Item,
int Tag,
void *Object,
size_t Size)
{
// many code
}
// Also many code
Sllist.h:
#ifndef SLLIST_H__
#define SLLIST_H__
typedef struct SLLIST
{
int Tag;
struct SLLIST *Next;
void *Object;
size_t Size; // line 45
} SLLIST;
/* Add new item immediately after current item */
int SLAdd(SLLIST **Item,
int Tag,
void *Object,
size_t Size); // line 52
// Also many code
#endif
因此,如您所见,我的项目尽可能干净。当我尝试编译时出现以下错误:
In file included from headers/libs/Queue.h:34:0,
from sources/main.c:7:
headers/libs/Sllist.h:45:3: error: expected specifier-qualifier-list before 'size_t'
headers/libs/Sllist.h:52:11: error: expected declaration specifiers or '...' before 'size_t'
In file included from sources/main.c:7:0:
headers/libs/Queue.h:49:3: error: expected specifier-qualifier-list before 'size_t'
headers/libs/Queue.h:59:14: error: expected declaration specifiers or '...' before 'size_t'
我试过 include <stddef.h>
包含 "size_t" 定义,但它没有帮助。
slist.h 和 queue.h 需要包括 stddef.h
。或者您可以包含 stdlib.h
,后者又包含 stddef.h
。根据经验,尝试始终包含 h 文件中的库,而不是 c 文件中的库。
在您的 main.c
源文件中,您没有包含任何定义 size_t
.
的标准库头文件
只需添加例如
#include <stddef.h>
在 main.c
源文件的顶部,在当前包含之前。
我想将外部库包含到我的项目中,但我遇到了一些问题。
我的项目结构:
Project folder/ --- sources/ --- main.c
--- libs/ --- Queue.c
--- Sllist.c
--- headers/ --- main.h
--- libs/ --- Queue.h
--- Sllist.h
main.c:
#include "main.h"
#include "Queue.h"
QUEUE q = {0};
int main(void)
{
return 0;
}
main.h:
#ifndef MAIN_H__
#define MAIN_H__
#endif
Queue.c:
#define NDEBUG // if defined then delete assert checks
#include <string.h>
#include <assert.h>
#include "Queue.h"
int QueueAdd(QUEUE *Queue,
int Tag,
void *Object,
size_t Size)
{
// many code
}
// Also many code
Queue.h:
#ifndef QUEUE_H__
#define QUEUE_H__
#include "Sllist.h"
typedef struct
{
#ifndef NDEBUG
int CheckInit1;
#endif
SLLIST *HeadPtr;
SLLIST *TailPtr;
size_t NumItems; // line 49
#ifndef NDEBUG
int CheckInit2;
#endif
} QUEUE;
int QueueAdd(QUEUE *Queue,
int Tag,
void *Object,
size_t Size); // line 59
// Also many code
#endif
Sllist.c:
#define NDEBUG // if defined then delete assert checks
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "Sllist.h"
int SLAdd(SLLIST **Item,
int Tag,
void *Object,
size_t Size)
{
// many code
}
// Also many code
Sllist.h:
#ifndef SLLIST_H__
#define SLLIST_H__
typedef struct SLLIST
{
int Tag;
struct SLLIST *Next;
void *Object;
size_t Size; // line 45
} SLLIST;
/* Add new item immediately after current item */
int SLAdd(SLLIST **Item,
int Tag,
void *Object,
size_t Size); // line 52
// Also many code
#endif
因此,如您所见,我的项目尽可能干净。当我尝试编译时出现以下错误:
In file included from headers/libs/Queue.h:34:0,
from sources/main.c:7:
headers/libs/Sllist.h:45:3: error: expected specifier-qualifier-list before 'size_t'
headers/libs/Sllist.h:52:11: error: expected declaration specifiers or '...' before 'size_t'
In file included from sources/main.c:7:0:
headers/libs/Queue.h:49:3: error: expected specifier-qualifier-list before 'size_t'
headers/libs/Queue.h:59:14: error: expected declaration specifiers or '...' before 'size_t'
我试过 include <stddef.h>
包含 "size_t" 定义,但它没有帮助。
slist.h 和 queue.h 需要包括 stddef.h
。或者您可以包含 stdlib.h
,后者又包含 stddef.h
。根据经验,尝试始终包含 h 文件中的库,而不是 c 文件中的库。
在您的 main.c
源文件中,您没有包含任何定义 size_t
.
只需添加例如
#include <stddef.h>
在 main.c
源文件的顶部,在当前包含之前。