简体   繁体   English

代码缺少库:块

[英]Missing library for code:blocks

Recently picked up programming again. 最近又重新开始编程。 I am a beginner. 我是初学者。 I took one class a while back, but am now trying to compile and run a program I have on flash that worked fine in class using Dev C++. 我前一段时间上过一堂课,但是现在我尝试编译并运行一个我在Flash上​​运行的程序,该程序在使用Dev C ++的课堂上运行良好。 I am using the latest version of Code::Blocks at home now. 我现在在家中使用最新版本的Code :: Blocks。

Here is the program code as follows for a simple calculator program: 这是一个简单的计算器程序的程序代码,如下所示:

/* 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

Getting the following build errors when I try to compile: 尝试编译时出现以下构建错误:

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) ===|

I believe the errors are not because of code errors (I KNOW the code worked before) but because I am using Code::Blocks instead of Dev C++ now, I need to reference a different library but have no idea which library i need. 我相信这些错误不是由于代码错误(我知道该代码以前工作过),而是因为我现在使用Code :: Blocks而不是Dev C ++,所以我需要引用其他库,但不知道我需要哪个库。

And help would be greatly appreciated. 并且帮助将不胜感激。

You can clearly see that add, sub, mul, divn, printResult are declared but not defined. 您可以清楚地看到add,sub,mul,divn,printResult已声明但未定义。 Therefore, in order to use them you would need to define them. 因此,为了使用它们,您需要定义它们。 Also, your code is inaccurate: 另外,您的代码不正确:

  1. Takes two int as input, but perform all of the operations on float ... 接受两个int作为输入,但是在float上执行所有操作...
  2. Secondly, as stated above you do not define your functions anywhere but proceed to use them. 其次,如上所述,您不会在任何地方定义函数,而是继续使用它们。
  3. Finally, some of the function declared ( getOption, getData & calc ) are defined, why not do the same for the arithmetic functions and the printResult. 最后,定义了一些声明的函数( getOption,getData和calc ),为什么不对算术函数和printResult做同样的事情。

Therefore, it is not because of a missing library but because the function were declared but not defined. 因此,这不是因为缺少库,而是因为函数已声明但未定义。 Or you might need to link another file containing the definition if such is the case. 否则,您可能需要链接另一个包含定义的文件。

-Dany -丹尼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM