背景:MFC中一個project App需引用另一個project Dll中的其中一個類的方法。
錯誤信息:
error LNK1120: 1 unresolved externals
error LNK2019: unresolved external symbol "public: static void __cdecl myLog::Test(void)" (?Test@myLog@@SAXXZ) referenced in function "public: void __cdecl CMFCTestDlg::OnBnClickedOk(void)" (?OnBnClickedOk@CMFCTestDlg@@QEAAXXZ)
解決方案:
1.新建一個公用的.h文件,如BasicLib.h
#ifdef BASICLIB_EXPORTS
#define BASICLIB_API __declspec(dllexport)
#else
#define BASICLIB_API __declspec(dllimport)
#endif
也可不創建新.h頭文件,將上面的文字copy至所使用類的.h文件中。
2.在用到的類.h文件中include BasicLib.h文件【若沒有新創建公用頭文件,這步可省略】,將該類標記為BASICLIB_API.
e.g. Class BASICLIB_API MyTest{}
3. Project Dll properties->Configuration Properties –> C/C++ –> Preprocessor –> Preprocessor Definitions
增加:BASICLIB_EXPORTS
4. 重新Build Project Dll,OK.
參考:http://wordpress.wongpakm.com/2014/08/13/linking-a-c-dll-of-a-solution-in-visual-studio-2012/