烦人的undefined reference to...
匚匚 2010-01-16 04:51:25 以下三个文件:zwlist.c为函数定义文件;zwlist.h是相应的头文件;temp.c是主程序文件。其中我将头文件放了stdio.h所在的目录之下。
执行以下操作:
gcc -c zwlist.c
ar cqs libt.a zwlist.o
gcc temp.c -static -L. -lt
出现以下提示,不明原因
temp.o:temp.c:<.text+0x1a>: undefined reference to `_initstack`
temp.o:temp.c:<.text+0xba>: undefined reference to `_push`
temp.o:temp.c:<.text+0x111>: undefined reference to `_pop`
temp.o:temp.c:<.text+0x172>: undefined reference to `_pop`
temp.o:temp.c:<.text+0x1e9>: undefined reference to `_pop`
temp.o:temp.c:<.text+0x260>: undefined reference to `_pop`
clooect2: ld returned 1 exit status
/* =====zwlist.h===== */
typedef int ElementType;
#ifndef ZW_STACK_ARRAY
#define ZW_STACK_ARRAY
#define Min_liary (6)
struct arraystack
{
ElementType min;
ElementType top;
ElementType *array;
};
typedef struct arraystack *stack_zw;
stack_zw initstack(int);
int push(stack_zw,ElementType);
ElementType pop(stack_zw); //
ElementType top(stack_zw); //
#endif
/* ==== zwlist.c ==== */
#include "zwlist.h"
#include "stdio.h"
#include "stdlib.h"
/*用数组实现栈*/
#ifndef ZW_STACK_ARRAY
#define ZW_STACK_ARRAY
stack_zw initstack(int n)//创建头结点及数组
{
stack_zw s;
n=n<Min_liary?Min_liary:n;
if((s=(stack_zw)malloc(sizeof(struct arraystack)))==NULL)
{
printf("未能成功创建头结点!\n");
return NULL;
}
s->min=n;
s->top=-1;
s->array=(ElementType *)malloc(n*sizeof(ElementType));
return s;
}
int push(stack_zw k,ElementType x)//入栈
{
if(k->top==k->min-1)
{
printf("数组已满\n");
return 0;
}
k->array[++k->top]=x;
return 1;
}
ElementType pop(stack_zw m)//出栈
{
if(m->top==-1)
{
printf("数组已空\n");
return 0;
}
return m->array[m->top--];
}
#endif //ZW_STACK_ARRAY
/* ===== temp.c ===== */
#include<time.h>
#include <stdio.h>
#include<stdlib.h>
#include "zwlist.h"
int main(void)
{
int card[52];
int pos;
int i,temp;
long temptime;
stack_zw s;
s=initstack(100);
srand(time(&temptime)%60);
for(i=0;i<52;i++)
card[i]=0;
i=0;
while(i!=52)
{
pos=rand()%52;
if(card[pos]==0)
{
push(s,pos);
card[pos]=1;
i++;
}
}
printf(" 1 2 3 4 \n");
printf(" ============================\n");
for(i=0;i<4;i++)
{
temp=pop(s);
printf(" [%c%2d] ",temp/13+3,temp%13+1);
temp=pop(s);
printf(" [%c%2d] ",temp/13+3,temp%13+1);
temp=pop(s);
printf(" [%c%2d] ",temp/13+3,temp%13+1);
temp=pop(s);
printf(" [%c%2d] ",temp/13+3,temp%13+1);
printf("\n");
}
}