我正试图在Windows上针对Rust库链接一个简单的C lib
我的lib是.h
extern "C" {
void say_hello(const char* s);
}
Run Code Online (Sandbox Code Playgroud)
的.cpp
#include <stdio.h>
void say_hello(const char* s) {
printf("hello world");
}
Run Code Online (Sandbox Code Playgroud)
我的Rust文件
#[link(name="CDbax", kind="static")]
extern "C" {
fn say_hello(s: *const libc::c_char) -> () ;
}
Run Code Online (Sandbox Code Playgroud)
通过给出其中一个数据符号的错误来链接失败
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.2\bin\rustlib\i686-pc-windows-gnu\lib" "e:\Rust\DBTools\DBAnalytics\target\debug\DBAnalytics.o" "-o" "e:\Rust\DBTools\DBAnalytics\target\debug\DBAnalytics.dll" "e:\Rust\DBTools\DBAnalytics\target\debug\DBAnalytics.metadata.o" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\libstd-11582ce5.rlib" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\libcollections-11582ce5.rlib" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\librustc_unicode-11582ce5.rlib" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\librand-11582ce5.rlib" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\liballoc-11582ce5.rlib" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\liblibc-11582ce5.rlib" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib\libcore-11582ce5.rlib" "-L" "e:\Rust\DBTools\DBAnalytics\target\debug" "-L" "e:\Rust\DBTools\DBAnalytics\target\debug\deps" "-L" "C:\Program Files (x86)\Rust 1.2\bin\rustlib\i686-pc-windows-gnu\lib" "-L" "e:\Rust\DBTools\DBAnalytics\.rust\bin\i686-pc-windows-gnu" "-L" "e:\Rust\DBTools\DBAnalytics\bin\i686-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
Run Code Online (Sandbox Code Playgroud)
该库是在MSVC2013上构建的一个简单的静态库.字符串"hello world"位于数据部分,因此我不希望它导致链接错误.在Windows上与C库链接时,是否需要注意一些特定的设置?
顺便说一句,这是32位MSVC lib.
DK.*_*DK. 10
好的,一些事情.首先,没有"静态DLL"这样的东西:DLL是一个动态链接的库.
其次,Rust使用MinGW工具链和运行时.混合使用MSVC和MinGW运行时可能会导致奇怪的事情发生,因此如果可能的话,最好避免使用它.Rust最近才获得了使用MSVC运行时构建的早期支持.
但是,你可以让这个具体的例子起作用,显然没有任何不良影响.你只需要改变一些事情:
您需要使用动态库; 我的理解是,这会使不良交互的可能性降低一些.
你需要真正编译say_hello与C联动,不C++联动.您在标题中执行了此操作,但未在源文件中执行此操作.
您需要say_hello从库中公开导出.
从而:
hello.rs:
#[link(name="hello", kind="dylib")]
extern {
fn say_hello();
}
fn main() {
unsafe { say_hello(); }
}
Run Code Online (Sandbox Code Playgroud)
hello.h:
#ifndef HELLO_H
#define HELLO_H
extern "C" {
__declspec(dllexport) void say_hello();
}
#endif
Run Code Online (Sandbox Code Playgroud)
hello.cpp:
#include <cstdio>
#include "hello.h"
void say_hello() {
printf("hello world\n");
}
Run Code Online (Sandbox Code Playgroud)
build.cmd
cl /LD hello.cpp
rustc -L. hello.rs
Run Code Online (Sandbox Code Playgroud)
在我的机器上,这产生hello.exe和hello.dll; 跑步时hello.exe打印出来hello world.
| 归档时间: |
|
| 查看次数: |
1200 次 |
| 最近记录: |