static const char from Clib

Hello,

I'd like to use the esp_log library (
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/log.html) in my cpp program. But it requires the definition of a TAG variable to be different in each of the source files.
Here is the excerpt:


How to use this library

In each C file that uses logging functionality, define the TAG variable as shown below:

static const char* TAG = "MyModule";
"

I found a similar question (https://stackoverflow.com/questions/177437/what-does-const-static-mean-in-c-and-c):
Answer:
It has uses in both C and C++.

As you guessed, the static part limits its scope to that compilation unit. It also provides for static initialization. const just tells the compiler to not let anybody modify it. This variable is either put in the data or bss segment depending on the architecture, and might be in memory marked read-only.

All that is how C treats these variables (or how C++ treats namespace variables). In C++, a member marked static is shared by all instances of a given class. Whether it's private or not doesn't affect the fact that one variable is shared by multiple instances. Having const on there will warn you if any code would try to modify that.

If it was strictly private, then each instance of the class would get its own version (optimizer notwithstanding).


but I'm still not sure how to go about this problem.

thank you.
Last edited on
And the answers are:
a. Someone already ran into this same situation - http://www.cplusplus.com/forum/beginner/121441/ . I also (incorrectly) declared the variable in the .h that was included in multiple .cpp
b. i should stop programming after 23:00....
Last edited on
Topic archived. No new replies allowed.