Share object between files

Hi guys,
Can I share same object(Adafruit_SH1106 Screen(4)) between main.cpp and ui.cpp? How I can share it?
Declare, but do not define it in every file that requires the definition. Define it in exactly one file.

Generally, this is accomplished by placing the declaration in a header file:
ui.hpp
1
2
3
4
5
6
7
#if ! defined PROJECT_MY_HEADER_HPP_INCLUDED
#define PROJECT_MY_HEADER_HPP_INCLUDED
// ... etc.

// extern: "this symbol is defined somewhere else" 
extern Adafruit_SH1106 Screen;
#endif 


And the corresponding definition would be placed in exactly one source file:
ui.cpp
1
2
#include "ui.hpp"
Adafruit_SH1106 Screen(4);

You cannot simply define the variable in multiple source files (or, equivalently, in a header file included in multiple source files) because this would violate the One Definition Rule.

Later, anyone who wants to use Screen should include ui.hpp and proceed.

See:
https://stackoverflow.com/questions/9702053/how-to-declare-a-global-variable-in-c
Last edited on
Thanks for help. I need answer for:
- If I share an object on way above, can I use that object in another object?
- When I declare the function in class "ui", function(and later whole object) will go on stack memory. If I define the object in setup() function, and after setup() function is done, does object stays in memory or it goes out?

I'm trying to optimize RAM usage. In setup() function(called when chip starts) I have to use a lot of functions(those functions I'll never use again). So is it better idea to create the object inside setup(function) that will hold all functions?

If I define those functions in main.cpp at global scope, they'll be in memory forever. I don't want that.
Last edited on
I did little test and compiler doesn't give any error but code doesn't work as it should.
I defined Screen object in ui.cpp and declare it in ui.h file. But if I use Screen object in main.cpp, it doesn't work.
clearDisplay() function should clear the display, but it doesn't. Code doesn't stuck at Screen.clearDisplay()

Why?

1
2
3
4
5
6
	test();
  	delay(3000);
  	digitalWrite(PC13, HIGH);
  	Screen.clearDisplay();
  	delay(1000);
  	digitalWrite(PC13, LOW);


Both objects have same address in the memory.

Sorry for DP
Last edited on
There are no "files" in a program; all the object code is linked together.

I'm trying to optimize RAM usage.

Don't. Not yet. Have you shown that memory is an issue?

Your questions imply that you don't even know how memory is used.

Executable instructions and literal values are in the memory whether they are used or not.
Data (values) gets allocated memory (stack, heap, free store) as needed.
Lifetime of objects depends on whether they are automatic, dynamic, global, etc.


1
2
3
4
5
6
7
8
9
10
void foo( const T & );

int main() {
  T bar;
  foo( bar );
}

void foo( const T & gaz ) {
  // use gaz
}

The implementation of foo() can be in "separate file".
It is sufficient that main() knows the declaration of foo();
Both main() and foo() must see definition of T.

There is only one object, "bar".
Screen is define at global scope in ui.cpp. Why then object "doesn’t work" if I use it in main.cpp?
What do you mean by "doesn't work"?

Is it failing to compile?

Is it failing to link?

Is your program crashing?

Is something else happening?

We can't read your mind. If you have a problem you want us to help solve, you have to tell us what the problem actually is.
Compiler doesn't give eny error.
If I call function drawRect(part of Screen object) from UI.cpp(where Screen object is defined) then function Works fine.
But if I call function clearDisplay(also part of Screen object) from main.cpp, then function doesn't work.
If I call clearDisplay from UI.cpp then it Works as it should.
Why? What causes this problem?

When I say "doesn't work" I mean like I didn't even call the function.
Last edited on
If your code us building, and you've determined that the object being accessed in main.cpp is the same as the one in UI.cpp (by checking the addresses), then it must be something to do with the way you're using the Adafruit library, not with the way you're defining and declaring a global variable.
I use functions on same way in both files. I'll do more tests and write results here. Really have no clue what causes this problem.

EDIT:

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	Screen.clearDisplay();

	Serial.begin(9600);
	test();

  	delay(3000);

  	Screen.setCursor(0, 0);
  	Screen.setTextColor(WHITE, BLACK);
  	Screen.setTextSize(1);
  	Screen.print("test");
  	Screen.display();

  	delay(1000);

  	Screen.clearDisplay();

  	delay(1000);

  	clear();

  	int x = *((int*)(&Screen));
  	Serial.println(x, HEX); // PRINTS OBJECT'S ADDRESS FROM MAIN.CPP
  	Serial.println(pr(), HEX); // PRINTS OBJECT'S ADDRESS FROM UI.CPP 


UI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void test()
{
    for (uint8_t i = 0; i < 65; i += 12)
    {
    	Screen.drawRect(64 - i, 32 - (i / 2), i * 2, i, WHITE);
    	Screen.display();
    }
}

int pr()
{
	return (*((int*)(&Screen))); 
}

void clear()
{
	Screen.clearDisplay();
}



I was wrong, clearDisplay doesn't work at all(no matter from where I call it).
I Search in Adafruit lib and found that function clearDisplay is:

1
2
3
void Adafruit_SH1106::clearDisplay(void) {
  memset(buffer, 0, (SH1106_LCDWIDTH*SH1106_LCDHEIGHT/8));
}


and array buffer is defined in same file at global scope.
Screen.print("test"); Works fine from main.cpp.
I guess clearDisplay doesn't work because it uses memset and I'm not sure what that function doesn't work
Function Works in other procjets(where whole code is in one file)


EDIT: Found what causes the problem. I missed display(). That was the problem. Thanks for help and explanations
Last edited on
Topic archived. No new replies allowed.