BeeLabeille
BeeLabeille

Reputation: 184

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. I have a header file "SerialDll.h" which is included in my c++ source file "SerialDll.cpp". When I try to build the solution in visual studio 2012, i get the errors below:

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

When I try implementing John Zwinck's Solution, this is the error i get:

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

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:

#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!

Upvotes: 0

Views: 2562

Answers (2)

Shree Kumar
Shree Kumar

Reputation: 46

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

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249642

The answer is exactly the same as this answer to the previous question you linked: 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;
}

Upvotes: 4

Related Questions