未定义对_imp__CoInitialize@4的引用

3

我正在查看Virtual Box的示例。我尝试快速执行以下代码,但出现了与COM相关的错误。

    //#include "VirtualBox.h"
#include "rpc.h" #include
int main(int argc, char *argv[]) { HRESULT rc; // IVirtualBox *virtualBox;
do { /* 初始化COM子系统。*/ CoInitialize(NULL);
/* 实例化VirtualBox根对象。*/ // rc = CoCreateInstance(CLSID_VirtualBox, /* VirtualBox基础对象 */ // NULL, /* 不进行聚合 */ // CLSCTX_LOCAL_SERVER, /* 对象存在于此计算机上的服务器进程中 */ // IID_IVirtualBox, /* 接口的IID */ // (void**)&virtualBox);
if (!SUCCEEDED(rc)) { printf("创建VirtualBox实例时出错!rc = 0x%x\n", rc); break; }
} while (0);
//CoUninitialize(); return 0; printf("你好,世界\n");
return 0;
And the error I have got is:
<pre>
E:\vbox\mscom\samples>g++ -I E:\vbox\mscom\include -Wall helloworld.cpp -o helloworld.exe
helloworld.cpp: In function 'int main(int, char**)':
helloworld.cpp:26: warning: format '%x' expects type 'unsigned int', but argument 2 has type 'HRESULT'
helloworld.cpp:24: warning: 'rc' is used uninitialized in this function
C:\Users\AKANTH~1.ADO\AppData\Local\Temp\ccCmOygi.o:helloworld.cpp:(.text+0x1e): undefined reference to `_imp__CoInitialize@4'
collect2: ld returned 1 exit status

1个回答

14

尝试一下

g++ -I E:\vbox\mscom\include -Wall helloworld.cpp -o helloworld.exe -lole32 -loleaut32

参考资料

CoInitialize函数

你需要链接ole32.dll ("-lole32")。为了链接到它,你需要安装它的导入库(ole32.lib),并将其放在搜索路径中。我假设你可能需要安装Windows SDK。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接