加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

DLL – 链接到使用MSVC编译的静态库

发布时间:2020-12-14 02:54:48 所属栏目:Windows 来源:网络整理
导读:我正试图在 Windows上针对Rust库链接一个简单的C lib 我的lib是.h extern "C" { void say_hello(const char* s);} 的.cpp #include stdio.hvoid say_hello(const char* s) { printf("hello world");} 我的Rust文件 #[link(name="CDbax",kind="static")]exter
我正试图在 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运行时构建的早期支持.

但是,你可以让这个具体的例子起作用,显然没有任何不良影响.你只需要改变一些事情:

>您需要使用动态库;我的理解是,这会使不良交互的可能性降低一些.
>您需要使用C链接实际编译say_hello,而不是C链接.您在标题中执行了此操作,但未在源文件中执行此操作.
>您需要从库中公开导出say_hello.

从而:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读