尝试获取主目录时的隐式声明警告

时间:2018-12-17 22:51:47

标签: c compiler-warnings home-directory

我键入以下代码以获取主目录。我后来对其进行了编辑,以包括所有代码:

#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdarg.h>
#include "anotherfile.h"

typedef unsigned int uint;

void Interval(void) {
    static uint S = 0;
    static uint C = 0;
    static uint M = 0;
    static uint D = 0;
    usleep(10e5/0x20);
    printf("%d\n", C);
    printf("%d\n", S);
    printf("%d\n", M);
    if(C == 0x20) {
        if(S == 59) {
            S=0;
            M++;
        }else{S++;}
        C=0;
    }else{C++;}
    Interval();
}

int main(int argc, const char *argv[]) {
    char *HomeDir;
    if((HomeDir = getenv("HOME")) == NULL) {
        HomeDir = getpwuid(getuid())->pw_dir;
        if(HomeDir == NULL) {
            printf("Failed to get Home Directory\n");
        }else{printf("Retry Home Directory Found\n");}
    }else{printf("Success getting Home Directory\n");}
    Interval();
    return 0;
}

它给了我隐式声明警告。它说getenv部分有些东西我该如何解决?

1 个答案:

答案 0 :(得分:0)

根据this参考,在getenv中声明了函数stdlib.h。所以你需要添加

#include <stdlib.h>