开发者

undefined reference to _imp__CoInitialize@4

开发者 https://www.devze.com 2023-03-20 18:00 出处:网络
I was looking at Virtual Box examples. I quickly tried to fire the following code but I got the error related to COM.

I was looking at Virtual Box examples. I quickly tried to fire the following code but I got the error related to COM.


    //#include "VirtualBox.h"

    #include "rpc.h"
    #include 

    int main(int argc, char *argv[])
    {
         HRESULT rc;
     //    开发者_如何学Go  IVirtualBox *virtualBox;

          do
            {
                /* Initialize the COM subsystem. */
                CoInitialize(NULL);

                /* Instantiate the VirtualBox root object. */
             //   rc = CoCreateInstance(CLSID_VirtualBox,       /* the VirtualBox base object */
             //                        NULL,                   /* no aggregation */
             //                         CLSCTX_LOCAL_SERVER,    /* the object lives in a server process on this machine */
              //                        IID_IVirtualBox,        /* IID of the interface */
             //                         (void**)&virtualBox);

                if (!SUCCEEDED(rc))
                {
                    printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
                    break;
                }


            } while (0);

            //CoUninitialize();
            return 0;
        printf("Hello, world\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


Try

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

Reference

CoInitialize Function

You need to link ole32.dll ("-lole32"). In order to link to that, you need to have the import library for it (ole32.lib) installed, and in your search paths. I assume you might need to have the Windows SDK installed for that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号