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

无法链接InMemoryRandomAccessStream的LNK2019错误求助

问题原因分析

LNK2019未解析外部符号错误指向winrt::Windows::Storage::Streams::InMemoryRandomAccessStream的默认构造函数,核心原因有两点:

  • 缺少必要头文件:Microphone.h中未引入winrt/Windows.Storage.Streams.h,导致编译器仅生成构造函数的引用,但链接器无法找到对应的实现代码。
  • 未显式初始化WinRT对象:类成员audioStream作为WinRT类型对象,需要显式触发构造逻辑,默认零初始化无法调用其构造函数。
解决方案
  1. 在Microphone.h中添加winrt/Windows.Storage.Streams.h头文件,确保链接器能定位到InMemoryRandomAccessStream的实现。
  2. 在Microphone构造函数中显式初始化audioStream成员(通过初始化列表或构造函数内赋值均可)。
修改后的代码示例

Microphone.h

#pragma once
#include <windows.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.Core.h>
#include <winrt/Windows.UI.Core.h>
#include <winrt/Windows.UI.Composition.h>
#include <winrt/Windows.UI.Input.h>
#include <winrt/Windows.Media.Capture.h>
#include <winrt/Windows.Media.MediaProperties.h>
// 添加WinRT流操作的必要头文件
#include <winrt/Windows.Storage.Streams.h>

using namespace winrt::Windows::Media::Capture;
using namespace winrt::Windows::Media::MediaProperties;

class Microphone
{
public:
    Microphone();
    void StartRecording();
    void StopRecording();
    winrt::Windows::Storage::Streams::InMemoryRandomAccessStream AudioStream();
private:
    MediaCapture mediaCapture;
    winrt::Windows::Storage::Streams::InMemoryRandomAccessStream audioStream;
};

Microphone.cpp

#include "Microphone.h"

// 通过初始化列表显式初始化audioStream
Microphone::Microphone() : audioStream{}
{
    // Initialize the MediaCapture object
    mediaCapture = MediaCapture();
    auto settings = MediaCaptureInitializationSettings();
    settings.StreamingCaptureMode(StreamingCaptureMode::Audio);
    mediaCapture.InitializeAsync(settings).get();
}

void Microphone::StartRecording()
{
    // Set up the audio encoding properties
    auto audioProfile = MediaEncodingProfile::CreateWav(AudioEncodingQuality::High);
    // Start recording audio
    mediaCapture.StartRecordToStreamAsync(audioProfile, audioStream).get();
}

void Microphone::StopRecording()
{
    // Stop recording audio
    mediaCapture.StopRecordAsync().get();
}

winrt::Windows::Storage::Streams::InMemoryRandomAccessStream Microphone::AudioStream()
{
    return audioStream;
}
额外注意事项
  • 确保项目已正确配置Windows SDK,控制台项目需确认WindowsApp.lib已包含在链接器输入项中(WinRT项目通常会自动配置)。
  • 你的代码中已正确调用winrt::init_apartment()初始化WinRT运行时环境,无需修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 15:35:01