简体   繁体   中英

Implicit Declaration of Function in C UNIX

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) {
      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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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