code::blocks编译多文件:例如 《C primer plus》的9.9 ,9.10,9.11

           code::blocks是一款据说灰常强大的IDE,就是一个以上的源文件并且加入其他非标准的头文件(自定义头文件),今天想做一个多文件的编译,用的Codeblock,结果却非常不幸的无法编译过,在主函数里调用另一个源文件里的函数,却提示没有定义的引用,若干分钟后终于找出正解:

  • 步骤1:新建工程:File - New - Project... - Console application - C - 选择路径,以及命名:usehotel   --  双击Sources:

             

                   

  • 步骤2: 在main.c中编写代码    :
// usehotel.c -- 房间费率程序
/* 与程序清单 9.10 hotel.c 一起编译 */
#include <stdio.h>
#include <stdlib.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 = HOTEL2;
            break;
        case 3:
            hotel_rate = HOTEL3;
            break;
        case 4:
            hotel_rate = HOTEL4;
            break;
        default:
            hotel_rate = 0.0;
            printf("Oops!\n");
            break;
        }
        nights = getnights();
        showprice(hotel_rate, nights);
    }
    printf("Thank you and goodbye.\n");

    return 0;
}
  • 步骤3:  在usehotel项目中添加文件(同一目录下)

              

  • 步骤4:添加源文件 hotel.c   点击文件+ (如下图所示)--   File...  --   C/C++ Source   --   C   --   勾选“Debug”“Release”  --  填写源文件名:hotel

           

                

              

          所得结果:     

  • 步骤5:编写代码 hotel.c

/* 9.10 hotel.c -- 酒店管理函数 */
#include <stdio.h>
#include <stdlib.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");
    printf("3) Chertworthy Plaza     4) The Stockton \n");
    printf("5) quit  \n");
    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);
}
  • 步骤6: 添加文件hotel.h   文件+   --   File...  (与步骤3同)--  C/C++ header   --  勾选“Debug”“Release”  -- 填写头文件名:hotel

          

          

  • 步骤7:编写代码:hotel.h
#ifndef HOTEL_H_INCLUDED
#define HOTEL_H_INCLUDED

/* hotel.h -- 符号常量和hotel.c 中所有函数的原型 */
#define QUIT          5
#define HOTEL1   180.00
#define HOTEL2   225.00
#define HOTEL3   255.00
#define HOTEL4   355.00
#define DISCOUNT   0.95
#define STARS  "**************************"

//显示选择列表
int menu(void);

//返回预定天数
int getnights(void);

//根据费率、入住天数计算费用
//并显示结果
void showprice(double rate, int nights);


#endif // HOTEL_H_INCLUDED
  • 步骤8:编译运行

         

 

成功!!!

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值