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");
}
DemonicImpact
  • 307
  • 2
  • 6
  • 12
  • What platform specifically is this on? And, is that the whole sample, or is your code within an actual function? – Joe Oct 19 '10 at 02:58
  • http://bugs.php.net/bug.php?id=32045 has some clues – Preet Sangha Oct 19 '10 at 02:59
  • Its code in an actual function, I didn't include the complete function since that was the only part that was causing problems and its on UNIX. – DemonicImpact Oct 19 '10 at 03:03
  • 1
    Your professor isn't alone. We treat warnings as errors in our build system. I is generally a good practice to get used to, especially if you don't understand the warning as is the case here. – Ed S. Oct 19 '10 at 03:13

4 Answers4

4

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
Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
vanza
  • 9,715
  • 2
  • 31
  • 34
2

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.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
Eric Towers
  • 4,175
  • 1
  • 15
  • 17
2

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
  • 76,821
  • 6
  • 102
  • 177
  • 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 Oct 19 '10 at 06:48
  • 1
    @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 Oct 19 '10 at 07:11
0

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

ariel
  • 15,620
  • 12
  • 61
  • 73