开发者

Implicit Declaration of Function in C UNIX

开发者 https://www.devze.com 2023-01-20 13:41 出处:网络
In the following code, I get a warning that there is an implicit declaration of function getpgid. I know its only a warning, but its for a class and the professor wants us to treat warnings as errors.

In the following code, I get a warning that there is an implicit declaration of function 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) {
   开发者_StackOverflow社区   app_error("Failure to get process group ID");
}


From the man page:

Feature Test Macro Requirements for glibc (see 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. 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. 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.

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. 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). If this an option for you (you are just doing this for the id of the process itself) you should definitively do that.


See in the "getpgid" documentation if there's some other header needed

0

精彩评论

暂无评论...
验证码 换一张
取 消