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

C++: Not possible to define global constants?

If in "normal" C, I define a global constant in one module:

const int iTabCount= 5;


and then use this constant in another module (module2.c) with extern:

extern const int iTabCount;


Then "normal" C will do this without any problems.

But in C++ files this fails, even if I encapsulate these definitions in extern "C" { ... };

If I use C++, in the first module, I get

warning #177-D: variable "iTabCount" was declared but never referenced


(This is correct, it is really not referenced in this module - but as this variable is NOT defined as static, there should be no problem ...).

And on linking I get the following error:

Error: L6218E: Undefined symbol iTabCount (referred from module2.c)

All works fine, if I remove the "const" qualifier. But I would like to keep it - especially for some larger const table, where it would be quite inefficient to reserve RW data space for such a code table.

I think this is some C++ compiler problem? Not possible to use const data over several modules in C++?

Parents
  • Your problem starts right here:

    and then use this constant in another module (module2.c) with extern:

    extern const int iTabCount;
    

    You should basically never write "extern" in any *.c file. Extern declarations belong into *.h files, which you then #include into *.c files (and that includes the *.c file with the definition).

    warning #177-D: variable "iTabCount" was declared but never referenced
    

    This would probably not have happened had you #included that *.h file into the module defining the variable.

    I think this is some C++ compiler problem?

    No. It's a problem with your assumption that C and C++ were basically the same language, so you could just compile some arbitrary piece of C as C++ and get away with it. Well, that hasn't been the case since basically day 1 of the existence of C++ as a practical programming language. So, to be blunt: C and C++ are different languages, get used to it. And yes, the precise meaning of the 'const' qualifier is one of the key differences between them. C uses it to signify write-protected variables, C++ for actual constants.

Reply
  • Your problem starts right here:

    and then use this constant in another module (module2.c) with extern:

    extern const int iTabCount;
    

    You should basically never write "extern" in any *.c file. Extern declarations belong into *.h files, which you then #include into *.c files (and that includes the *.c file with the definition).

    warning #177-D: variable "iTabCount" was declared but never referenced
    

    This would probably not have happened had you #included that *.h file into the module defining the variable.

    I think this is some C++ compiler problem?

    No. It's a problem with your assumption that C and C++ were basically the same language, so you could just compile some arbitrary piece of C as C++ and get away with it. Well, that hasn't been the case since basically day 1 of the existence of C++ as a practical programming language. So, to be blunt: C and C++ are different languages, get used to it. And yes, the precise meaning of the 'const' qualifier is one of the key differences between them. C uses it to signify write-protected variables, C++ for actual constants.

Children
  • Ups - I am surprised. You are right - if I insert the

    extern const int iAnzParTab;
    


    into the header file (included into both modules), then all works fine.

    Thank's a lot! (I tried it also with my table - I can also declare the "unknown size" table in this header file, and then give the specific size only in module2.cpp - that's exactly what I needed:)

    extern const TAB aTab[];