cancel
Showing results for 
Search instead for 
Did you mean: 

SImple question #ifndef __STM32F100_Dicovery_H

xtian
Associate II
Posted on January 18, 2013 at 02:11

what is this for

/**

  ******************************************************************************

  * @file    STM32vldiscovery.h

  * @author  MCD Application Team

  * @version V1.0.0

  * @date    09/13/2010

  * @brief   Header file for STM32vldiscovery.c module.

  ******************************************************************************

  * @copy

  *

  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS

  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE

  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY

  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING

  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE

  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

  *

  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>

  */

 

/* Define to prevent recursive inclusion -------------------------------------*/

 

&sharpifndef __STM32F100_Dicovery_H

 

&sharpdefine __STM32F100_Dicovery_H

//this one, is see this in every header... what is their function?

&sharpifdef __cplusplus

 extern ''C'' {

&sharpendif

/* Includes ------------------------------------------------------------------*/

&sharpinclude ''STM32f10x.h''

/** @addtogroup Utilities

  * @{

  */

 

/** @addtogroup STM32vldiscovery

#new-question-=-new-thread #include-guard #include-guard #include-guard #learning-c
11 REPLIES 11
Posted on January 18, 2013 at 02:38

C++ is intolerant of  redefinition, this prevents circular or repetitive #include's

ie you #include <stdio.h> other files you include also have a #include <stdio.h>, etc.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xtian
Associate II
Posted on January 18, 2013 at 02:43

so this means

#ifndef __STM32F100_Dicovery_H

 

#define __STM32F100_Dicovery_H

//if not defined

define this include as __STM32f100_dicovery_H and use it for the one implementing it...?

crt2
Associate II
Posted on January 18, 2013 at 10:39

In even more plain words:

It means that, if you have 3 files all calling same library, they will not include functions from that library 3 times, but will use linker to link functions to one existing compiled library source. This define tells compiler that if library was once compiled, it does not need to be recompiled again, but just linked to it. This also saves space as pointers are small compared to some large functions which can be in libraries.
Andrew Neil
Evangelist III
Posted on January 18, 2013 at 12:14

''if you have 3 files all calling same library''

 

No, that's wrong.

This has nothing to do with the header being included by multiple different  source files (or, more strictly, ''compilation units'')

What this prevents against is the file being included in the

same

  compilation unit multiple times.

It is a standard, widely-used practice in 'C' programming - commonly known as an ''Include Guard''.

Andrew Neil
Evangelist III
Posted on January 18, 2013 at 12:25

Example.

Say we have a .c file which includes this header 3 times:

# include STM32vldiscovery.h
 # include STM32vldiscovery.h
 # include
STM32vldiscovery.h

What the compiler will actually see is:

// From 1st #include: 
#ifndef __STM32F100_Dicovery_H// this is not defined - so the compiler ''sees'' the rest of the file 
#define __STM32F100_Dicovery_H // now we define it 
// the compiler ''sees'' the rest of the file... 
#endif 
// From 2nd #include: 
#ifndef __STM32F100_Dicovery_H // IS now defined - so compiler ignores the rest of the file 
#endif 
// From 3rd #include: 
#ifndef __STM32F100_Dicovery_H // IS still defined - so compiler again ignores rest of file 
#endif 

You wouldn't normally directly #include the same header like that - but, as already noted, it might happen as a result of one header file #including another...

http://en.wikipedia.org/wiki/Include_guard

Note that names with leading underscores should be reserved for use by the compiler - so, strictly, ST (like many others) are being naughty in their naming here.
Andrew Neil
Evangelist III
Posted on January 18, 2013 at 12:35

Just to emphasise that this is a standard  practice - nothing specifically to do with STM32, or ST, or embedded...

http://c-faq.com/cpp/nestincl.html

http://en.wikipedia.org/wiki/Include_guard

frankmeyer9
Associate II
Posted on January 18, 2013 at 12:54

Starting both with C and Cortex M3 at the same time seems to make a good recipe for disappointments.

One should at least master the ''hello world'' with a PC compiler ...

Andrew Neil
Evangelist III
Posted on January 18, 2013 at 13:13

''One should at least master the ''hello world'' with a PC compiler ...''

 

I (among others) think that is a very good suggestion!

Here's my list of some suggested resources for learning C:

http://bit.ly/X5Atml

crt2
Associate II
Posted on January 18, 2013 at 14:27

Ok, I must have stated poorly- your explanation is better.