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

Visual Studio使用cpprestsdk创建dll时提示命名空间不存在

问题描述

我需要创建一个可调用API并返回响应的dll文件,用于Unity项目中。为简化开发,我安装了cpprestsdk,按照官方入门教程操作时示例代码可正常运行,但修改代码尝试生成dll时出现了编译错误。

相关代码

源代码(SendRequest.cpp)

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include "pch.h"

using namespace utility;                    // 字符串转换等通用工具
using namespace web;                        // URI相关通用特性
using namespace web::http;                  // HTTP通用功能
using namespace web::http::client;          // HTTP客户端特性
using namespace concurrency::streams;       // 异步流

extern "C" {

    void SendRequest() {
        auto fileStream = std::make_shared<ostream>();

        // 打开输出文件流
        pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
        {
            *fileStream = outFile;

            // 创建http_client发送请求
            http_client client(U("https://www.bing.com/"));

            // 构建请求URI并发起请求
            uri_builder builder(U("/search"));
            builder.append_query(U("q"), U("cpprestsdk github"));
            return client.request(methods::GET, builder.to_string());
        })

        // 处理响应头返回
        .then([=](http_response response)
        {
            printf("Received response status code:%u\n", response.status_code());

            // 将响应体写入文件
            return response.body().read_to_end(fileStream->streambuf());
        })

    // 关闭文件流
            .then([=](size_t)
                {
                    return fileStream->close();
                });

        // 等待所有I/O完成,处理异常
        try
        {
            requestTask.wait();
        }
        catch (const std::exception& e)
        {
            printf("Error exception:%s\n", e.what());
        }
    }

}

头文件代码

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>

#define SENDREQUEST_API __declspec(dllexport) 

extern "C" {
    SENDREQUEST_API void SendRequest();
}

报错信息(已截断)

1>C:\Users\91805\source\repos\RestAPICalls\RestAPICalls\SendRequest.cpp(5,24): error C2871: 'utility': a namespace with this name does not exist
1>C:\Users\91805\source\repos\RestAPICalls\RestAPICalls\SendRequest.cpp(6,20): error C2871: 'web': a namespace with this name does not exist
1>C:\Users\91805\source\repos\RestAPICalls\RestAPICalls\SendRequest.cpp(7,17): error C2653: 'web': is not a class or namespace name

补充说明

我使用Visual Studio开发dll,参照原生插件开发教程已可成功生成无外部依赖的dll。


解决方案

报错的核心原因是预编译头引用顺序错误,附带还有几个后续编译、Unity适配需要注意的问题,按以下步骤修改即可:

  • 调整cpp文件的include顺序
    Visual Studio默认开启预编译头功能时,要求pch.h必须是代码文件中第一行被include的内容,放在其他头文件后面会导致预编译头不生效,后续引入的第三方库头文件实际没有被正确解析,就会出现命名空间找不到的错误。
    把SendRequest.cpp开头的include顺序改成:
    #include "pch.h"
    #include <cpprest/http_client.h>
    #include <cpprest/filestream.h>
    
  • 修正头文件与函数的导出声明
    当前cpp里的SendRequest函数没有加SENDREQUEST_API导出宏,会导致dll生成后Unity找不到函数入口,把cpp里extern "C"块内的函数定义改成:
    SENDREQUEST_API void SendRequest() {
        // 原有函数逻辑不变
    }
    
    另外头文件里不要直接引入cpprestsdk的头,避免Unity侧引用头时出现依赖问题,头文件可以简化成:
    #pragma once
    #define SENDREQUEST_API __declspec(dllexport) 
    extern "C" {
        SENDREQUEST_API void SendRequest();
    }
    
  • 检查项目配置匹配
    • 确认项目的附加包含目录、附加库目录已经正确配置cpprestsdk的对应路径,链接器输入里已经添加了cpprestsdk的lib依赖,和你跑通官方示例时的配置保持一致即可
    • 编译dll的架构必须和Unity项目的目标架构一致:如果Unity是64位版本就编译x64的dll,32位就编译x86,不能混用
    • 把cpprestsdk对应的dll(比如cpprest_2_xx.dll、对应的openssl依赖dll)和你生成的dll一起放到Unity的Plugins文件夹下,否则运行时会报找不到依赖的错误

内容的提问来源于stack exchange,提问作者aquaman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 19:51:18