0

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?

  • You need to include `` – Osiris Dec 17 '18 at 22:54
  • In my real program I did, I forgot to add it to my question –  Dec 17 '18 at 22:55
  • 1
    Please always post the code you are actually compiling. With the new code I can't reproduce the problem, what does your compiler exactly say? – Osiris Dec 17 '18 at 22:57
  • @Osiris I am sorry. –  Dec 17 '18 at 22:58
  • It's no problem. What is the exact line the compiler outputs? On which platform are you working? – Osiris Dec 17 '18 at 23:00
  • Possible duplicate of [warning: implicit declaration of function](https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function) – MyStackRunnethOver Dec 17 '18 at 23:02
  • linux, but what do you mean by which line does the compiler output? –  Dec 17 '18 at 23:02
  • @MyStackRunnethOver That's not a helpful question. He's calling a function that should be declared in a header that he's included. – Barmar Dec 17 '18 at 23:03
  • 1
    The whole warning the compiler outputs could be helpful for us. – Osiris Dec 17 '18 at 23:04
  • Try putting `#define _POSIX_SOURCE` before all the `#include`s. – Barmar Dec 17 '18 at 23:05
  • please answer my q –  Dec 17 '18 at 23:19
  • 1
    As said we need the whole warning the compiler outputs, for example before including `unistd.h` i got `:10:28: warning: implicit declaration of function 'getuid'; did you mean 'getpwuid'? [-Wimplicit-function-declaration]` – Osiris Dec 17 '18 at 23:23
  • @JulianTiemann i cant reproduce it: `~/code$ gcc -Wall -Wimplicit-function-declaration test.c -o test ~/code$ ./test Tried again, Home Directory Found!` – EsmaeelE Dec 17 '18 at 23:26
  • Is there any more code missing from the post? The message `printf("Tried again, Home Directory Found!\n");` appears to be in the wrong code block, since in its position there was only one attempt. The repeat attempt is in the code block above, which doesn't print anything on success. – Weather Vane Dec 17 '18 at 23:31
  • I made a mistake –  Dec 17 '18 at 23:56
  • EVERYONE I OMMITED SOME CODE BECAUSE I THOUGHT IT WOULD CAUSE CONFUSION! –  Dec 17 '18 at 23:58
  • I LATER EDITED IT TO bE COMPLETE! –  Dec 18 '18 at 00:03
  • But now `stdlib.h` is missing where `getenv` is defined, which was included in your previous code. – Osiris Dec 18 '18 at 00:03
  • @Osiris thx that solved my problem post that as an answer and it will be accepted –  Dec 18 '18 at 00:05
  • A general rule to follow: before using *any* library function, read its documentation to find out what header declares it, and the types and meanings of its parameters and return value. Also, when posting a question, don't just describe the error message, copy-and-paste the message into your question, formatted as code. – Keith Thompson Dec 18 '18 at 01:48

1 Answers1

0

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

#include <stdlib.h>
Osiris
  • 2,783
  • 9
  • 17