1

Alright, I understand how the extern definition works but I don't know what would be the "best" place to put them. Consider the following file structure:

  • main.c / main.h / global.h
  • drv_adc.c / drv_adc.h
  • drv_pwm.c / drv_pwm.h

You might guess, this is quite common for a small microcontroller. The two drivers work on different parts of the hardware and have no interdependencies. Both drivers are able to set a flag (say: adc_irq_occured, pwm_irq_occured) which indicates something has happened and which will be handled in the main.c.

Now I can think of two approaches where I would put the "extern bool adc_irq_occured;" flag.

  • The drv_adc.h: It somewhat belongs to the ADC driver, therefore I could add it to its header file and instantiate it in the main.c.
  • I turn the logic around and place the extern declaration into my main.h (or global.h if it has to be so) and instantiate it in my drv_adc.c.

Now the question: Which option is the preferred option here? Is there any good book where I could read about such topics?

Tom L.
  • 932
  • 2
  • 9
  • 30

1 Answers1

1
In main.c:
int flag = 0;

In main.h:  
extern int myGlobal;


In drv_adc.c: 
#include "main.h"

In drv_pwm.c:
#include "main.h"

Now that the variable is global , it is less secure , so use it with caution , and make sure any other drv files won't tamper with it.

-- EDIT -- Why not the other way round ?

We put the extern declaration in the header which is to be included by your other files. This is because we declare it once , telling the compiler that only one common version of the flag variable is available to both the drv files , which we included in the header , for more clarity do read this discussion , Difference between putting variables in header vs putting variables in source .

Community
  • 1
  • 1
Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
  • Why not do it the other way round? – Tom L. Mar 08 '13 at 14:45
  • @TomL. , have edited my post to remove the other ridiculous suggestion i gave you , and also have included the answer to your comment – Barath Ravikumar Mar 08 '13 at 15:52
  • Yes, that is one point of the story. Yet, imagine the following: The drivers are independent from each other, they need not know of each other. I only would like to let "main.c" know of them both, but not of them each other. So wouldn't the other way make more sense (in that it limits the scope a little bit more?) – Tom L. Mar 08 '13 at 16:17
  • like it or not , once you are intentionally , sharing a global variable between two files , they can and never will never be independent , even though it appears that you "limit the scope" , declaring as extern simply means , the variable will come from the other file during linking (hence scope does not matter here) – Barath Ravikumar Mar 08 '13 at 16:19
  • One more thought: If I put the extern declaration in the drv_adc.h and main.h includes the drv_adc.h, then it won't be visible to drv_pwm.c, thus limiting visibility - or do I have this wrong? – Tom L. Mar 09 '13 at 09:27
  • You can figure it out yourself if you know that #include , is nothing but copy , pasting header.h , so yeah that's true , but it goes against your question of sharing a variable between pwm and adc – Barath Ravikumar Mar 09 '13 at 10:47
  • Where did I say that? I always stated that the two are independent from each other. I want to share a variable between adc and main and another one between pwm and main (so main should know everything, but the other two need not know of each other). (I think I didn't make that point clear enough :( ) – Tom L. Mar 09 '13 at 14:18
  • "Both drivers are able to set a flag" , this strongly suggests that you are sharing a variable "flag" , between the two drivers – Barath Ravikumar Mar 09 '13 at 14:33