简体   繁体   English

Rust常量/静态可以暴露给C吗?

[英]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. 假设我有一个包含常量或静态的Rust API,例如i32。 I want to use this Rust API from C. From the C side, I want to use that constant as an array size. 我想从C使用这个Rust API。从C方面,我想将该常量用作数组大小。 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? 在我的C头文件中重新声明常量的最佳解决方案是为其余的Rust API提供声明吗?

Update: To be more specific, I am using a compiler which does not support variable-length arrays (Visual C++ 2005) 更新:更具体地说,我使用的是不支持可变长度数组的编译器(Visual C ++ 2005)

You most certainly can do it, at least inside functions: 你当然可以做到这一点,至少在内部功能:

cnst.rs : cnst.rs

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

cnstuse.c : 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. 但是,顶级数组声明不能将全局变量/常量用于大小,因此这仅适用于函数内部。

To be more specific, I am using a compiler which does not support variable-length arrays (Visual C++ 2005) 更具体地说,我使用的是不支持可变长度数组的编译器(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; 此外,C对于构成可用作数组维的常量的限制比C ++有更多限制:基本上是整数文字(可以通过宏替换)和枚举数; 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. 与C ++不同,它没有整数常量( int const x ),因此根据您编译的模式(C或C ++),您可能会受到限制。

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. 在rustc或Cargo中没有自动生成C文件的工具,这些符号仅在链接时导出并可用,而不是在编译时。


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. Rust具有build.rs文件,该文件作为常规编译过程的一部分进行编译和执行。 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 .rs文件中一次性写下常量
  2. Generate a C header "exporting" this constant in a C format through the build.rs file. 通过build.rs文件生成C标头,以C格式“导出”此常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM