簡體   English   中英

代碼缺少庫:塊

[英]Missing library for code:blocks

最近又重新開始編程。 我是初學者。 我前一段時間上過一堂課,但是現在我嘗試編譯並運行一個我在Flash上​​運行的程序,該程序在使用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已聲明但未定義。 因此,為了使用它們,您需要定義它們。 另外,您的代碼不正確:

  1. 接受兩個int作為輸入,但是在float上執行所有操作...
  2. 其次,如上所述,您不會在任何地方定義函數,而是繼續使用它們。
  3. 最后,定義了一些聲明的函數( getOption,getData和calc ),為什么不對算術函數和printResult做同樣的事情。

因此,這不是因為缺少庫,而是因為函數已聲明但未定義。 否則,您可能需要鏈接另一個包含定義的文件。

-丹尼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM