zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    设计模式之观察者模式
    设计模式之外观模式
    设计模式之模板模式
    设计模式之装饰器模式
    设计模式之代理模式
    .NET常见问题汇总
    使用位运算计算两个整数的加减
    一个程序判断CPU是大端还是小端
    后缀表达式 转 表达式树
    实习一个月的小结
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1380324.html
Copyright © 2011-2022 走看看