Search code examples
ccompiler-warningshome-directory

implicit declaration warning when trying getting home directory


I type this code to get the home directory. I have later edited it to include all of the code:

#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;
}

It gives me the implicit declaration warning. It says something is wong with the getenv partHow can I fix it?


Solution

  • The function getenv is declared in stdlib.h according to this reference. So you need to add

    #include <stdlib.h>