2016-07-09 94 views
0

我對Visual Studio並不陌生,我知道這方面有很多問題。但我真的不知道這一點。這是錯誤:Visual Studio 2015中無法解析的外部符號鏈接器錯誤

1>moc_displaycounter.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const Counter::staticMetaObject" ([email protected]@@[email protected]@B) 
1>moc_displaymanometer.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const ManoMeter::staticMetaObject" ([email protected]@@[email protected]@B) 
1>moc_displaysvgmeter.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const SVGMeter::staticMetaObject" ([email protected]@@[email protected]@B) 

所以我做了DUMPBIN /上導入庫出口,它返回此:

Microsoft (R) COFF/PE Dumper Version 14.00.24210.0 
Copyright (C) Microsoft Corporation. All rights reserved. 

Dump of file analogwidgets.lib 

File Type: LIBRARY 

    Exports 

     ordinal name 

        [email protected]@[email protected]@@@Z (public: __cdecl AbstractMeter::AbstractMeter(class QWidget *)) 
        [email protected]@@[email protected]@B (public: static struct QMetaObject const Counter::staticMetaObject) 
        [email protected]@@[email protected]@B (public: static struct QMetaObject const Item::staticMetaObject) 
        [email protected]@@[email protected]@B (public: static struct QMetaObject const Led::staticMetaObject) 
        [email protected]@@[email protected]@B (public: static struct QMetaObject const ManoMeter::staticMetaObject) 
        [email protected]@@[email protected]@B (public: static struct QMetaObject const PotentioMeter::staticMetaObject) 
        [email protected]@@[email protected]@B (public: static struct QMetaObject const SVGMeter::staticMetaObject) 

我檢查了導入庫的確切簽名。對於一個很好的方法,我使用取決於來檢查這些符號是否在DLL中(是的,我知道它在鏈接過程中不起作用,但只是爲了確保導入庫沒有被破壞),但仍然它不會鏈接。嘗試調試和發佈x64版本都沒有成功。以前沒有VS的其他版本的問題。

關於接下來我該做什麼的任何想法?

+0

看起來你忘記了定義一些靜態類變量。 –

+0

當您聲明某些內容但忘記定義它時會發生無法解析的外部符號錯誤。可能是成員函數或靜態變量。 –

回答

2

從DLL導出靜態數據有點棘手。鏈接器錯誤說明你忘了做什麼,在客戶端代碼中使用這些類時,沒有聲明__declspec(dllimport)。攝製代碼:

Header.h:

#ifdef _USRDLL 
#define EXPORTED __declspec(dllexport) 
#else 
#define EXPORTED //__declspec(dllimport)  // <=== Problem here! 
#endif 

struct EXPORTED QMetaObject { 
public: 
    int foo; 
}; 

class EXPORTED Counter { 
public: 
    static const QMetaObject staticMetaObject; 
}; 

Source.cpp:

#include "header.h" 
const QMetaObject Counter::staticMetaObject; 

Client.cpp:

#include "Header.h" 

int main() 
{ 
    auto foo = Counter::staticMetaObject.foo; 
    return 0; 
} 

鏈接錯誤:

ConsoleApplication1.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const Counter::staticMetaObject" ([email protected]@@[email protected]@B)

刪除Header.h中的//註釋以修復,編譯器現在知道如何正確使用指向DLL數據的指針。請注意原始鏈接器錯誤如何告訴您這個問題。如果您現在運行庫上的Dumpbin.exe/exports,您可以看到導出的成員名稱更改爲[email protected]@@[email protected]@B。這是指向數據的指針。指針是必需的,因爲如果無法將DLL加載到其首選基地址,數據可能會重新定位。

相關問題