• 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.

  • 相关阅读:
    可变长参数
    函数的参数
    函数
    文件操作
    字符编码
    数据类型的内置方法:集合
    数据类型的内置方法:元组
    数据类型的内置方法:字典
    数据类型内置方法:列表
    js对对象引用
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1380324.html
Copyright © 2020-2023  润新知