【发布时间】:2019-12-25 07:43:46
【问题描述】:
我想使用 c 中的静态库来实现堆栈及其功能。但即使在使用 extern 在头文件中声明 STACK 之后,我仍然收到未声明 STACK 的错误。我已在单独的 stack_1.c 文件中声明了堆栈,并希望其他函数(如 push、pop 等)访问此堆栈。有可能吗所以呢?
-
列表项
在 stack_main.c 中
#define MAX 5 #include "lib.h" void push(int, STACK *); int pop(STACK *); void display(STACK *); int main() { int flag,ele,show; STACK s; s.top=-1; while(1) { printf("Enter 1:push 2:pop 3:display 4:exit\n"); scanf("%d",&flag); switch(flag) { case 1: printf("Enter ele\n"); scanf("%d",&ele); push(ele,&s); break;在 lib.h 中
extern STACK; int main(); void push(int , STACK *); int pop(STACK *); void display(STACK *);在 push.c 中
#include "lib.h" void push(int ele, STACK* a) { if(a->top!= MAX-1) { a->top++; a->stk[a->top]=ele; } }在stack_1.c中
#define MAX 5 #include "lib.h" typedef struct { int stk[MAX] ; int top; }STACK;错误:
ani@ani-VirtualBox:~/Documents/ostl/week4$ gcc -c stack_main.c stack_1.c push.c pop.c display.c 在 stack_main.c:4:0 包含的文件中: lib.h:1:8:警告:在声明“STACK”[-Wimplicit-int] 时,类型默认为“int” 外部堆栈; ^~~~~ lib.h:3:17:错误:预期的声明说明符或“...”在“堆栈”之前 无效推(int,堆栈*); ^~~~~ lib.h:4:9:错误:预期的声明说明符或“...”在“堆栈”之前 诠释弹出(堆栈*); ^~~~~ lib.h:5:14:错误:预期的声明说明符或“...”在“堆栈”之前 无效显示(堆栈*); ^~~~~ stack_main.c:8:16:错误:预期的声明说明符或“...”在“堆栈”之前 无效推(int,堆栈*); ^~~~~ stack_main.c:9:9:错误:预期的声明说明符或“...”在“堆栈”之前 诠释弹出(堆栈*); ^~~~~ stack_main.c:10:14:错误:预期的声明说明符或“...”在“堆栈”之前 无效显示(堆栈*); ^~~~~ stack_main.c:在函数'main'中: stack_main.c:16:11: 错误:预期的‘;’在‘s’之前 堆栈; ^ stack_main.c:17:5: error: ‘s’ undeclared (第一次在这个函数中使用) s.top=-1;
【问题讨论】:
标签: c stack static-libraries typedef extern