DemonicImpact
DemonicImpact

Reputation: 317

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

Upvotes: 4

Views: 3266

Answers (4)

Jens Gustedt
Jens Gustedt

Reputation: 78993

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.

Upvotes: 2

Eric Towers
Eric Towers

Reputation: 4215

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.

Upvotes: 2

vanza
vanza

Reputation: 9903

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

Upvotes: 4

ariel
ariel

Reputation: 16140

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

Upvotes: 0

Related Questions