【问题标题】:Why I am getting undefined reference to sleep error?为什么我得到未定义的睡眠错误引用?
【发布时间】:2012-11-03 17:33:16
【问题描述】:

代码:

#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

我尝试了此处给出的解决方案 (Undefined reference to "sleep" but I did include <unistd.h>),但仍然无法正常工作。我在 Ubuntu 12.04 上使用 gcc。

【问题讨论】:

  • getch()getche()(可能是getch() 的拼写错误)也不是标准的 Unix 函数。您需要在编译时启用更多警告,以便获得有关未定义函数而不是(或以及)链接器错误的编译器警告。

标签: c include sleep


【解决方案1】:

您的代码似乎来自 Windows 操作系统。在 Linux 上,函数 Sleep 不存在(Sleep 是来自 Windows API 的函数!)。请改用sleep(来自&lt;unistd.h&gt;)。正如 alk 所说,请记住 Sleepsleep 的论点是不同的:

  • Sleep 以毫秒为单位;
  • sleep 需要以秒为单位的时间。

同样,getch 函数和cls shell 命令不适用于 GNU/Linux。

【讨论】:

  • Windows' Sleep() 休眠毫秒,而 unix' sleep() 需要几秒钟。所以可能想添加#define Sleep(ms) sleep((ms)/1000)
【解决方案2】:

getch() 不是标准库的一部分,它是库中的一个函数,你忘了声明它使用

【讨论】:

    【解决方案3】:

    按照标准,您的代码不可移植 删除 getchcls

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

    终端上的更多信息类型:man 3 sleep

    还有一个建议,当您将代码从 windows 移植到 linux 时

    尝试使用gcc -Wall -Werror 选项进行编译

    【讨论】:

      猜你喜欢
      • 2017-03-29
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 2015-03-23
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多