How to avoid error "LNK2001 unresolved external" by using DEFINE_GUID

原文:http://support.microsoft.com/kb/130869

In Microsoft Visual C++, if the version of the compiler is older than a specific version, a GUID must be initialized exactly once. For this reason, there are two different versions of the DEFINE_GUID macro. One version just declares an external reference to the symbol name. The other version actually initializes the symbol name to the value of the GUID. If you receive an LNK2001 error for the symbol name of the GUID, the GUID was not initialized.
在VC中,如果编译器的版本比某个特定版本老,一个GUID的被初始化只能而且必须执行一次。由于这个原因,有两个版本的DEFINE_GUID宏。一个版本只是声明对符号名的外部引用;另一个版本会初始化这个符号名为其GUID值。如果碰到GUID的符号名的LNK2001错误,是因为GUID没有被初始化。
Note The DEFINE_GUID macro in the guiddef.h header file declares a GUID. To also define the GUID, include the INITGUID.H header file in the source file where the GUID should be defined.

You can make sure your GUID gets initialized in one of two ways:

  • If you are using precompiled header files, include the INITGUID.H header file before defining the GUID in the implementation file where it should be initialized. (AppWizard-generated MFC projects use precompiled headers by default.) 如果使用预先编译的头文件,实现文件中在定义GUID之前,包含INITGUID.h头文件。
  • If you are not using precompiled headers, define INITGUID before including OBJBASE.H. (OBJBASE.H is included by OLE2.H.)如果没有使用预先编译的头文件,在包含OBJBASE.h前定义INITGUID。

Note If the version of the compiler is newer than a specific version, when you are using precompiled headers, you can include the INITGUID.H header file in a single header file that includes other header files. This will cause the GUID to be defined in all the source files where this header file is included. You will not receive an LNK2001 error even if a symbol is defined multiple times. This is because a DECLSPEC_SELECTANY keyword is used in the DEFINE_GUID macro. This keyword makes sure that the linker will correctly handle this multiple definition.

 

你可能感兴趣的:(extern)