简体   繁体   English

C99 中的标准函数被称为“隐式声明”

[英]Standard functions in C99 being called "Implicit declarations"

I have a C program that I am running on my MacOS terminal.我有一个在我的 MacOS 终端上运行的 C 程序。 All command line tools and GCC compiler have been installed.已安装所有命令行工具和 GCC 编译器。 However for using functions like getpid() or execv() it gives the following error :但是,对于使用getpid()execv()之类的函数,会出现以下错误

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.下面的Stack Overflow 交流建议我为那些被视为隐式声明的函数编写辅助函数。 However, I am not sure you could do the same with getpid() or execv() .但是,我不确定您是否可以对getpid()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.请注意:“print”只是另一个帮助文件,一旦execv()就应该运行。

Note that you are using system calls that are defined in the unistd.h header file.请注意,您正在使用unistd.h header 文件中定义的系统调用。 Therefore calling them without including the std library #include <unistd.h> amounts to "implicit declaration" = "calling a function without defining it first".因此,在不包括标准库#include <unistd.h>的情况下调用它们相当于“隐式声明”=“在没有先定义的情况下调用 function”。

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

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