To be more specific, I am using a compiler which does not support variable-length arrays (Visual C++ 2005)
This requires the constant to be defined (and not merely declared) at the point of use. Furthermore, C has much more restrictions than C++ on what constitutes a constant usable as an array dimension: basically integer literals (which can be substituted via macros) and enumerators; unlike C++ it does not have integral constants (int const x
), so depending on the mode (C or C++) you compile in, you might be restricted.
There is no facility in rustc or Cargo to automatically generate C files, the symbols are only exported and available at link-time, not at compile-time.
You are fortunate though, there is a solution, though it is slightly more cumbersome.
Rust features a build.rs
file, which is compiled and executed as part of the regular compilation process. This file can contain command to generate other files, and therefore it is perfectly possible to:
- Write down the constant once and for all in a
.rs
file
- Generate a C header "exporting" this constant in a C format through the
build.rs
file.