This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Global Class Decleration

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.

Parents Reply Children
  • I wrote in b.cpp

    extern MyClass MyObject;
    

    when I use

    MyObject.value = 5;
    

    but I get the error
    "Undefined symbol MyObject (referred from b.o)"

    and the project is not built.

    Thanks for your helps.

  • Even better to read a good C++ book if the program is using C++.

    I normally have a global.h with extern declarations for more important global variables.

    Less important variables are either exported the module-specific header file, or have an accessor function in case it's a singleton.

    Overall, C++ is great for keeping down the number of symbols in the global namespace.

  • In your "b.cpp", add:

    extern MyClass MyObject;
    

    No, don't do that. Never write "extern" in a module's implementation file. Extern declarations of globally available items belong into the interface header file of the module defining them, not in the implementation file of modules using them.

    The basic rule is: no extern in *.c/*.cpp files, and no static in *.h files.

  • Hans,

    You are right of course (for the record, I NEVER do that). I guess I just tried to pull the OP out of the mud while I should have taken the time to elaborate a little more. My bad.

  • Why not?

    My K&R doesn't say anything about it being illegal.

  • My K&R doesn't say anything about it being illegal.

    Legality is not the problem. Clean design and code maintainability are.

    Extern-declaring other modules' objects in your own is an invitation for problems that are notoriously hard (read: expensive!) to debug, because it robs the compiler and most other tools of any opportunity to check the correctness of that declaration.

    Declarations of global objects are part of the defining modules' job. Duplicating that work can only result in two kinds of results: either your re-declaration is equivalent to the original one --- then it's a wast of time to write it down. Or it's not equivalent --- then you're in undefined behaviour land, and you really don't want to go there.

    In the end, nothing good ever comes from writing "extern" in a C file.

  • Just because it's not illegal doesn't mean to say that it is desirable!

    eg, goto is entirely legal, but rarely desirable.

    To see this taken to the extreme, just look at the Obfuscated 'C' Code Contest...!

    http://www.ioccc.org/

  • Hi,

    I made the extern declerations in the header file but I still have error. I don't know maybe the problem is with the project options?

    The error says:

    Error: L6218E: Undefined symbol MyObject (referred from b.o).
    

    And the information with this error say:

    Not enough information to list image symbols.
    Not enough information to list the image map.
    

    I am really stuck here, your helps will be beneficial.

    Thank you.

  • "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:

    MyClass MyObject;
    


    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?

  • Hi,

    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.

    Thanks for your helps.

  • 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.