Home > Back-end >  Standard functions in C99 being called "Implicit declarations"
Standard functions in C99 being called "Implicit declarations"

Time:08-25

I have a C program that I am running on my MacOS terminal. All command line tools and GCC compiler have been installed. However for using functions like getpid() or execv() it gives the following error:

execv-test.c:7:35: error: implicit declaration of function 'getpid' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        printf("Pid before execv: %d\n", getpid());
                                         ^
execv-test.c:8:2: error: implicit declaration of function 'execv' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        execv("print",NULL);
        ^
2 errors generated.

The code:

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

int main (void)
{
        printf("The game is never over, John. But there may be some new players now.\n");
        printf("Pid before execv: %d\n", getpid());
        execv("print",NULL);
        printf("Returned from execv call.\n");
        return 0;
}

The following Stack Overflow exchange suggested that I write helper functions for the ones that were taken as implicit declarations. However, I am not sure you could do the same with getpid() or execv(). What should I do to make sure this doesn't happen?

PLEASE NOTE: "print" is just another helper file that is supposed to be run once execv() is called.

CodePudding user response:

Note that you are using system calls that are defined in the unistd.h header file. Therefore calling them without including the std library #include <unistd.h> amounts to "implicit declaration" = "calling a function without defining it first".

  • Related