注意#include

LNK2001 是要鏈接的目標沒有找到,他管你是不是庫。你在 main() 裏只寫一個 test(); 肯定報這個錯,除非你自己寫個 test() 函數。initguid.h 中定義了一個宏,其他的頭文件裏如果遇到了這個宏,則會對某個 CLSID 進行定義,否則就只是聲明。你只有聲明沒有定義當然會鏈接找不到。不過好幾個定義也不行,鏈接器不知道該連接哪個。所以,一個工程裏,如果有多個 initguid.h 的包含,也會出問題。
-------------------------------------------
另一篇:
在沒有包含Initguid.h的地方,DEFINE_GUID宏創建外部引用來使用GUID值,在包含Initguid.h的地方,DEFINE_GUID重定義DEFINE_GUID宏以產生GUID的定義。
如是沒有在任何地方添加 Initguid.h,你會得到一個鏈接錯誤:"unresolved external symbol." ,如果同樣的GUID包含Initguid.h兩次,會得到編譯錯誤"redefinition; multiple initialization."要解決這些問題,請確認Initguid.h只包含一次。同樣的,不要包含Initguid.h到預編譯頭文件中去,因爲預編譯頭文件會被每個源文件包含。
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/lxiongh/archive/2010/03/05/5350329.aspx
========================
另一篇:
// Src1.cpp #include #include "MyGuids.h" // Src2.cpp #include "MyGuids.h" // Src3.cpp #include "MyGuids.h" 其中在MyGuids.h必須使用 DEFINE_GUID(CLSID_MyObject, 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); (Where this example has zeroes, put the actual GUID values.) You can use the Guidgen.exe utility to create a new GUID and paste it 其中initguid.h只能在一個.cpp中包含, DEFINE_GUID()宏定義了,如果包含了一個initguid.h,那麼定義一個guid, 如果沒有包含一個initguid.h ,則定義一個extern guid指向定義的那個guid, 所以項目中必須有一個文件.cpp包含initguid.h,否則會出錯。但如果包含了多個的initguid.h,也會出錯
*******************************************8
 問:

What library needs to be included for the PKEY_AudioEngine_DeviceFormat symbol to be recognized? I added some code to the WinAudio sample to try and get the PKEY_AudioEngine_DeviceFormat (for exclusive mode format negotiation) and I encounter a link error:

     error LNK2001: unresolved external symbol _PKEY_AudioEngine_DeviceFormat

回答:

It's defined via an INITGUID-style mechanism in mmdeviceapi.h

#include <initguid.h> // if this file is included
#include <mmdeviceapi.h> // then the GUIDs end up in this .obj file
回覆:
Thank you. Doing this, and making sure that the GUID got initialized in only one source file (to avoid LNK2005 errors) fixed my problem.
發佈了5 篇原創文章 · 獲贊 5 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章