Сcomplile错误(预期声明说明符或size_t)之前“……”

时间:2021-02-08 16:05:18

I want to include external library to my project, but I have some problems with it.

我想把外部库包括到我的项目中,但是我有一些问题。

My project structure:

我的项目结构:

Project folder/ --- sources/ --- main.c
                             --- libs/  --- Queue.c
                                        --- Sllist.c
                --- headers/ --- main.h
                             --- libs/  --- Queue.h
                                        --- Sllist.h

main.c:

c:

#include "main.h"
#include "Queue.h"

QUEUE q = {0};

int main(void)
{
    return 0;
}

main.h:

main.h:

#ifndef MAIN_H__
#define MAIN_H__

#endif

Queue.c:

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:

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:

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:

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

So, as you can see my project is as clean as possible. When I try to compile get the following errors:

所以,正如你所看到的,我的项目是尽可能干净的。当我试图编译时,会得到以下错误:

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'

I tried include that contains "size_t" definition, but it doesn't help.

我尝试过包含“size_t”定义,但是没有帮助。

Thank you very much in advance!

非常感谢!

2 个解决方案

#1


1  

slist.h and queue.h need to include stddef.h. Or you could include stdlib.h which in turn includes stddef.h. As a rule of thumb, try to always include libraries from the h file, not the c file.

slist。h和队列。h需要包括stddef.h。也可以包括stdlib。h也包括stddef。根据经验,尽量包含h文件中的库,而不是c文件中的库。

#2


-1  

In your main.c source file you don't include any standard library header file that defines size_t.

在你的主。c源文件不包括任何定义size_t的标准库头文件。

Simply add e.g.

简单的添加。

#include <stddef.h>

at the top of the main.c source file, before your current includes.

在主要的顶端。c源文件,在当前包含之前。

#1


1  

slist.h and queue.h need to include stddef.h. Or you could include stdlib.h which in turn includes stddef.h. As a rule of thumb, try to always include libraries from the h file, not the c file.

slist。h和队列。h需要包括stddef.h。也可以包括stdlib。h也包括stddef。根据经验,尽量包含h文件中的库,而不是c文件中的库。

#2


-1  

In your main.c source file you don't include any standard library header file that defines size_t.

在你的主。c源文件不包括任何定义size_t的标准库头文件。

Simply add e.g.

简单的添加。

#include <stddef.h>

at the top of the main.c source file, before your current includes.

在主要的顶端。c源文件,在当前包含之前。