what is the use of #ifndefs
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 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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- ---------------------------------------------- Leônidas S. Barbosa (Kirotawa) blog: corecode.wordpress.com
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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
The whole structure seems to #ifndef ... #define ... . . . #endif
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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
The whole structure seems to #ifndef ... #define ... . . . #endif
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@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
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
<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
participants (10)
-
Ahmed Soliman -
Amit Pandey -
anish singh -
Aruna Hewapathirane -
Bernd Petrovitsch -
Greg Freemyer -
leo kirotawa -
Navy -
Raul Piper -
Stephan Müller