implicit declaration of function 'execle' error

空扰寡人 提交于 2019-12-01 23:52:55
Sourav Ghosh

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.

You need to include unistd.h to resolve the implicit dec warning

Ani

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!