2

In C file

// f1.c
const uint8_t C_VAL=2;`  

In C++ file I declare simple array

// f2.cpp
extern "C" const uint8_t C_VAL;
char charray[C_VAL];

Build output: #259: constant value is not known

There was not any errors when I link extern in .C

// f2.c
extern const uint8_t C_VAL;
char charray[C_VAL];

Works fine.

Seems the problem is with linkage. Is it possible to fix, how? Is this problem only in MDK-ARM or other compilers have too?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
kyb
  • 7,233
  • 5
  • 52
  • 105
  • Where exactly do you declare `char charray[C_VAL];` ? Inside a function our outside of functions `? – Jabberwocky Oct 04 '16 at 09:55
  • C and C++ are different languages. Why do you expect them to have the same features? – too honest for this site Oct 04 '16 at 10:22
  • If you are using *gcc*, you can probably use `-std=gnu++11` or something, to enable extension to support for variable length arrays on C++ code. – hyde Oct 04 '16 at 10:36
  • @Olaf, this is one of the design objectives of Stroustrup when designing the C++ language, the usability of C code from C++. This is an extremely simple example that shows something that should work. So I think your comment is out of scope here. I upvote and follow you in many questions we share, but I cannot do this time. – Luis Colorado Oct 05 '16 at 06:21
  • @LuisColorado: That might have been true for pre-standard C++ and ancient C90, resp. historic K&R-C. Especially the `const` qualifier has different semantics in both languages, not to mention other incompatibilities. Identical syntax does not imply identical semantics. And just using some C library would make any question a C question! Reading and comparing the standards helps! Re. what Stroustrup once wrote: Linus Torvalds once wrote that Linux would never be ported to anything other than x86 ... – too honest for this site Oct 05 '16 at 09:50

1 Answers1

4

C supports variable length arrays (since C99, but only optionally since C11. Some earlier compilers support them as a language extension), so the value of C_VAL doesn't need to be known at the time f2.c is compiled and so there isn't a problem.

C++ doesn't support variable length arrays (except some compilers support them as a language extension), so the value of C_VAL must be known at the time f2.cpp is compiled. Since it is merely declared, its value is not known and that is why the compiler shows you the quoted error. The value remains unknown until the object files of f2.cpp and f1.c are linked together.

Solution: Either use a language that supports VLA (such as C99), or a compiler that supports VLA as an extension (see the manual of your compiler whether it supports it, and how to enable the support) or define the array with a length that is known at compile time.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I'd add, that value *can't* be known at compile time usually, because the dynamic library (or whatever) where `C_VAL` is defined can change. – hyde Oct 04 '16 at 10:37