0

Hi this is probably a stupid question but I don't know how to solve this after looking online. In a file which we will call filea.h under project/a/aa/filea.h I have a variable declared (global) bool is_testing = false;.

In another file called fileb.c under project/b/bb/fileb.c I have the following:

#include <a/aa/filea.h>
...
int ex_func(void)
{
  is_testing = true;
}

I am getting this error when I try to compile: filea.h:19: first defined here. Does anyone know how to fix this?

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
innyme
  • 69
  • 5
  • It is reporting an error in `filea.h`, but you chose to neither show the full error message to us nor this file... – Eugene Sh. Aug 24 '20 at 16:55
  • Use the `extern` keyword. You may want to take a look at [How do I use extern to share variables between source files?](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files) – sebastian Aug 24 '20 at 16:55
  • Does this answer your question: [What is the difference between a definition and a declaration?](/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration) – n. 'pronouns' m. Aug 24 '20 at 16:56

1 Answers1

1

This declaration in the header

bool is_testing = false;

is also a definition of the variable. So the variable will defined as many times as the header will be included in different translation units.

Place in the header

extern bool is_testing;

and in some module write

bool is_testing = false;

for example where the function is defined..

user3386109
  • 32,020
  • 7
  • 41
  • 62
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268