c - 为什么我收到未定义的 sleep 引用错误?

标签 c include sleep

代码:

#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。

最佳答案

您的代码似乎来自Windows操作系统。在 Linux 上,函数 Sleep不存在( Sleep 是 Windows API 中的函数!)。试试sleep (来自 <unistd.h> )。正如 alk 所说,请记住 Sleep 的论点。和sleep不同的是:

  • Sleep花费的时间以毫秒为单位;
  • sleep花费的时间以秒为单位。

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

关于c - 为什么我收到未定义的 sleep 引用错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13211700/

相关文章:

c - 如何在滚动平均程序中输出第一个滚动平均值?

c - 通过 fgets() 接受用户的多行输入

qt - 包含来自单独目录的文件时的 undefined reference

php - 从包含的文件调用函数?

android - BOOT_COMPLETED 的 BroadcastReceiver 未从 sleep() 返回

c - MPI 中是否有一种方法类似于 MPI_Bcast 但针对单个进程,而不是对整个通信器进行操作?

c - C中是否允许通过指向不完整数组类型的指针的多维数组 "reformat"?

php - 如何使用 PHP 执行 Apache 子请​​求?

C# |控制台应用 |如何让程序在执行下一行之前等待(时间以毫秒为单位)?

objective-c - 有没有办法监控 Mac 何时即将进入休眠状态?