Comment 1 for bug 1921483

Revision history for this message
David Brown (davidbrown) wrote :

In C++, the default linkage for a "const" declaration is internal. This is different from non-const declarations, and different from C (or C++ with extern "C"), where the default linkage is external. So if you declare an object in a C++ file as "const uint32_t testCplusplusDatap[4] = {..." , then it it has internal linkage - the symbol is not exported from the current file.

There is nothing wrong with the names here, it is simply that the const symbol is left internal to the C++ file.

The quick solution is to add "extern" to the definition of the C++ const array.

The correct solution is to use headers, and be strict in making all your "extern" declarations in a header, while the C or C++ file #include's the matching header and has the definitions of the objects. Any objects that don't have a declaration in the header should be "static", if they are not C++ const objects. The use of the keyword "extern" within a C or C++ file, rather than a header, should therefore be a sign of questionable code structure.

(There are always exceptions to such rules - including quick tests.)