对CLSID_MMDeviceEnumerator和IID_IMMDeviceEnumerator的未定义引用 [英] Undefined reference to CLSID_MMDeviceEnumerator and IID_IMMDeviceEnumerator

查看:572
本文介绍了对CLSID_MMDeviceEnumerator和IID_IMMDeviceEnumerator的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在C中使用MinGW-w64使用COM和CoCreateInstance()编译示例代码失败.

Trying to compile an example code using COM and CoCreateInstance() using MinGW-w64 in C fails.

#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>

#include <stdlib.h>
#include <stdio.h>

extern const CLSID CLSID_MMDeviceEnumerator;
extern const IID IID_IMMDeviceEnumerator;

int main( void )
{
    CoInitialize( NULL );

    LPVOID device = NULL;
    const HRESULT ok = CoCreateInstance(    &CLSID_MMDeviceEnumerator, NULL, 
                                            CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, 
                                            &device );  
    CoUninitialize();

return EXIT_SUCCESS;
}

使用以下命令编译:gcc main.c libole32.a -Wall -Wextra -o a

Compiling with: gcc main.c libole32.a -Wall -Wextra -o a

即使在mmdeviceapi.h中定义了CLSID_MMDeviceEnumerator,也找不到.实际上,从示例代码中删除我的extern定义会得到相同的结果,因为两个extern似乎都在mmdeviceapi.h中定义了.

Even though CLSID_MMDeviceEnumerator is defined in mmdeviceapi.h it isn't found. Actually removing my extern definitions from the sample code gives the same result since both externs seems to be defined in the mmdeviceapi.h

当我使用__uuidof并使用g ++进行编译时,代码起作用了,但是__uuidof的C替换"无效.

When I was using __uuidof and compiling with g++ the code worked, but this C "replacement" for __uuidof doesn't.

为什么找不到COM标识符?

Why aren't COM identifiers found?

推荐答案

使用MinGW-w64时,解决方案是在包含包含COM标识符(例如mmdeviceapi.h#include <initguid.h>. >.

The solution, when using MinGW-w64, is to include the header #include <initguid.h>, before including headers that contain COM identifiers such as mmdeviceapi.h, endpointvolue.h.

然后不需要额外的声明,并且可以直接使用标识符.

Then no extra declarations are needed and identifiers can be used directly.

解决方案:

#include <windows.h>
#include <initguid.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
    CoInitialize( NULL );

    LPVOID device = NULL;
    const HRESULT ok = CoCreateInstance(    &CLSID_MMDeviceEnumerator, NULL, 
                                            CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, 
                                            &device );  
    CoUninitialize();

return EXIT_SUCCESS;
}

这篇关于对CLSID_MMDeviceEnumerator和IID_IMMDeviceEnumerator的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆