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++ extern const link error

In C++, I tried to reference a constant in flash defined in another file using this declaration:

extern const int count;

In another C++ file I had the definition:

const int count = 1;

(The original test case had more to it, but I boiled it down to this simple scenario.)

The linker gives the error "undefined reference to 'count'".  I am using ARM GCC 9.3.1 on a Cortex-M0+ MCU.

I found if I took off the "const" in both places, thus moving it from flash to RAM, there was no problem.  I also eventually figured out that it all linked (and lived in flash) if I added 'extern "C"' to both.  This is an adequate work-around, but I'd like to know what the problem is.  Is it a bug?  Is there something missing from my declarations?

Parents
  • I took off the "const" in both places, thus moving it from flash to RAM

    This is one of those places where C++ differs from C.

    In 'C',

    const int count = 1;

    definitely creates an object in memory.

    But in C++, it doesn't (necessarily); in C++, it could be just like a #define (but with type & scope).

Reply
  • I took off the "const" in both places, thus moving it from flash to RAM

    This is one of those places where C++ differs from C.

    In 'C',

    const int count = 1;

    definitely creates an object in memory.

    But in C++, it doesn't (necessarily); in C++, it could be just like a #define (but with type & scope).

Children
No data