Discussion:
<net/if.h> vs. <linux/if.h>
(too old to reply)
Roman Mashak
2006-03-30 06:22:17 UTC
Permalink
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: ***@tusur.ru
Ulrich Eckhardt
2006-03-30 08:15:37 UTC
Permalink
Post by Roman Mashak
What's the big difference of using </if.h> and <linux/if.h> ? Consider
#include <net/if.h>
[..]
Post by Roman Mashak
char buf[IFNAMSIZ];
struct ifreq ifr;
[..]
Post by Roman Mashak
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.
Post by Roman Mashak
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.
Post by Roman Mashak
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/
Roman Mashak
2006-03-30 10:40:49 UTC
Permalink
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: ***@tusur.ru
Igmar Palsenberg
2006-03-30 09:49:48 UTC
Permalink
Post by Roman Mashak
Hello, All!
What's the big difference of using </if.h> and <linux/if.h> ? Consider for
#include <net/if.h>
int main(void)
{
char buf[IFNAMSIZ];
struct ifreq ifr;
return 0;
}
#gcc -ansi -W -Wall ifr1.c
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
Roman Mashak
2006-03-30 11:23:36 UTC
Permalink
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: ***@tusur.ru
Bjorn Reese
2006-03-30 19:00:51 UTC
Permalink
Post by Roman Mashak
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
Continue reading on narkive:
Loading...