简体   繁体   English

函数“execle”错误的隐式声明

[英]implicit declaration of function 'execle' error

I keep getting我不断得到

implicit declaration of function 'execle' is invalid in C99函数“execle”的隐式声明在 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.C99 ,不允许隐式声明函数。 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.根据execle()手册页,您需要包含unistd.h以获取前向声明。

您需要包含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. execle 之后的任何内容都不会运行。

#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.我也在阅读第一本 c 书并遇到了这一部分。 I think you need two programs one should be named "dinner_info"我认为你需要两个程序,一个应该命名为“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 你需要在其中包含头文件 unistd.h 因为你将调用execle函数

//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.特别注意你如何设置 JUICE 环境变量,JUICE、等号和桃子之间不能有空格。 I kept getting null values for JUICE because of this omission.由于这个遗漏,我一直为 JUICE 获取空值。

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 .那么这里发生了什么,您正在调用一个程序,该程序调用另一个程序并使用函数execle将环境变量传递给被调用的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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