13

Say I have a Rust API that contains a constant or a static, an i32 for example. I want to use this Rust API from C. From the C side, I want to use that constant as an array size. Am I correct that there is no way to do it? Is the best solution to redeclare the constant in my C header files that are providing the declarations for the rest of the Rust API?

Update: To be more specific, I am using a compiler which does not support variable-length arrays (Visual C++ 2005)

2 Answers2

15

You most certainly can do it, at least inside functions:

cnst.rs:

#[no_mangle]
pub static X: i32 = 42;

cnstuse.c:

#include <stdint.h>
#include <stdio.h>

extern const int32_t X;

int main() {
    char data[X];
    printf("%lu\n", sizeof(data));
    return 0;
}

Compilation:

% rustc --crate-type=staticlib cnst.rs
note: link against the following native artifacts when linking against this static library
note: the order and any duplication can be significant on some platforms, and so may need to be preserved
note: library: System
note: library: pthread
note: library: c
note: library: m
% gcc -o cnstuse cnstuse.c libcnst.a
% ./cnstuse
42

Top-level array declarations, however, can't use global variables/constants for size, so this will only work inside functions.

Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • 1
    Ah, that works fine for compilers that support variable-length arrays, but not for mine. I'm using the Visual C++ 2005 compiler. I will update my question to be more specific. –  Jul 29 '15 at 15:49
  • In that case you're stuck with preprocessor-based constants, so you wouldn't be able to use constants defined on Rust side, unfortunately. – Vladimir Matveev Jul 29 '15 at 18:42
  • how to declare `X` for C without initilizing it? https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=eb9a09e43bf6495eb950fcea1a967ae6 https://stackoverflow.com/q/65292878 – user2284570 Dec 15 '20 at 01:20
1

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:

  1. Write down the constant once and for all in a .rs file
  2. Generate a C header "exporting" this constant in a C format through the build.rs file.
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • I will have to mess around with build.rs. I've seen it, but passed over it in favor of getting as simple of a minimum proof-of-concept that I could. –  Jul 31 '15 at 18:26