DLL – 链接到使用MSVC编译的静态库
我正试图在
Windows上针对Rust库链接一个简单的C lib
我的lib是.h extern "C" { void say_hello(const char* s); } 的.cpp #include <stdio.h> void say_hello(const char* s) { printf("hello world"); } 我的Rust文件 #[link(name="CDbax",kind="static")] extern "C" { fn say_hello(s: *const libc::c_char) -> () ; } 通过给出其中一个数据符号的错误来链接失败 error: linking with `gcc` failed: exit code: 1 note: "gcc" "-Wl,--enable-long-section-names" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-Wl,--large-address-aware" "-shared-libgcc" "-L" "C:Program Files (x86)Rust 1.2binrustlibi686-pc-windows-gnulib" "e:RustDBToolsDBAnalyticstargetdebugDBAnalytics.o" "-o" "e:RustDBToolsDBAnalyticstargetdebugDBAnalytics.dll" "e:RustDBToolsDBAnalyticstargetdebugDBAnalytics.metadata.o" "C:Program Files (x86)Rust 1.2binrustlibi686-pc-windows-gnuliblibstd-11582ce5.rlib" "C:Program Files (x86)Rust 1.2binrustlibi686-pc-windows-gnuliblibcollections-11582ce5.rlib" "C:Program Files (x86)Rust 1.2binrustlibi686-pc-windows-gnuliblibrustc_unicode-11582ce5.rlib" "C:Program Files (x86)Rust 1.2binrustlibi686-pc-windows-gnuliblibrand-11582ce5.rlib" "C:Program Files (x86)Rust 1.2binrustlibi686-pc-windows-gnulibliballoc-11582ce5.rlib" "C:Program Files (x86)Rust 1.2binrustlibi686-pc-windows-gnulibliblibc-11582ce5.rlib" "C:Program Files (x86)Rust 1.2binrustlibi686-pc-windows-gnuliblibcore-11582ce5.rlib" "-L" "e:RustDBToolsDBAnalyticstargetdebug" "-L" "e:RustDBToolsDBAnalyticstargetdebugdeps" "-L" "C:Program Files (x86)Rust 1.2binrustlibi686-pc-windows-gnulib" "-L" "e:RustDBToolsDBAnalytics.rustbini686-pc-windows-gnu" "-L" "e:RustDBToolsDBAnalyticsbini686-pc-windows-gnu" "-Wl,-Bstatic" "-Wl,--whole-archive" "-l" "CDbax" "-Wl,--no-whole-archive" "-Wl,-Bdynamic" "-l" "ws2_32" "-l" "userenv" "-l" "advapi32" "-shared" "-l" "compiler-rt" note: Warning: corrupt .drectve at end of def file Cannot export ??_C@_0M@LACCCNMM@hello?5world?$AA@: symbol not found 该库是在MSVC2013上构建的一个简单的静态库.字符串“hello world”位于数据部分,因此我不希望它导致链接错误.在Windows上与C库链接时,是否需要注意一些特定的设置? 顺便说一句,这是32位MSVC lib. 解决方法
好的,一些事情.首先,没有“静态DLL”这样的东西:DLL是一个动态链接的库.
其次,Rust使用MinGW工具链和运行时.混合使用MSVC和MinGW运行时可能会导致奇怪的事情发生,因此如果可能的话,最好避免使用它. Rust最近才获得了使用MSVC运行时构建的早期支持. 但是,你可以让这个具体的例子起作用,显然没有任何不良影响.你只需要改变一些事情: >您需要使用动态库;我的理解是,这会使不良交互的可能性降低一些. 从而: hello.rs: #[link(name="hello",kind="dylib")] extern { fn say_hello(); } fn main() { unsafe { say_hello(); } } hello.h: #ifndef HELLO_H #define HELLO_H extern "C" { __declspec(dllexport) void say_hello(); } #endif HELLO.CPP: #include <cstdio> #include "hello.h" void say_hello() { printf("hello worldn"); } build.cmd cl /LD hello.cpp rustc -L. hello.rs 在我的机器上,这会产生hello.exe和hello.dll;运行时,hello.exe打印出hello world. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows – 如何通过PowerShell的串行控制台电缆连接到思科
- 如何将uint32_t转换为struct in_addr?
- windows下一个tomcat版本创建多个实例
- windows – Cygwin启动错误/usr/bin/id:找不到组ID的名称
- 隐藏Microsoft Sql Server 2008R2及更高版本上的登录数据库
- windows-server-2012 – 如何在Windows Server 2012中修改I
- 20164322韩玉婷 -----Exp2 后门原理和实践
- VS远程调试小记
- 调整Windows7下时间同步的频率
- windows-server-2008 – Windows Server 2008 R2 – 如何更