User forums > General (but related to Code::Blocks)

extern const

<< < (2/2)

grv575:

--- Quote from: Ceniza on January 10, 2006, 12:09:30 am ---
--- Quote from: sethjackson ---Then in appglobals.cpp set the wxString's to something? Why is this needed?
--- End quote ---

Declarations in header files and definitions in source files? :)

Those macros are just to protect against re-declaring them for the same Translation Unit, but if more than one Translation Unit includes the same header file and instead of declarations it finds definitions you'll get problems linking.

--- End quote ---

but the header has just declarations... you can later define them in a specific source file.  tried a simple gcc project and it didn't look like extern was needed (non-local/function vars are global i.e. extern scope by default)

Ceniza:
If you remove the "extern" but keep the "const", all those wxString will be initialized with an empty string and remain "const". Including that header (without "extern") in more than one TU (Translation Unit) and compiling it won't give you problems. If those weren't const neither extern linking would complain, 'cause it would find "multiple definitions" for every wxString there. The only thing that makes it "work" is that "const" makes it have internal linkage, so it'd mean multiple definitions with the same name but without clashing, if every definition occurs in only one TU.

With "extern", it gives the compiler enough info about their type to compile a TU, knowing their definition will be somewhere else (it's now the linker's job to 'link' the definition when linking).

Navigation

[0] Message Index

[*] Previous page

Go to full version