簡體   English   中英

為什么我得到未定義的睡眠錯誤參考?

[英]Why I am getting undefined reference to sleep error?

碼:

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

struct subscriber {
    char phonenumber[20];
    char name[50];
    float amount;
} s;

void addrecords();
void listrecords();
void modifyrecords();
void deleterecords();
void searchrecords();
void payment();

char get;

int main()
{
    int password;
    int phonenumber;
    char choice;

    system("cls");

    printf
    ("\n\n\n\n\n\n\n\n\n**********************************************************************");
    printf("\n\t\t---WELCOME TO THE TELECOM BILLING MANAGEMENT SYSTEM---");
    printf("\n\t\t****************************************************************");

    Sleep(2000);

    getch();

    system("cls");

    while (1) {
        system("cls");
        printf("\n enter\n A : for adding new records.\n L : for list of records");
        printf("\n M : for modifying records.\n P : for payment");
        printf("\n S : for searching records.");
        printf("\n D : for deleting records.\n E : for exit\n");

        choice = getche();
        choice = toupper(choice);

        switch (choice) {
        case 'P':
            payment();
        break;

        case 'A':
            addrecords();
            break;

        case 'L':
            listrecords();
            break;

        case 'M':
            modifyrecords();
            break;

        case 'S':
            searchrecords();
            break;

        case 'D':
            deleterecords();
            break;

        case 'E':
            system("cls");
            printf("\n\n\t\t\t\tTHANK YOU");
            printf("\n\n\n\n\n:\n\tFOR USING OUR SERVICE");
            Sleep(2000);
            exit(0);
            break;

        default:
            system("cls");
            printf("Incorrect Input");
            printf("\nAny key to continue");
            getch();

        }

    }

}

錯誤:

proj.c:(.text+0x53): undefined reference to `Sleep'
proj.c:(.text+0x5d): undefined reference to `getch'
proj.c:(.text+0xbb): undefined reference to `getche'
proj.c:(.text+0x17f): undefined reference to `Sleep'
proj.c:(.text+0x1c1): undefined reference to `getch'
/tmp/cc4UYi0H.o: In function `addrecords':
proj.c:(.text+0x244): undefined reference to `getch'
proj.c:(.text+0x340): undefined reference to `getche'
/tmp/cc4UYi0H.o: In function `listrecords':
proj.c:(.text+0x44c): undefined reference to `getch'
/tmp/cc4UYi0H.o: In function `deleterecords':
proj.c:(.text+0x5b2): undefined reference to `getch'
proj.c:(.text+0x632): undefined reference to `getch'
/tmp/cc4UYi0H.o: In function `searchrecords':
proj.c:(.text+0x791): undefined reference to `getch'
/tmp/cc4UYi0H.o: In function `payment':
proj.c:(.text+0xb1f): undefined reference to `getch'
collect2: ld returned 1 exit status

我嘗試了此處給出的解決方案( 未定義的“ sleep”引用,但確實包含了<unistd.h> ),但仍然無法正常工作。 我在Ubuntu 12.04上使用gcc。

您的代碼似乎來自Windows操作系統。 在Linux上,功能Sleep不存在( Sleep是從Windows API函數!)。 嘗試sleep (來自<unistd.h> )。 alk所述,請記住, Sleepsleep的論點是不同的:

  • Sleep以毫秒為單位;
  • sleep需要幾秒鍾的時間。

同樣, getch函數和cls shell命令不適用於GNU / Linux。

getch()不是標准庫的一部分,它是庫中的一個函數,您忘了聲明使用

根據標准刪除getchcls您的代碼不可移植

睡眠用sleep(n); //n is number of seconds sleep(n); //n is number of seconds

有關終端的更多信息類型: man 3 sleep

還有一個建議,當您將代碼從Windows移植到Linux時

嘗試使用gcc -Wall -Werror選項進行編譯

暫無
暫無

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

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