We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi,
I am using uVision4 for LPC3250.
I declare my class in my header file and define its functions in the corresponding cpp file.
My question is this, how can I reach the object of this class when I create it in another file.
For example, in a.cpp:
MyClass MyObject;
in b.cpp
MyBoject.value = 5;
I want my object to be global.
Thanks for your helps.
"I wrote in b.cpp
extern MyClass MyObject;
when I use ... I get the error"
And:
"I made the extern declerations in the header file but I still have error."
Do you understand how header files work? They are just simple text inclusion - so, if it didn't work by typing the 'extern' directly in the .cpp file, then it obviously also won't work just by putting it in a header!
Note that what you are getting is a Linker erro - so the code must have Compiled OK?
So you need to look at why the Linker cannot find a definition for this symbol in its input object modules and/or libraries...
Are you sure that a module that defines MyObject is being linked?
I think the OP has a typo somewhere.
Are you sure that the linker will process a file that contains:
Ant that this line isn't inside a namespace or contains the "static" keyword?
Have you checked your spellings?
And you are sure you are not using some clever #defines that changes what the compilers sees from what you see?
And you are sending the relevant object file to the linker?
Andy, Yes, I am aware that I am getting a Linker error. The code is compiled without a problem.
I manually did not link any library or module. I just wrote a very simple class and created its instance. Should I link any library?
Are you sure that a module that defines MyObject is being linked? No module defining MyObject is linked. Should there be?
Per, I checked spellings, there is no wrong with spellings.
I have 4-5 #defines, I dont think they would cause any problem.
And you are sending the relevant object file to the linker? I dont know how to send the object file to the linker. Can you please explain?
Besides, I am still spending time to globalize my object. It is really important.
No module defining MyObject is linked. Should there be?
Absolutely!
And it really didn't occur to you that this lack of a definition might be the problem, given that the linker error was about an undefined reference?
extern tells that there somewhere is expected to exist a variable with the specified name and type.
But one of the source files must contain that variable. And the corresponding object file must be part of the linking.
Not specific to Keil or ARM!