为什么我会出错(C)?

时间:2014-03-24 21:04:45

标签: c include declaration

我正在尝试使用unistd.h库中的usleep函数。但是,即使我将库放在函数顶部,错误仍然存​​在。这是错误:

anfernee@AJ-crunchbang:~/Dropbox/CSCAPE/Tesla$ make calculator
clang -ggdb3 -O0 -std=c99 -Wall -Werror -lm -lcs50    calculator.c   -o calculator
calculator.c:20:2: error: implicit declaration of function 'usleep' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        usleep(180000);
        ^
1 error generated.
make: *** [calculator] Error 1
anfernee@AJ-crunchbang:~/Dropbox/CSCAPE/Tesla$ ^C
anfernee@AJ-crunchbang:~/Dropbox/CSCAPE/Tesla$ 

以下是出现错误的代码。

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

#define CLEAR printf("\033[2J");printf("\033[%d;%dH", 0, 0); //creates a constant that clears the console screen


void PResistance(void); //calculates total resistance with resistors in parallel
//void Truth(void); //determines the truth table for logic gates specified
void EForce(void); //Calculated the electric force
void Ohm(void); //calculates power

int main(void){
    int option; //Stores the user's option 
    int exit = 0;
    printf("\033[0;36;40m"); //Changes the colour of screen to cyan 
    printf("Welcome!\n"); //greet user

    usleep(180000);

我做错了什么?

1 个答案:

答案 0 :(得分:1)

usleep()已过时。您可以使用nanosleep()setitimer()作为替代方案。