【问题标题】:Missing library for code:blocks缺少代码库:块
【发布时间】:2013-09-05 16:31:39
【问题描述】:

最近又开始学习编程了。我是初学者。不久前我上了一堂课,但现在我正在尝试编译和运行我在闪存上的一个程序,该程序在使用 Dev C++ 的课堂上运行良好。我现在在家使用最新版本的 Code::Blocks。

这是一个简单的计算器程序的程序代码如下:

/* This program adds, subtracts, multiplies, and divides two integers. */

#include <stdio.h>
#include <stdlib.h>

// Function Declarations
int getOption(void);
void getData (int* a, int* b);
float calc   (int option, int num1, int num2);

float add  (float num1, float num2);
float sub  (float num1, float num2);
float mul  (float num1, float num2);
divn (float num1, float num2);

void printResult (float num1, float num2, float result, int option);

int main (void)
{
// Local Declarations
int done = 0;
int option;
int num1;
int num2;
int result;

// Statements
while (!done)
{
    option = getOption();
    if (option == 5)
    done = 1;
    else
    {
        do
            {
                printf("\n\nEnter two numbers: ");
                scanf("%f %f", &num1, &num2);
                if (option == 4 && num2 == 0)

                {
                    printf("\a\n *** Error *** ");
                    printf("Second Number cannot be 0\n");
                    } //if

                    } while (option == 4 && num2 == 0);

                    switch (option)
                    {
                        case 1: result = add  (num1, num2);
                        break;
                        case 2: result = sub  (num1, num2);
                        break;
                        case 3: result = mul  (num1, num2);
                        break;
                        case 4: result = divn (num1, num2);
                        } // switch

                printResult (num1, num2, result, option);
            } // else option != 5
        } // while

        printf("\nThank you for using Calculator.\n");
        return 0;
    } // main

/* ========================= getOption ===================================
    This function shows a menu and reads the user option.
        Pre     nothing
        Post    returns a valid option */

int getOption (void)
{
// Local Declarations
int option;

// Statements
do
{
    printf("\n******************");
    printf("\n*      Menu      *");
    printf("\n*                *");
    printf("\n*  1.  ADD       *");
    printf("\n*  2.  SUBTRACT  *");
    printf("\n*  3.  MULTIPLY  *");
    printf("\n*  4.  DIVIDE    *");
    printf("\n*  5.  QUIT      *");
    printf("\n*                *");
    printf("\n******************");

    printf("\n\n\nPlease type your choice ");
    printf("and press the return key : ");
    scanf("%d", &option);

    if (option < 1 || option > 5);
    printf("Invalid option. Please re-enter.\n");

 } while (option < 1 || option > 5);
    return option;
} // getoption

当我尝试编译时出现以下构建错误:

C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `add'|
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `sub'|
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `mul'|
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `divn'|
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `printResult'|
||=== Build finished: 5 errors, 0 warnings (0 minutes, 0 seconds) ===|

我相信错误不是因为代码错误(我知道代码以前工作过)而是因为我现在使用 Code::Blocks 而不是 Dev C++,所以我需要引用不同的库但不知道我是哪个库需要。

我们将不胜感激。

【问题讨论】:

  • add、sub、mul、divn、printResult的定义在哪里?在另一个不在项目中的 .cpp 中?

标签: c++ compiler-errors libraries


【解决方案1】:

可以清楚的看到add, sub, mul, divn, printResult 是声明了但是没有定义。因此,为了使用它们,您需要定义它们。另外,您的代码不准确:

  1. 以两个 int 作为输入,但对 float 执行所有操作...
  2. 其次,如上所述,您无需在任何地方定义函数,而是继续使用它们。
  3. 最后,定义了一些声明的函数(getOption, getData & calc),为什么不对算术函数和 printResult 做同样的事情。

因此,这不是因为缺少库,而是因为函数已声明但未定义。或者,如果是这种情况,您可能需要链接另一个包含该定义的文件。

-丹妮

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多