繁体   English   中英

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

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

我有一个在我的 MacOS 终端上运行的 C 程序。 已安装所有命令行工具和 GCC 编译器。 但是,对于使用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.

编码:

#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;
}

下面的Stack Overflow 交流建议我为那些被视为隐式声明的函数编写辅助函数。 但是,我不确定您是否可以对getpid()execv()做同样的事情。 我应该怎么做才能确保不会发生这种情况?

请注意:“print”只是另一个帮助文件,一旦execv()就应该运行。

请注意,您正在使用unistd.h header 文件中定义的系统调用。 因此,在不包括标准库#include <unistd.h>的情况下调用它们相当于“隐式声明”=“在没有先定义的情况下调用 function”。

暂无
暂无

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

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