0

So... I suspect this is a result of my never having properly learned c++, but I'm trying to port an arduino project into c++ via platformio, and I'm having a bit of a head-scratch moment. Turning to the community for help =)

Say I have four files:

// fileone.cpp

#include "utilities.h"

boolean question_is_stupid = false;

int result = answer_the_question(); // should be 0
// filetwo.cpp
#include "utilities.h"

boolean question_is_stupid = true;

int result = answer_the_question(); // should be 1
// utilities.h
int answer_the_question();
// utilities.cpp
int answer_the_question() {
  if (question_is_stupid) return 1;
  else return 0;
}

How can I structure this so that answer_the_question() has access to the variable question_is_stupid in both of the cpp files?

Obviously in this simple example, I could just pass the variable, but I'm hoping to extrapolate the answer to a much more complex codebase. Essentially, I want to pull functions used by both fileone.cpp and filetwo.cpp into a separate utilities file, so that I don't have to write/edit/mess up the same function in two places. Some of my functions reference many (say, 10) global variables, so it'd be a pain (and confusing) to have to pass them all in. Is this just not done in cpp and I need to get over it? Or... is there a way?

  • 1
    Is [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) any help? – user4581301 Sep 23 '21 at 21:40
  • @user4581301 hmmm I had looked into extern but this seems like a much better explanation. I'll dig in. I did *try* to just add extern to all my variables and see what happened but it didn't compile, so I must not be understanding the concept. Thanks for the reference! – defenestrated Sep 23 '21 at 21:46
  • If you don't get something in the other question's answers, call it out here and someone will explain and show you how to adjust your code to make it work. – user4581301 Sep 23 '21 at 21:48

1 Answers1

0

Ok I'm answering my own question, thanks to @user4581301's comment.

The trick, if this helps any other relative beginners like me, is to:

  1. declare the extern variable in a header file (in my example, utilities.h).
  2. define the variable in the source file(s) as needed
  3. code with abandon. So my example becomes:
// utilities.h
extern boolean question_is_stupid; // DECLARING the variable here

int answer_the_question();
// utilities.cpp
int answer_the_question() {
  if (question_is_stupid) return 1;
  else return 0;
}
// fileone.cpp

#include "utilities.h"

boolean question_is_stupid = false; // DEFINING the variable here

int result = answer_the_question(); // should be 0
// filetwo.cpp
#include "utilities.h"

boolean question_is_stupid = true; // DEFINING the variable (again) here

int result = answer_the_question(); // should be 1
  • Don't define the variable more than once in files that will be linked together. That will break the [One Definition Rule](https://en.cppreference.com/w/cpp/language/definition). If fileone and filetwo are both in the same program, the linker should catch that you have the same variable twice and and slap you down with a linker error, but if it doesn't (buried deep in a dynamically linked library for example), you could be in for some *really* interesting debugging. – user4581301 Sep 23 '21 at 22:17
  • Thanks. In my case, `fileone` and `filetwo` are on two different microcontrollers, but I definitely appreciate the logic of not defining variables more than once in the same program! – defenestrated Sep 29 '21 at 16:58