You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Visual Studio中将带依赖的Rust静态库链接到C++程序时出现未解析外部符号错误的解决咨询

在Visual Studio中将带依赖的Rust静态库链接到C++程序时出现未解析外部符号错误的解决咨询

我需要一些关于将Rust库静态链接到C应用程序的建议。
我可以正确链接没有Rust依赖的简单代码,也可以动态链接(*.dll)带有依赖的代码,但我想把代码嵌入到C
的*.exe中——也就是静态链接,这会在VS构建期间产生很多“未解析外部符号”错误。

为了演示这个问题,我写了下面的简单代码。在取消注释reqwest库功能之前,它完全可以正常工作,之后只有动态链接(cdylib)能正常运行...
我漏掉了什么“技巧”?

test/src/lib.rs

// use reqwest::blocking::Client;

#[no_mangle]
pub extern "C" fn connect() -> i32 {
    // let c = Client::new();
    // c.get("http://www.google.com").send().expect("failed");
    0
}

test/cargo.toml

[package]
name = "test"
version = "0.1.0"
edition = "2021"

[lib]
# crate-type = ["cdylib"]
crate-type = ["staticlib"]

[dependencies]
reqwest = { version = "0.12.9", features = ["blocking"]}

test/test.h

extern "C" {

int32_t connect();

}  // extern "C"

test_c/src/main.cpp

#include <iostream>
#include "test.h"

int main()
{
    int i = connect();
    std::cout << "connect -> " << i << "\n";
}

我从Visual Studio 2019得到的错误如下:

Error       LNK1120    77 unresolved externals                                    test_c    test_c.exe  1   
Error       LNK2001    unresolved external symbol __imp_accept                    test_c    test.lib(std-2df1f22abef96888.std.1de697f09da2cdd5-cgu.0.rcgu.o)    1   
Error       LNK2001    unresolved external symbol __imp_accept                    test_c    test.lib(socket2-220b4c5575b738d5.socket2.195adf5aa58b4623-cgu.1.rcgu.o)    1   
Error       LNK2001    unresolved external symbol __imp_accept                    test_c    test.lib(socket2-220b4c5575b738d5.socket2.195adf5aa58b4623-cgu.2.rcgu.o)    1   
Error       LNK2001    unresolved external symbol __imp_accept                    test_c    test.lib(socket2-220b4c5575b738d5.socket2.195adf5aa58b4623-cgu.0.rcgu.o)    1   
Error       LNK2001    unresolved external symbol __imp_AcceptSecurityContext     test_c    test.lib(reqwest-6591741496af5748.reqwest.d1308cb910735ad3-cgu.04.rcgu.o)   1   

非常感谢任何提示!

备注:内容来源于stack exchange,提问作者masaj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 08:55:27