0

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!

Community
  • 1
  • 1
BeeLabeille
  • 174
  • 1
  • 4
  • 16
  • You've already been told the answer (twice) yet the code above does not include those answers. Why not include what you did in your attempts at the answer you were given, obviously you did it wrong some how. – john Oct 11 '13 at 11:37
  • There are two issues here, the first is the C++ language issue, which is that you haven't defined `MySerial::MySerialPort::serial_port_handle`, you have only declared it. You have been told the answer to this several times (see John Zwinck's answer). The other issue is the attributes on the symbols `SERIAL_DLL_API` etc. I don't know the answer to this, maybe what Shree Kumar says is correct, I don't know. But I do know you have to get both things right. So implement John Zwinck's answer and then take it from there. – john Oct 11 '13 at 11:49
  • Hi @john the reason why i didn't include my attempts at those answers is because they didn't work for me and i wanted to present my code as is. You can see in my reply to John Zwinck's question that I get another error when I try his solution. – BeeLabeille Oct 14 '13 at 08:25
  • Like I said there are two problems here. John Zwinck's answer is the correct answer to the first problem. Now you have to try and solve the second problem. So don't reject John Zwinck's answer because it isn't the whole solution, it's still part of the solution. – john Oct 14 '13 at 09:22

2 Answers2

4

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;
}
Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

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();
    };
}
  • Hi @Shree, I am actually following the example from this link [link](http://msdn.microsoft.com/en-us/library/vstudio/ms235636.aspx) I have followed your suggestion but unfortunately and I still get the same error. – BeeLabeille Oct 14 '13 at 08:14
  • Hi @shree, indeed you were correct. The key was to omit 'SERIAL_DLL_API' from the member declarations in the header file and implement it on the whole class. The reason your solution didn't work the first time is because according to this [link](http://msdn.microsoft.com/en-us/library/vstudio/ms235636.aspx) I assumed that 'SERIAL_DLL_EXPORTS' will be defined by default during the build process but it wasn't so i explicitly defined it in my .cpp file and it just worked. Thanks a bunch! – BeeLabeille Oct 14 '13 at 11:01
  • Edit: When i exported the class as a whole as in your example, I still got the same error. So the key was to omit 'SERIAL_DLL_API' only from the declaration of serial_port_handle in the header file but leave the other declarations as before. Not forgetting the explicit definition of 'SERIAL_DLL_EXPORTS' in the .cpp file. THANKS AGAIN – BeeLabeille Oct 14 '13 at 11:29
  • @user2867708 glad you found it useful! – Shree Kumar Dec 06 '13 at 06:54