简体   繁体   English

C ++ DLL项目中无法解析的外部符号

[英]unresolved external symbol in C++ DLL project

can I just start by saying I appreciate you taking your time to go through my question and attempting to help. 我可以首先说声谢谢,请您抽出宝贵的时间回答我的问题并尝试提供帮助。 However I have already attempted the solution suggested on here and on here and they haven't worked for me. 但是,我已经尝试过这里这里建议的解决方案,但它们并没有为我工作。

This is my problem: I am attempting to create a serial port class as a VS12 DLL project. 这是我的问题:我正在尝试将串行端口类创建为VS12 DLL项目。 I have a header file "SerialDll.h" which is included in my c++ source file "SerialDll.cpp". 我有一个头文件“ SerialDll.h”,它包含在我的c ++源文件“ SerialDll.cpp”中。 When I try to build the solution in visual studio 2012, i get the errors below: 当我尝试在Visual Studio 2012中构建解决方案时,出现以下错误:

Error 11 error LNK1120: 1 unresolved externals C:\\Sprint 7\\SerialDll\\Debug\\SerialDll.dll 1 1 SerialDll Error 10 error LNK2001: unresolved external symbol "__declspec(dllimport) private: static void * MySerial::MySerialPort::serial_port_handle" (__imp_?serial_port_handle@MySerialPort@MySerial@@0PAXA) C:\\Sprint 7\\SerialDll\\SerialDll\\SerialDll.obj SerialDll 错误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

When I try implementing John Zwinck's Solution, this is the error i get: 当我尝试实施John Zwinck的解决方案时,这是我得到的错误:

Error 2 error C2491: 'MySerial::MySerialPort::serial_port_handle' : definition of dllimport static data member not allowed c:\\sprint 7\\serialdll\\serialdll\\serialdll.cpp 16 1 SerialDll 错误2错误C2491:'MySerial :: MySerialPort :: serial_port_handle':不允许定义dllimport静态数据成员c:\\ sprint 7 \\ serialdll \\ serialdll \\ serialdll.cpp 16 1 SerialDll

This is the code in my header file: 这是我的头文件中的代码:

#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();
    };
}

This is the code in my c++ source file, with John Zwinck's solution: 这是约翰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;
    }
}

Hope you guys can help me with a solution or at least refer me to a link with a working solution. 希望你们能为我提供解决方案,或者至少将我引向具有有效解决方案的链接。

Cheers! 干杯!

The answer is exactly the same as this answer to the previous question you linked: https://stackoverflow.com/a/17902142/4323 答案与您链接的上一个问题的答案完全相同: https : //stackoverflow.com/a/17902142/4323

That is, you have only declared, but not allocated storage for, your static member. 也就是说,您仅声明了静态成员,但未为其分配静态存储。 You need to add this to your implementation file: 您需要将此添加到您的实现文件中:

namespace MySerial
{
    HANDLE MySerialPort::serial_port_handle;
}

If you are looking to export the class outside the DLL, then you need to use __declspec for the class, and not for each member function/variable. 如果要在DLL外部导出类,则需要对类使用__declspec,而不是对每个成员函数/变量使用__declspec。 (See http://msdn.microsoft.com/en-us//library/a90k134d.aspx ) (请参阅http://msdn.microsoft.com/en-us//library/a90k134d.aspx

Your header file needs to look like : 您的头文件需要如下所示:

namespace MySerial
{
    class SERIAL_DLL_API MySerialPort
    {
        private:
            static HANDLE serial_port_handle;
        public:
            MySerialPort();
            ~MySerialPort();
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM