C程序 无法解析的外部符号

一共三个文件 要求把1 2号文件放在一起编译 我把三个文件放在一个文件夹
然后打开a.c文件编译通过 点Build时出现提示无法解析的外部符号(就是2号文件内的那几个) 望指点!!! 主要是不理解怎么把1 2号文件一起编译!
1.程序名 a.c
# include <stdio.h>
# include "hotel.h" // 定义常量、声明函数
int main(void)
{
int nights;
double hotel_rate;
int code;
while((code = menu()) != QUIT)
{
switch(code)
{
case 1: hotel_rate = HOTEL1;
break;
case 2: hotel_rate = HOTEL1;
break;
case 3: hotel_rate = HOTEL1;
break;
case 4: hotel_rate = HOTEL1;
break;
default:hotel_rate = 0.0;
printf("oops!\n");
break;
}
nights = getnights();
showprice(hotel_rate,nights);
}
printf("thank you and goodbye.");
return 0;
}
2.程序名 hotel.c
// 9.9程序 支持模块
# include <stdio.h>
# include "hotel.h"
int menu(void)
{
int code,status;
printf("\n%s%s\n",STARS,STARS);
printf("Enter the number of the desired hotel: \n");
printf("1)fairfield arms 2)hotel olympic\n"
"3)chertworthy plaza 4)the stockton\n"
"5)退出" );
printf("%s%s\n",STARS,STARS);
while((status = scanf("%d",&code)) != 1 || (code < 1 || code >5))
{
if(status != 1)
scanf("%*s");
printf("Enter an integer from 1 to 5,please.\n");
}
return code;
}
int getnights(void)
{
int nights;
printf("how many nights are needed? ");
while(scanf("%d",&nights) != 1)
{
scanf("%*s");
printf("Please enter an integer such as 2.\n");
}
return nights;
}
void showprice(double rate,int nights)
{
int n;
double total = 0.0;
double factor = 1.0;
for(n = 1;n <= nights;n++,factor *= DISCOUNT)
total += rate * factor;
printf("the total cost will be $%0.2f.\n",total);
}
3.程序名 hotel.h
// 旅馆程序中的常量定义和函数声明
# define QUIT 5
# define HOTEL1 80.00
# define HOTEL2 125.00
# define HOTEL3 155.00
# define HOTEL4 200.00
# define DISCOUNT 0.95
# define STARS "*************************************"
//给出选项列表
int menu(void);
//返回预定的天数
int getnights(void);
//按饭店的星级和预定的天数计算价格并显示出来
void showprice(double,int);

你应该在hotel.h头文件的函数前面加入extern,以表明这是可以被外部文件访问的函数,
例如:
extern getnights(void);

另外,在编写头文件的时候,应该编写头文件保护代码,
在头文件的开始处,加入:
#ifndef HOTEL_H
#define HOTEL_H
在头文件末尾,加入:
#endif
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-09-12
#include "hotel.h"
#include "hotel.c"
把前面的写成这样就可以了,当然如果你使用VC的话,在资源管理器的”头文件“那里包含"hotel.h",然后在“源文件”哪里包含hotel.c, 如果你使用的是linux系统,编译选项如下

gcc -o hotel a.c hotel.c本回答被提问者采纳
相似回答