How to initialize GUID

For the details on how to initialize a GUID, check this page http://support.microsoft.com/kb/130869. (definitely we are using a new version compiler! So get to use #include<initguid.h>)

Summary of a problem I met and finally find the above solution to totally undertand the case:

Some cpp in our project uses below #include to intialize some GUID:

#include<XX.\..somename.h>

#include<XX..\..somename.c> // it includes the CLSID variable like const IID xx = {000..-}

It's wrong way to initialize a GUID. Because by directly including the CLSID variable, if many cpps need the GUID and #include this way, link error:

error LNK2005: xx object already defined in xxx.obj.

Oh, my!

The correct way to initialize a GUID is to #include<initguid.h> before any #include to use a GUID. By a macro DECLSPEC_SELECTANY keyword is used in the DEFINE_GUID macro, which makes sure that the linker will correctly handle this multiple definition.

Note more:

If two cpp in one project #include<test.h>, which includes below:

#include "StdAfx.h"

int pp = 100;
class CTest
{

};

Building the project, we will recevie one link error:

error LNK2005: "int pp" (?pp@@3HA) already defined

But the class definition will pass. So the conclusion is: if more than one cpps #include a header file, which happens to define some variable like "int ..", link error will be reported while the class definition is ok to use.

posted @ 2009-01-23 11:56  能巴  阅读(232)  评论(0编辑  收藏  举报