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

implicit declaration warning when trying getting 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 部分有问题我该如何解决?

函数getenv根据this引用在stdlib.h中声明。所以你需要添加

#include <stdlib.h>