我想在我的项目中包含外部库,但我遇到了一些问题。
我的项目结构:
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'
我尝试了包含&#34; size_t&#34;的<stddef.h>
。定义,但它没有帮助。
答案 0 :(得分:1)
slist.h和queue.h需要包含stddef.h
。或者您可以添加stdlib.h
,而stddef.h
又包含{{1}}。根据经验,尝试始终包含h文件中的库,而不是c文件。
答案 1 :(得分:-1)