All of lore.kernel.org
 help / color / mirror / Atom feed
* what is the use of #ifndefs
@ 2015-07-20 10:03 Ahmed Soliman
  2015-07-20 15:55 ` leo kirotawa
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Ahmed Soliman @ 2015-07-20 10:03 UTC (permalink / raw)
  To: kernelnewbies

currently I started reading through the linux kernel and I started
reading liunx/include/linux/list.h> I understood some of the functions
but still I dont know what does these lines of code do
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
which exist at the very beginning of the file
I also noticed that there is many similar ifndefs in almost any .h
file in the kernel
note that I understand wnat does ifndef do bu I dont understand what
goal is it supposed to achieve at the beginning of the headerfile

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-20 10:03 what is the use of #ifndefs Ahmed Soliman
@ 2015-07-20 15:55 ` leo kirotawa
  2015-07-20 16:03   ` Stephan Müller
  2015-07-20 16:11 ` Greg Freemyer
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: leo kirotawa @ 2015-07-20 15:55 UTC (permalink / raw)
  To: kernelnewbies

it means you don't want to redefine a .h file

On Mon, Jul 20, 2015 at 7:03 AM, Ahmed Soliman
<ahmedsoliman0x666@gmail.com> wrote:
> currently I started reading through the linux kernel and I started
> reading liunx/include/linux/list.h> I understood some of the functions
> but still I dont know what does these lines of code do
> #ifndef _LINUX_LIST_H
> #define _LINUX_LIST_H
> which exist at the very beginning of the file
> I also noticed that there is many similar ifndefs in almost any .h
> file in the kernel
> note that I understand wnat does ifndef do bu I dont understand what
> goal is it supposed to achieve at the beginning of the headerfile
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



-- 

----------------------------------------------
Le?nidas S. Barbosa (Kirotawa)
blog: corecode.wordpress.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-20 15:55 ` leo kirotawa
@ 2015-07-20 16:03   ` Stephan Müller
  0 siblings, 0 replies; 11+ messages in thread
From: Stephan Müller @ 2015-07-20 16:03 UTC (permalink / raw)
  To: kernelnewbies

The keyword is 'include guard'

 ~frukto

Am 20.07.2015 um 17:55 schrieb leo kirotawa:
> it means you don't want to redefine a .h file
> 
> On Mon, Jul 20, 2015 at 7:03 AM, Ahmed Soliman
> <ahmedsoliman0x666@gmail.com> wrote:
>> currently I started reading through the linux kernel and I started
>> reading liunx/include/linux/list.h> I understood some of the functions
>> but still I dont know what does these lines of code do
>> #ifndef _LINUX_LIST_H
>> #define _LINUX_LIST_H
>> which exist at the very beginning of the file
>> I also noticed that there is many similar ifndefs in almost any .h
>> file in the kernel
>> note that I understand wnat does ifndef do bu I dont understand what
>> goal is it supposed to achieve at the beginning of the headerfile
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> Kernelnewbies at kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> 
> 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-20 10:03 what is the use of #ifndefs Ahmed Soliman
  2015-07-20 15:55 ` leo kirotawa
@ 2015-07-20 16:11 ` Greg Freemyer
  2015-07-20 17:00 ` anish singh
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Greg Freemyer @ 2015-07-20 16:11 UTC (permalink / raw)
  To: kernelnewbies

Ahmed,

That's basic C syntax for 30 years both in and out of the kernel.

If you include the same header file multiple times you can get errors
about defining the same structures, constants, globals multiple times.

So the first time it is included you want the header file to actually
be included.

For all subsequent times you want it ignored by the compiler.

And that is exactly what

#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H

causes to happen.

Greg
--
Greg Freemyer
www.IntelligentAvatar.net


On Mon, Jul 20, 2015 at 6:03 AM, Ahmed Soliman
<ahmedsoliman0x666@gmail.com> wrote:
> currently I started reading through the linux kernel and I started
> reading liunx/include/linux/list.h> I understood some of the functions
> but still I dont know what does these lines of code do
> #ifndef _LINUX_LIST_H
> #define _LINUX_LIST_H
> which exist at the very beginning of the file
> I also noticed that there is many similar ifndefs in almost any .h
> file in the kernel
> note that I understand wnat does ifndef do bu I dont understand what
> goal is it supposed to achieve at the beginning of the headerfile
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-20 10:03 what is the use of #ifndefs Ahmed Soliman
  2015-07-20 15:55 ` leo kirotawa
  2015-07-20 16:11 ` Greg Freemyer
@ 2015-07-20 17:00 ` anish singh
  2015-07-21  2:24 ` Navy
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: anish singh @ 2015-07-20 17:00 UTC (permalink / raw)
  To: kernelnewbies

On Mon, Jul 20, 2015 at 3:03 AM, Ahmed Soliman <ahmedsoliman0x666@gmail.com>
wrote:

> currently I started reading through the linux kernel and I started
> reading liunx/include/linux/list.h> I understood some of the functions
> but still I dont know what does these lines of code do
> #ifndef _LINUX_LIST_H
> #define _LINUX_LIST_H
> which exist at the very beginning of the file
> I also noticed that there is many similar ifndefs in almost any .h
> file in the kernel
> note that I understand wnat does ifndef do bu I dont understand what
> goal is it supposed to achieve at the beginning of the headerfile
>

It makes sure that the file doesn't get included twice.Suppose
you have a.c and a.h file and  you have #include "thisfile"(where
you have that #ifndef thing) in both a.c and a.h file then it will
be included only once. Because once .c or .h includes it then
that particular #define is already defined so next time it will
not execute when someone tries to include it again.

Hope that helps.

>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150720/e8258c98/attachment.html 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-20 10:03 what is the use of #ifndefs Ahmed Soliman
                   ` (2 preceding siblings ...)
  2015-07-20 17:00 ` anish singh
@ 2015-07-21  2:24 ` Navy
  2015-07-21  5:05   ` Raul Piper
  2015-07-21  5:34 ` Amit Pandey
  2015-07-21 19:44 ` Aruna Hewapathirane
  5 siblings, 1 reply; 11+ messages in thread
From: Navy @ 2015-07-21  2:24 UTC (permalink / raw)
  To: kernelnewbies

On Mon, Jul 20, 2015 at 12:03:07PM +0200, Ahmed Soliman wrote:
> currently I started reading through the linux kernel and I started
> reading liunx/include/linux/list.h> I understood some of the functions
> but still I dont know what does these lines of code do
> #ifndef _LINUX_LIST_H
> #define _LINUX_LIST_H
> which exist at the very beginning of the file
> I also noticed that there is many similar ifndefs in almost any .h
> file in the kernel
> note that I understand wnat does ifndef do bu I dont understand what
> goal is it supposed to achieve at the beginning of the headerfile
> 
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

The whole structure seems to 
#ifndef ...
#define ...
.
.
.
#endif

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-21  2:24 ` Navy
@ 2015-07-21  5:05   ` Raul Piper
  0 siblings, 0 replies; 11+ messages in thread
From: Raul Piper @ 2015-07-21  5:05 UTC (permalink / raw)
  To: kernelnewbies

This is to avoid multiple declarations.In header file you declare the
variables and function names and these have to be declared just once
otherwise you will get multiple declaration error.
#ifndef avoids this error .By placing it at the top all the declaration is
visited just once =>How?Since suppose  _LINUX_LIST_H is not defined and
when you enter the file #ifndef (if not defined) will be true , hence it
will go to next line and define it using  #define _LINUX_LIST_H .Now Since
#defines(Macros) have global scopes across all the files, if suppose in
some other files you have included this file (which defines above macros) ,
it will check first #ifndef _LINUX_LIST_H , but since it has already been
defiend it will not enter the next line and you will be saved from multiple
declaration error.

On Tue, Jul 21, 2015 at 7:54 AM, Navy <navych@126.com> wrote:

> On Mon, Jul 20, 2015 at 12:03:07PM +0200, Ahmed Soliman wrote:
> > currently I started reading through the linux kernel and I started
> > reading liunx/include/linux/list.h> I understood some of the functions
> > but still I dont know what does these lines of code do
> > #ifndef _LINUX_LIST_H
> > #define _LINUX_LIST_H
> > which exist at the very beginning of the file
> > I also noticed that there is many similar ifndefs in almost any .h
> > file in the kernel
> > note that I understand wnat does ifndef do bu I dont understand what
> > goal is it supposed to achieve at the beginning of the headerfile
> >
> > _______________________________________________
> > Kernelnewbies mailing list
> > Kernelnewbies at kernelnewbies.org
> > http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
> The whole structure seems to
> #ifndef ...
> #define ...
> .
> .
> .
> #endif
>
>
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150721/78f31450/attachment-0001.html 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-20 10:03 what is the use of #ifndefs Ahmed Soliman
                   ` (3 preceding siblings ...)
  2015-07-21  2:24 ` Navy
@ 2015-07-21  5:34 ` Amit Pandey
  2015-07-21  7:01   ` Navy
  2015-07-21  8:33   ` Bernd Petrovitsch
  2015-07-21 19:44 ` Aruna Hewapathirane
  5 siblings, 2 replies; 11+ messages in thread
From: Amit Pandey @ 2015-07-21  5:34 UTC (permalink / raw)
  To: kernelnewbies

Hi Ahmed,

See the comments inline
#ifndef _LINUX_LIST_H      // If not defined _LINUX_LIST_H macro
#define _LINUX_LIST_H     // then define this macro
#include "linuxlist.h"             // and include linuxlist.h header file
#endif                                   //  end of #ifndef

Now say in another file if u r not sure whether you have already included
the "linuxlist.h" then you will again repeat above lines of code, assuming
you have included it then certainly _LINUX_LIST_H macro has already been
defined, so compiler will not include this file again. See comments below

#ifndef _LINUX_LIST_H      // Since macro _LINUX_LIST_H  has already defined
#define _LINUX_LIST_H     // compiler will ignore this line
#include "linuxlist.h"             // compiler will ignore this line, too.
Hence no multiple inclusion of same header file
#endif

Please let me know whether I was clear with the explanation.

Thanks,
Amit

On Mon, Jul 20, 2015 at 3:33 PM, Ahmed Soliman <ahmedsoliman0x666@gmail.com>
wrote:

> currently I started reading through the linux kernel and I started
> reading liunx/include/linux/list.h> I understood some of the functions
> but still I dont know what does these lines of code do
> #ifndef _LINUX_LIST_H
> #define _LINUX_LIST_H
> which exist at the very beginning of the file
> I also noticed that there is many similar ifndefs in almost any .h
> file in the kernel
> note that I understand wnat does ifndef do bu I dont understand what
> goal is it supposed to achieve at the beginning of the headerfile
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150721/7b870722/attachment.html 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-21  5:34 ` Amit Pandey
@ 2015-07-21  7:01   ` Navy
  2015-07-21  8:33   ` Bernd Petrovitsch
  1 sibling, 0 replies; 11+ messages in thread
From: Navy @ 2015-07-21  7:01 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Jul 21, 2015 at 11:04:15AM +0530, Amit Pandey wrote:
> Hi Ahmed,
> 
> See the comments inline
> #ifndef _LINUX_LIST_H      // If not defined _LINUX_LIST_H macro
> #define _LINUX_LIST_H     // then define this macro
> #include "linuxlist.h"             // and include linuxlist.h header file
> #endif                                   //  end of #ifndef
> 
> Now say in another file if u r not sure whether you have already included
> the "linuxlist.h" then you will again repeat above lines of code, assuming
> you have included it then certainly _LINUX_LIST_H macro has already been
> defined, so compiler will not include this file again. See comments below
> 
> #ifndef _LINUX_LIST_H      // Since macro _LINUX_LIST_H  has already defined
> #define _LINUX_LIST_H     // compiler will ignore this line
> #include "linuxlist.h"             // compiler will ignore this line, too.
> Hence no multiple inclusion of same header file
> #endif
> 
> Please let me know whether I was clear with the explanation.
> 
> Thanks,
> Amit
> 
> On Mon, Jul 20, 2015 at 3:33 PM, Ahmed Soliman <ahmedsoliman0x666@gmail.com>
> wrote:
> 
> > currently I started reading through the linux kernel and I started
> > reading liunx/include/linux/list.h> I understood some of the functions
> > but still I dont know what does these lines of code do
> > #ifndef _LINUX_LIST_H
> > #define _LINUX_LIST_H
> > which exist at the very beginning of the file
> > I also noticed that there is many similar ifndefs in almost any .h
> > file in the kernel
> > note that I understand wnat does ifndef do bu I dont understand what
> > goal is it supposed to achieve at the beginning of the headerfile
> >
> > _______________________________________________
> > Kernelnewbies mailing list
> > Kernelnewbies at kernelnewbies.org
> > http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> >

> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Your explanation seems to be wrong.
The code below should in "linuxlist.h" ,other than in other files which include "linuxlist.h".
#ifndef _LINUX_LIST_H    
#define _LINUX_LIST_H     
...
#endif

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-21  5:34 ` Amit Pandey
  2015-07-21  7:01   ` Navy
@ 2015-07-21  8:33   ` Bernd Petrovitsch
  1 sibling, 0 replies; 11+ messages in thread
From: Bernd Petrovitsch @ 2015-07-21  8:33 UTC (permalink / raw)
  To: kernelnewbies

Hi all!


On Die, 2015-07-21 at 11:04 +0530, Amit Pandey wrote:
[... crap deleted ...]
> Please let me know whether I was clear with the explanation.

It was clear and it is total and absolute crap:
- first, check with the .h (and .c) files in the kernel (and all others
  which get it right), that it is *not* as described above.
- second, the above approach may work (if done right) but has a some
  severe drawbacks and disadvantages:
  * you have to duplicate (lots of times!) the 3 lines ("#ifndef ...",
    "#define ...", "#endif" to all files (.c and .h) where on actually
    #includes the .h file.
  * imagine a .c file with 10 #include - you get 30 additional lines.
    And 10 is probably not the a large number for this.
  * you rely that in all places people use the very same #define macro
    for the same .h file - which is way to error-prone to use the
    pattern.

For non-crap solution: just look into one .h file in the kernel (or read
other answers in the thread) - no duplication etc. .....

BTW that is nothing that the Linux kernel created but everyone uses that
in the C/C++ work since ages .....

Kind regards,
	Bernd
-- 
"I dislike type abstraction if it has no real reason. And saving
on typing is not a good reason - if your typing speed is the main
issue when you're coding, you're doing something seriously wrong."
    - Linus Torvalds

^ permalink raw reply	[flat|nested] 11+ messages in thread

* what is the use of #ifndefs
  2015-07-20 10:03 what is the use of #ifndefs Ahmed Soliman
                   ` (4 preceding siblings ...)
  2015-07-21  5:34 ` Amit Pandey
@ 2015-07-21 19:44 ` Aruna Hewapathirane
  5 siblings, 0 replies; 11+ messages in thread
From: Aruna Hewapathirane @ 2015-07-21 19:44 UTC (permalink / raw)
  To: kernelnewbies

<snip>
>> I dont understand what goal is it supposed to achieve
>> at the beginning of the headerfile
<snip>

The 'goal' is to prevent 'redefinition' errors, like Stephan M?ller said it
is a 'include guard'.

A explanation of why and how to use include guards can be found here:
https://en.wikipedia.org/wiki/Include_guard

When I try : find . -name "*.c" -o -name "*.h" | xargs grep -i "include
<linux/list.h>" | wc -l

It gives me 1319 instances where list.h is being included, imagine your
function being redefined that many times. Not only will gcc bitch and
complain about redefinition errors but go screaming into the night like
Valdis says.. [grin]

If you still have difficulty understanding why, the best way would be to
write some simple code yourself then compile, you will start to see why we
require the #ifndef.

Thanks - Aruna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150721/dffcd221/attachment.html 

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2015-07-21 19:44 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-20 10:03 what is the use of #ifndefs Ahmed Soliman
2015-07-20 15:55 ` leo kirotawa
2015-07-20 16:03   ` Stephan Müller
2015-07-20 16:11 ` Greg Freemyer
2015-07-20 17:00 ` anish singh
2015-07-21  2:24 ` Navy
2015-07-21  5:05   ` Raul Piper
2015-07-21  5:34 ` Amit Pandey
2015-07-21  7:01   ` Navy
2015-07-21  8:33   ` Bernd Petrovitsch
2015-07-21 19:44 ` Aruna Hewapathirane

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.