锈和C与Visual Studio的兼容性。

时间:2022-09-06 21:24:15

Is it possible to create a static library compiled with rustc and link it to an executable compiled with MSVC?

是否可以创建一个用rustc编译的静态库,并将其链接到一个用MSVC编译的可执行文件?

1 个解决方案

#1


1  

If you want to use only rustc to produce a static library, you will do this by specifying some attributes in your crate's lib.rs file, and marking exported functions as so:

如果您只想使用rustc来生成一个静态库,您可以在您的crate的lib.rs文件中指定一些属性,并将导出的函数标记为:

#![crate_type = "static_lib"]
#![crate_name = "mylib"]

use libc::c_int;

#[no_mangle]
pub extern fn my_exported_func(num: c_int) -> c_int {
    num + 1
}

Then simply invoke rustc lib.rs. This applies to all platforms that rustc supports.

然后简单地调用rustc lib.rs。这适用于rustc支持的所有平台。

In a C/C++ header, add:

在C/ c++ header中,添加:

#pragma once

// only use extern block if the header is put inside a C++ CU
extern "C" {
    int my_exported_func(int num);
}

and link the output .lib or .a as necessary.

并根据需要链接输出.lib或.a。

For Cargo, you can specify the crate type and name in your Cargo.toml.

对于货物,您可以在您的Cargo.toml中指定crate类型和名称。

Sources:

来源:

#1


1  

If you want to use only rustc to produce a static library, you will do this by specifying some attributes in your crate's lib.rs file, and marking exported functions as so:

如果您只想使用rustc来生成一个静态库,您可以在您的crate的lib.rs文件中指定一些属性,并将导出的函数标记为:

#![crate_type = "static_lib"]
#![crate_name = "mylib"]

use libc::c_int;

#[no_mangle]
pub extern fn my_exported_func(num: c_int) -> c_int {
    num + 1
}

Then simply invoke rustc lib.rs. This applies to all platforms that rustc supports.

然后简单地调用rustc lib.rs。这适用于rustc支持的所有平台。

In a C/C++ header, add:

在C/ c++ header中,添加:

#pragma once

// only use extern block if the header is put inside a C++ CU
extern "C" {
    int my_exported_func(int num);
}

and link the output .lib or .a as necessary.

并根据需要链接输出.lib或.a。

For Cargo, you can specify the crate type and name in your Cargo.toml.

对于货物,您可以在您的Cargo.toml中指定crate类型和名称。

Sources:

来源: