简体   繁体   中英

implicit declaration of function 'execle' error

I keep getting

implicit declaration of function 'execle' is invalid in C99

when compiling the code below. What am I missing?

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

char *my_env[] = {"JUICE=PEACH and apple", NULL};

int main (int argc, char *argv[])
{
  execle ("diner_info", "diner_info", "4", NULL, my_env);
  printf ("Diners: %s\n", argv[1]);
  printf ("Juice: %s\n", getenv("JUICE"));
  return 0;
}

In C99 , the implicit declaration of a function is not allowed. That means, the compiler should be aware of the function signature before it encounters a call to that function. This can be achieved two ways:

  1. Define the function before using it.
  2. Provide a forward declaration of the function and define it later.

Usually, the function signature is provided as a forward declaration through the header files.

As per the man page of execle() , you need to include unistd.h to get the forward declaration.

您需要包含unistd.h来解决隐式 dec 警告

I got it working. That's the order the statements should be as it turns out. Anything after execle won't run.

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


  char *my_env[] = {"JUICE=PEACH and apple", NULL};
int main (int argc, char *argv[]) 
{
  printf ("Diners: %s\n", argv[1]);
  printf ("Juice: %s\n", getenv("JUICE"));
  execle ("diner_info", "diner_info", "4", NULL, my_env);
  return 0;
}

Result:

# :$ gcc diner_info.c -o diner_info && ./diner_info 
Diners: (null)
Juice: (null)
Diners: 4
Juice: PEACH and apple
Diners: 4
Juice: PEACH and apple
Diners: 4
Juice: PEACH and apple
Diners: 4
Juice: PEACH and apple
Diners: 4
Juice: PEACH and apple
Diners: 4
Juice: PEACH and apple

But I still don't understand why the null values on the top, though.

I think you might have this all wrong and just in case someone else is running into this problem I'm submitting this. I'm also reading the head first c book as well and came across this section. I think you need two programs one should be named "dinner_info"

//dinner_info.c

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


int main(int argc, char* argv[])
{
    printf("Dinners: %s\n", argv[1]);
    printf("Juice: %s\n",getenv("JUICE"));
    return 0;

}

and the other should be your driver program say my_exec_program where you need to include the header unistd.h as you will be calling the execle function

//my_exec_program
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>


int main (int argc, char* argv[])
{
    char * my_env[] = {"JUICE=peach and apple", NULL};
    execle("dinner_info", "dinner_info", "4" , NULL , my_env);
}

pay special attention to how you set the JUICE environment variable there should be no spaces between JUICE, equal sign and peach. I kept getting null values for JUICE because of this omission.

So what is happening here, You're calling a program that calls another program and passing in an environment variable to the called program using the function execle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM