Alex Rivera | Logout

undefined reference to array of constants

Asked 2012-05-30T12:34:43.013
8

a.cpp

const unsigned char whatever[123] = { /* ... */ };

a.h

extern const unsigned char whatever[123];

b.cpp

#include "a.h"
unsigned char x = whatever[0];
// error: undefined reference to 'whatever'

Why do I get an undefined reference error? Without the const, the error goes away.

How do I share an array of constants among multiple translation units?

Edit
Report

1 Answer

1

const has static (internal) linkage by default in c++, use extern const in your .c file as well.
This is a random SO thread that has more info. Or google "linkage in c++".

answered 2012-05-30T12:40:52.933

Your Answer