views:

117

answers:

4

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");
}
A: 

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

bortao
+3  A: 

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
vanza
What does this mean? Sorry, I am kind of new to programming... well, sort of.
DemonicImpact
That you have to define some macros for the prototype to be available to your code. Also see the referenced man page (feature_test_macros(7)).
vanza
Does this mean I need to include the following lines before my include statements?#define _XOPEN_SOURCE#define _XOPEN_SOURCE_EXTENDED
DemonicImpact
@DemonicImpact: `#define _XOPEN_SOURCE 500`
caf
+1  A: 

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.

Eric Towers
No, [`unistd.h`](http://opengroup.org/onlinepubs/007908799/xsh/unistd.h.html) is guaranteed to define `pid_t`, and he would get a different warning if it weren't. But the minimal failing case is good general advice.
Matthew Flaschen
You're right. I skimmed <a href="http://www.delorie.com/gnu/docs/glibc/libc_566.html">The GNU C Library: Process Identification</a> instead of diving into unistd.h.
Eric Towers
+1 for demonstrating what a minimal test case might look like. I can't emphasized enough how powerful that technique can be, and how important it is when attempting to report a bug.
RBerteig
+1  A: 

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.

Jens Gustedt
It stopped giving me an error when I did what the top answer suggested and added the test macro... However, I think I may use getpgrp instead maybe.
DemonicImpact
@DemonicImpact: sure, the solution that is indicated is somewhat a solution to circumvent the problem. "Naturally" your compiler should know what version of POSIX your system complies to, and do the right thing by itself.
Jens Gustedt