LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-28-2006, 04:39 AM   #1
diodio
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Rep: Reputation: 0
how to get rid of "warning : implicit declaration of function 'getpid' " ?


I am new to C .

I compile a C program on a SUNFIRE machine, with gcc 2.95.2 and I get the warning.
My code is :
===================================
#include <stdio.h>;
int main(int arg, char *argv[]){
printf("my PID is %d\n", getpid());
return 0;
}
===================================

and I get :
"warning : implicit declaration of function 'getpid' "

I guess I didn't include some kind of library.
Can some1 help ?
 
Old 01-28-2006, 05:07 AM   #2
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 32
Hi and Welcome to LQ!

The
Quote:
"warning : implicit declaration of function 'getpid' "
error is because getpid() is not declared before its use. This is because you have not included the header files in which it is declared. Here's a snippet of what man getpid has to say :
Code:
SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       pid_t getpid(void);
       pid_t getppid(void);

DESCRIPTION
       getpid returns the process ID of the current process.  (This is often used
       by routines that generate unique temporary file names.)

       getppid returns the process ID of the parent of the current process.
So inlude those header files and recompile your program.
Hope this helps.

Last edited by koodoo; 01-28-2006 at 05:10 AM.
 
Old 01-31-2006, 02:31 PM   #3
diodio
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Original Poster
Rep: Reputation: 0
It's different on my UNIX on Sunfire . When I try man fork I get this :
=======================================================================
Fortran Library Routines FORK(3F)

NAME
fork - create a copy of this process

SYNOPSIS
integer*4 function fork()

DESCRIPTION
The fork function creates a copy of the calling process.
The only distinction between the two processes is that the
value returned to one of them, referred to as the 'parent'
process, is the process id of the copy. The copy is usually
referred to as the `child' process. The value returned to
the `child' process is zero.

All logical units open for writing are flushed before the
fork to avoid duplication of the contents of I/O buffers in
the external files.

If the returned value is negative, it indicates an error and
is the negation of the system error code. See perror(3F).

A corresponding exec routine has not been provided because
there is no satisfactory way to retain open logical units
across the exec. However, the usual function of fork/exec
can be performed using system(3F).

FILES
libfui.a

SEE ALSO
fork(2), wait(3F), kill(3F), system(3F), perror(3F)

SunOS 5.8 Last change: 03/02/25
=======================================================================
 
Old 01-31-2006, 02:32 PM   #4
diodio
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Original Poster
Rep: Reputation: 0
So dear, what is the neccessary lib to include to get rid of the warning ?
 
Old 01-31-2006, 04:31 PM   #5
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 32
Quote:
It's different on my UNIX on Sunfire . When I try man fork I get this :
It's got to be different. What you've posted is the output of man fork while what I said was man getpid, read my post.

Why are you worrying about fork. As far as the error is concerned, its getpid that's causing it. It's not even an error, but a warning, and your program should still execute. On my P.C. I just copied and pasted your code and it ran fine (except for that semi-colon after #include <stdio.h> ;)

Quote:
So dear, what is the neccessary lib to include to get rid of the warning ?
You have all the libraries, its just that the declaration is missing.

As far as getting rid of the warning is concerned, just include the header files sys/types.h and unistd.h and it should work fine.

Hope this helps!

Last edited by koodoo; 01-31-2006 at 04:33 PM.
 
Old 02-01-2006, 06:28 AM   #6
diodio
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Koodoo, I tried sys/types.h and unistd.h ,it works.
 
Old 02-01-2006, 03:47 PM   #7
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 32
Smile

My pleasure
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
warning: implicit declaration of function liguorir Linux - Software 5 10-22-2012 04:20 PM
warning: incompatible implicit declaration of built-in function ‘exit’ xzotech Programming 1 08-14-2005 06:43 AM
implicit declaration of function 'getdelim' MicahCarrick Programming 2 05-02-2005 12:03 PM
When "function pointer" meets "template"... I can't get rid of this compiling er cyu021 Linux - Software 3 12-17-2004 08:52 PM
warning "conflicting types for buit-in function" while cross-compiling cheema Programming 0 07-07-2004 04:02 AM


All times are GMT -5. The time now is 12:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration