我目前正试图用Rust编写一个动态库,它将从现有程序加载.我需要导出一些具有特定名称和调用约定的函数.一切正常,但一旦我从标准库中取出任何东西:
- DLL大小inflating 到超过3MB(不太好看,但我可以接受)
- 整个标准库从DLL中导出.以下是所有导出的列表:http://pastebin.com/LsG1u96C(5http://pastebin.com/LsG1u96C个函数)
我是不是少了一些编译器switch ?
#![crate_type = "dylib"]
#![feature(std_misc)]
use std::ffi::CString;
#[link(name = "user32")]
#[allow(non_snake_case)]
extern "stdcall" {
fn MessageBoxA(hWnd: u32, lpText: *const i8, lpCaption: *const i8, uType: u32) -> u32;
}
#[no_mangle]
#[allow(non_snake_case)]
pub unsafe extern "stdcall" fn _AddLuaState(lua_state_ptr: u32)
{
let info_str = format!("Lua State Created: {}!", lua_state_ptr);
let info_cstring = CString::new(info_str).unwrap();
let caption = CString::new("Hello from my Rust Library!").unwrap();
MessageBoxA(0, info_cstring.as_ptr(), caption.as_ptr(), 0);
}
_AddLuaState@4
是唯一应该导出的函数.
这是在Windows 8.1机器上的rustc 1.0.0-nightly (522d09dfe 2015-02-19)
(x86)
更新:编译带有rustc -C prefer-dynamic
的动态链接文件时,DLL大小缩小到60kiB,只有3个额外的导出(http://pastebin.com/G0AYZrpF个),看起来都很合理.但我还是更喜欢静态链接的库.