简体   繁体   English

C UNIX中的隐式函数声明

[英]Implicit Declaration of Function in C UNIX

In the following code, I get a warning that there is an implicit declaration of function getpgid. 在下面的代码中,我得到一个警告,即函数getpgid有一个隐式声明。 I know its only a warning, but its for a class and the professor wants us to treat warnings as errors. 我知道它只是一个警告,但它是一个班级,教授希望我们将警告视为错误。 So, help please. 所以,请帮忙。

I have included the appropriate header file as well so I have no idea whats wrong: 我已经包含了相应的头文件,所以我不知道什么是错的:

#include <unistd.h>

pid_t pid, pgid;

if ((pgid = getpgid(pid)) < 0) {
      app_error("Failure to get process group ID");
}

From the man page : 手册页

Feature Test Macro Requirements for glibc (see feature_test_macros(7) ): glibc的功能测试宏要求(参见feature_test_macros(7) ):

 getpgid(): _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L 

Best guess with all the elision: pid_t is undefined. 所有elision的最佳猜测:pid_t未定义。 You need both 你需要两者

#include <sys/types.h>  
#include <unistd.h>  

Otherwise you don't get what you think you're getting. 否则你没有得到你认为你得到的东西。

It would have been more helpful to provide the smallest source file that failed in the same way. 以相同的方式提供失败的最小源文件会更有帮助。 For instance, the following (a minimal elaboration of your text) doesn't generate the warning you describe for me on the first system I tried. 例如,以下(对文本的最小细节)不会在我尝试的第一个系统上生成您为我描述的警告。

#include <unistd.h>
#include <stdio.h>
int main() {
     pid_t pid, pgid;
     if((pgid = getpgid(pid)) < 0) {
          puts("Oops.");
     }
     return 0;
}

The reason reduction to a minimal failing case is important: 减少最小失败案例的原因很重要:
1. Ensures that you have adequately isolated the problem. 1.确保您已充分隔离问题。 Frequently this step makes the cause evident. 通常这一步骤使事业变得明显。 It also helps eliminate false leads. 它还有助于消除错误的线索。
2. Ensures that others can recreate your difficulty and thereby diagnose it. 2.确保其他人可以重建您的困难,从而诊断它。

Frequently, the exercise of preparing to explain a problem clearly to someone who is unfamiliar with your project causes the source of the problem to leap out. 通常,准备向不熟悉您的项目的人清楚地解释问题的行为会导致问题的根源跳出来。

For such OS / compiler dependent errors you should definitively provide us with more information on your platform, your compiler and your compiler flags. 对于此类OS /编译器相关的错误,您应该明确地向我们提供有关您的平台,编译器和编译器标志的更多信息。 It is not normal that your system has this function and hides it to you. 您的系统具有此功能并将其隐藏给您是不正常的。 You are probably missing some compiler flag. 您可能缺少一些编译器标志。

My manual says that getpgid is to be avoided if not necessary and to be replaced with the simpler POSIX function getpgrp(void) . 我的手册说如果没有必要,应该避免使用getpgid ,并用更简单的POSIX函数getpgrp(void)替换。 If this an option for you (you are just doing this for the id of the process itself) you should definitively do that. 如果这是你的一个选项(你只是为进程本身的id做这个)你应该明确地这样做。

如果需要其他标头,请参阅“getpgid”文档

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

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