Re: what is the use of #ifndefs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxx> 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@xxxxxxxxxxxxxxxxx
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

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




_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux