Unix Programming - compiling a program which uses the function gethostid

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2007 > compiling a program which uses the function gethostid





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author compiling a program which uses the function gethostid
kris

2007-06-22, 7:23 am

Hi I have written a program which prints the hostid on a linux
system.
The programm uses gethostid() function call which is defined in the
library file unistd.h. But my programm gets compiled without any
warnings even if I didnot include any of the header files.


can I know how does this happen i.e how does the compiler identifies
this function gethostid.
Is there any default path from where the compiler picks up the
definition from.


My application is as follows..


main()
{


long id,hostid;


printf("%ld\n",gethostid());
id = gethostid();
printf("%lu\n",id);



}


output : (I obtain the correct hostid)

I hope I would get a reply soon.


Thanks,
Krish

Joachim Schmitz

2007-06-22, 7:23 am

"kris" <raghavakrishna.j@gmail.com> schrieb im Newsbeitrag
news:1182504234.052356.153550@x35g2000prf.googlegroups.com...
> Hi I have written a program which prints the hostid on a linux
> system.
> The programm uses gethostid() function call which is defined in the
> library file unistd.h. But my programm gets compiled without any
> warnings even if I didnot include any of the header files.
>
>
> can I know how does this happen i.e how does the compiler identifies
> this function gethostid.
> Is there any default path from where the compiler picks up the
> definition from.
>
>
> My application is as follows..
>
>
> main()
> {
>
>
> long id,hostid;
>
>
> printf("%ld\n",gethostid());
> id = gethostid();
> printf("%lu\n",id);
>
>
>
> }
>
>
> output : (I obtain the correct hostid)
>
> I hope I would get a reply soon.

Use "-Wall -ansi -pedantic -O2" to get the max warning level.
Using printf (or any varadic finction) wothout a prototy is causeing
Undefined Behavoir. In case of gethistid() an implicit int is assuned, on
linux int == long, so you're pretty safe here.

Bye, Jojo


Joachim Schmitz

2007-06-22, 7:23 am

"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> schrieb im Newsbeitrag
news:f5g4ql$19i$1@online.de...
> "kris" <raghavakrishna.j@gmail.com> schrieb im Newsbeitrag
> news:1182504234.052356.153550@x35g2000prf.googlegroups.com...
> Use "-Wall -ansi -pedantic -O2" to get the max warning level.
> Using printf (or any varadic finction) wothout a prototy is causeing
> Undefined Behavoir. In case of gethistid() an implicit int is assuned, on
> linux int == long, so you're pretty safe here.

Sorry for the typos, serious finger trouble... I ment to write:
Use "-Wall -ansi -pedantic -O2" to get the max warning level.
Using printf (or any varadic function) without a prototyp is causing
Undefined Behavoir. In case of gethostid() an implicit int is assumed, on
linux int == long, so you're pretty safe here.

Bye, Jojo


Spoon

2007-06-22, 7:23 am

kris wrote:
> Hi I have written a program which prints the hostid on a linux
> system.
> The programm uses gethostid() function call which is defined in the
> library file unistd.h. But my programm gets compiled without any
> warnings even if I didnot include any of the header files.
>
>
> can I know how does this happen i.e how does the compiler identifies
> this function gethostid.
> Is there any default path from where the compiler picks up the
> definition from.
>
>
> My application is as follows..
>
>
> main()
> {
>
>
> long id,hostid;
>
>
> printf("%ld\n",gethostid());
> id = gethostid();
> printf("%lu\n",id);
>
>
>
> }


Here's what GCC 3.4.4 thinks of your program:

$ cat foo.c
main()
{


long id,hostid;


printf("%ld\n",gethostid());
id = gethostid();
printf("%lu\n",id);



}

$ gcc -std=c89 -Wall -Wextra -pedantic foo.c
foo.c:2: warning: return type defaults to `int'
foo.c: In function `main':
foo.c:8: warning: implicit declaration of function `printf'
foo.c:8: warning: implicit declaration of function `gethostid'
foo.c:8: warning: long int format, int arg (arg 2)
foo.c:5: warning: unused variable `hostid'
foo.c:14: warning: control reaches end of non-void function


In fact, -std=c89 will "hide" the declaration for gethostid.

$ cat foo.c
#include <stdio.h>
#include <unistd.h>
int main(void)
{
long id;
id = gethostid();
printf("%ld\n", id);
return 0;
}

$ gcc -std=gnu89 -Wall -Wextra -pedantic foo.c
/* NO WARNING */
Spoon

2007-06-22, 7:23 am

Joachim Schmitz wrote:

> Using printf (or any variadic function) without a prototype is causing
> Undefined Behavior. In case of gethostid() an implicit int is assumed, on
> linux int == long, so you're pretty safe here.


This is /not/ true.

cf. the x86-64 ABI:
http://www.x86-64.org/documentation/abi.pdf

sizeof(int) = 4
sizeof(long) = 8

These days, one should never assume sizeof(int) == sizeof(long)
Joachim Schmitz

2007-06-22, 7:23 am

"Spoon" <root@localhost> schrieb im Newsbeitrag
news:467ba190$0$23885$426a34cc@news.free.fr...
> Joachim Schmitz wrote:
>
>
> This is /not/ true.
>
> cf. the x86-64 ABI:

darn, forgot about the 64-bit Intels...

Bye, Jojo


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com