-3

I have two c++ files. One (AudioSystem.cpp) located at ./framework/av/media/libmedia The other (AudioFlinger.cpp) locatet at ./framework/av/services/audioflinger

Both they share an header file

#include <system/audio.h>

I would like to create a var that is shared among the AudioFlinger.cpp and the AudioSystem.cpp.

They both should be able to modify the var value and they should see the same value.

Thanks

JBL
  • 12,588
  • 4
  • 53
  • 84
Giuseppe
  • 447
  • 2
  • 5
  • 14

1 Answers1

1

Global variable can be defined in any of your AudioFlinger.cpp or AudioSystem.cpp, but you should ensure that in other cpp file the statement declaring this variable as extern in placed e.g. with h-file and #include.

Also, you can make separate cpp-file with variable definition and appropriate h-file with extarn variable declaration, e.g.:

   // var.cpp
   int sharedVariable;

and

   // var.h
   extern int sharedVariable;

And now any cpp module, where #include "var.h" is, can use sharedVariable

VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • Following your suggestion I get: frameworks/av/media/libmedia/AudioSystem.cpp:736: error: undefined reference to 'sharedVariable' – Giuseppe Mar 30 '15 at 14:25
  • If you written `extern data_type var_name;` in `frameworks/av/media/libmedia/AudioSystem.cpp` you should compile this cpp file together with cpp file where `data_type var_name;` (global declaration means outside of a function) is – VolAnd Mar 30 '15 at 14:33