简单C语言问题 linker error

来源:百度知道 编辑:UC知道 时间:2024/06/06 15:49:26
我写了一个贪食蛇程序。才写一半:
#define N 200
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x110b
int i,key;
int score=0;/*it is the score that players get*/
struct Food
{
int x;/*the X of food*/
int y;/*the Y of food*/
int yes;/*wether there will be food*/
}food;/*struct for food*/
struct Snake
{
int x[N];
int y[N];
int node;/*how long the snake is*/
int direction;
int life;/*the life of snake 1:living 2:die*/
}snake;/*for snake*/
void Init(void);/*pictrue start*/
void Close(void);/*pictrue over*/
void DrawK(void);/*begin*/
void Gameover(void);/*over*/
void Gameplay(void);/*the prosedure in detils*/
void PrScore(void);/*the score*/
/*The main function*/
void main()
{
Init();/*to prepare*/
Dr

这种提示是连接错误(Linker error)。
一般可执行程序的过程是:源代码->目标代码(经过编译)->可执行程序(经过连接)。
你的问题就出在最后一步。那么为何出现这样的问题呢?
既然编译通过,说明你的源程序没问题。那么连接出错由什么原因引起的呢?
一般我们写程序不可能每个功能自己写,一些共用的功能(函数)被封装在库中(主要有两个文件:.h和.lib),你上面的错误说明graphics.h是存在的,所以没有编译错误,但是graphics.lib找不到,就不能进行连接。
解决方法:不管你用什么方法找到graphics.lib,复制到你编程环境中的LIB目录中去。
给你一个完整的贪吃蛇程序(要将TC拷贝至C盘下):
/*共有两关,有记时器和记分器;
按Enter键开局;
在游戏过程中,按ESC键退出,按Enter键重新开局; */
#include <stdlib.h>
#include <graphics.h>
#include <bios.h>
#include <dos.h>
#include <conio.h>
#define Enter 7181
#define ESC 283
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
void interrupt (*oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS);
void SetTimer(void interrupt (*IntProc)(__CPPARGS));
void KillTim