chipKIT® Development Platform

Inspired by Arduino™

#Include duplicate

Created Fri, 12 Feb 2016 03:38:39 +0000 by FredCailloux


FredCailloux

Fri, 12 Feb 2016 03:38:39 +0000

We often see preprocessor directive

#if !defined BlaBla
	  define  BlaBla
#endif

But what if I must include in my Class definition a #Include directive and this inclusion is already defined inside another header of cpp code that include the very file that I am including in my definition. Is there not directive like #if !Included ? Just to garanty that the same file is not going to be included twice ? Something like:

#if !included BlaBla
	  include  BlaBla
#endif

Is there a way to do that or an equivalent ? Also, What would be the consequence of including twice the same file ?


majenko

Fri, 12 Feb 2016 09:04:58 +0000

No, there is nothing like that. However, that is the role of the bits like this in headers:

#ifndef _BLAH_H
#define _BLAH_H
... header contents here
#endif

That means that if a header is included twice the content is only actually there once since the second time it is included _BLAH_H has been defined so the #ifndef will fail.

When you do an include the preprocessor literally replaces that include directive with the contents of the included file, so no matter how deep your inclusions go you still end up with them inline in the same file.

Sent from my SM-T555 using Tapatalk