[英]unresolved external symbol in C++ DLL project
我可以首先說聲謝謝,請您抽出寶貴的時間回答我的問題並嘗試提供幫助。 但是,我已經嘗試過這里和這里建議的解決方案,但它們並沒有為我工作。
這是我的問題:我正在嘗試將串行端口類創建為VS12 DLL項目。 我有一個頭文件“ SerialDll.h”,它包含在我的c ++源文件“ SerialDll.cpp”中。 當我嘗試在Visual Studio 2012中構建解決方案時,出現以下錯誤:
錯誤11錯誤LNK1120:1未解決的外部C:\\ Sprint 7 \\ SerialDll \\ Debug \\ SerialDll.dll 1 1 SerialDll錯誤10錯誤LNK2001:未解決的外部符號“ __declspec(dllimport)私有:靜態無效* MySerial :: MySerialPort :: serial_port_handle” (__imp_?serial_port_handle @ MySerialPort @ MySerial @@ 0PAXA)C:\\ Sprint 7 \\ SerialDll \\ SerialDll \\ SerialDll.obj SerialDll
當我嘗試實施John Zwinck的解決方案時,這是我得到的錯誤:
錯誤2錯誤C2491:'MySerial :: MySerialPort :: serial_port_handle':不允許定義dllimport靜態數據成員c:\\ sprint 7 \\ serialdll \\ serialdll \\ serialdll.cpp 16 1 SerialDll
這是我的頭文件中的代碼:
#include <Windows.h>
#ifdef SERIAL_DLL_EXPORTS
#define SERIAL_DLL_API __declspec(dllexport)
#else
#define SERIAL_DLL_API __declspec(dllimport)
#endif
namespace MySerial
{
class MySerialPort
{
private:
static SERIAL_DLL_API HANDLE serial_port_handle;
public:
SERIAL_DLL_API MySerialPort();
SERIAL_DLL_API ~MySerialPort();
};
}
這是約翰Zwinck解決方案在我的c ++源文件中的代碼:
#include "stdafx.h"
#include "SerialDll.h"
#include <stdexcept>
#include <iostream>
using namespace std;
namespace MySerial
{
HANDLE MySerialPort::serial_port_handle;
MySerialPort::MySerialPort()
{
serial_port_handle = INVALID_HANDLE_VALUE;
}
MySerialPort::~MySerialPort()
{
if(serial_port_handle != INVALID_HANDLE_VALUE)
{
CloseHandle(serial_port_handle);
}
serial_port_handle = INVALID_HANDLE_VALUE;
}
}
希望你們能為我提供解決方案,或者至少將我引向具有有效解決方案的鏈接。
干杯!
答案與您鏈接的上一個問題的答案完全相同: https : //stackoverflow.com/a/17902142/4323
也就是說,您僅聲明了靜態成員,但未為其分配靜態存儲。 您需要將此添加到您的實現文件中:
namespace MySerial
{
HANDLE MySerialPort::serial_port_handle;
}
如果要在DLL外部導出類,則需要對類使用__declspec,而不是對每個成員函數/變量使用__declspec。 (請參閱http://msdn.microsoft.com/en-us//library/a90k134d.aspx )
您的頭文件需要如下所示:
namespace MySerial
{
class SERIAL_DLL_API MySerialPort
{
private:
static HANDLE serial_port_handle;
public:
MySerialPort();
~MySerialPort();
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.