Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
Can a Rust constant/static be exposed to C?

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)

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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.
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda