<net/if.h> vs. <linux/if.h>

  • Follow


Hello, All!

What's the big difference of using </if.h> and <linux/if.h> ? Consider for 
instance simple code:

#include <net/if.h>
int main(void)
{
    char buf[IFNAMSIZ];
    struct ifreq ifr;

    return 0;
}

#gcc -ansi -W -Wall ifr1.c
ifr1.c: In function `main':
ifr1.c:13: `IFNAMSIZ' undeclared (first use in this function)
ifr1.c:13: (Each undeclared identifier is reported only once
ifr1.c:13: for each function it appears in.)
ifr1.c:14: storage size of `ifr' isn't known
ifr1.c:13: warning: unused variable `buf'
ifr1.c:14: warning: unused variable `ifr'

"Storage size isn't known" means that particular structure was found 
undefined in the scope, on the other hand "gcc -E .." produced quite valid 
output.
Upon changing included header to <linux/if.h> compilation succeded. What's 
so magical in it?

Yes, I'm using RedHat ang glibc.

Thanks in advance.

With best regards, Roman Mashak.  E-mail: mrv@tusur.ru 


0
Reply Roman 3/30/2006 6:22:17 AM

Roman Mashak wrote:
> What's the big difference of using </if.h> and <linux/if.h> ? Consider
> for instance simple code:
> 
> #include <net/if.h>
[..]
>     char buf[IFNAMSIZ];
>     struct ifreq ifr;
[..]
> ifr1.c:13: `IFNAMSIZ' undeclared (first use in this function)
> ifr1.c:13: (Each undeclared identifier is reported only once
> ifr1.c:13: for each function it appears in.)

Constant was not defined.

> ifr1.c:14: storage size of `ifr' isn't known
> ifr1.c:13: warning: unused variable `buf'
> ifr1.c:14: warning: unused variable `ifr'

struct ifreq was declared but not defined.

> Upon changing included header to <linux/if.h> compilation succeded.
> What's so magical in it?

<linux/...> as the name suggests contains stuff that is specific for that
particular kernel. As a general guideline, you shouldn't use that stuff in
userspace programs, at most in programs closely related to the kernel and
programs willing to adapt to every interface change of these private
parts. 

Also, the code of course becomes non portable because it depends on Linux
internals/specifics, while the interface in <net/...> is probably more
widely portable.

If you told us here what you're trying to do, people might be able to offer
proper alternatives.

Uli

-- 
http://www.erlenstar.demon.co.uk/unix/
0
Reply Ulrich 3/30/2006 8:15:37 AM


Roman Mashak wrote:
> Hello, All!
> 
> What's the big difference of using </if.h> and <linux/if.h> ? Consider for 
> instance simple code:
> 
> #include <net/if.h>
> int main(void)
> {
>     char buf[IFNAMSIZ];
>     struct ifreq ifr;
> 
>     return 0;
> }
> 
> #gcc -ansi -W -Wall ifr1.c
> ifr1.c: In function `main':
> ifr1.c:13: `IFNAMSIZ' undeclared (first use in this function)
> ifr1.c:13: (Each undeclared identifier is reported only once
> ifr1.c:13: for each function it appears in.)
> ifr1.c:14: storage size of `ifr' isn't known
> ifr1.c:13: warning: unused variable `buf'
> ifr1.c:14: warning: unused variable `ifr'

Your include files aren't what they should be.


	Igmar
0
Reply Igmar 3/30/2006 9:49:48 AM

Hello, Ulrich!
You wrote  on Thu, 30 Mar 2006 10:15:37 +0200:

 ??>> ifr1.c:13: `IFNAMSIZ' undeclared (first use in this function)
 ??>> ifr1.c:13: (Each undeclared identifier is reported only once
 ??>> ifr1.c:13: for each function it appears in.)

 UE> Constant was not defined.
It was defined in net/if.h
 ??>> ifr1.c:14: storage size of `ifr' isn't known
 ??>> ifr1.c:13: warning: unused variable `buf'
 ??>> ifr1.c:14: warning: unused variable `ifr'

 UE> struct ifreq was declared but not defined.
This struct was declared in net/if.h and I defined 'struct ifreq ifr' in my 
code.
 ??>> Upon changing included header to <linux/if.h> compilation succeded.
 ??>> What's so magical in it?

 UE> <linux/...> as the name suggests contains stuff that is specific for
 UE> that particular kernel. As a general guideline, you shouldn't use that
 UE> stuff in userspace programs, at most in programs closely related to the
I understand this and want to avoid, that's why I posted here. What's so 
unusual in my simple code (where I only made definition) that causes 
warnings?

With best regards, Roman Mashak.  E-mail: mrv@tusur.ru 


0
Reply Roman 3/30/2006 10:40:49 AM

Hello, Igmar!
You wrote  on Thu, 30 Mar 2006 11:49:48 +0200:

 ??>> undeclared (first use in this function) ifr1.c:13: (Each undeclared
 ??>> identifier is reported only once ifr1.c:13: for each function it
 ??>> appears in.) ifr1.c:14: storage size of `ifr' isn't known ifr1.c:13:
 ??>> warning: unused variable `buf' ifr1.c:14: warning: unused variable
 ??>> `ifr'

 IP> Your include files aren't what they should be.
Doubt strongly... The probable cause of warnings is '-ansi' option of 
compiler which doesn't suppose any extensions such as sockets.

With best regards, Roman Mashak.  E-mail: mrv@tusur.ru 


0
Reply Roman 3/30/2006 11:23:36 AM

Roman Mashak wrote:

> Doubt strongly... The probable cause of warnings is '-ansi' option of 
> compiler which doesn't suppose any extensions such as sockets.

Yes. In this case you need to explicitly tell the compiler which
extensions you want to use. See the "Feature Test Macros" in the
glibc man page (info libc). You probably need _BSD_SOURCE or
_SVID_SOURCE.

-- 
mail1dotstofanetdotdk
0
Reply Bjorn 3/30/2006 7:00:51 PM

5 Replies
550 Views

(page loaded in 8.454 seconds)

Similiar Articles:













7/26/2012 1:55:23 PM


Reply: