user593747
user593747

Reputation: 1514

My first DLL has many compile errors I don't understand

I am attempting to create my 1st Simple DLL. I have a class(that's a singleton class) & a Window Procedure function that I declare in the DLL & want to import in my project later on. My IDE is Microsoft Visual C++ 2010 & my project is a Win32 DLL one & I have used MSVC++ default DLL template(you know how it creates all the default files when I create the project).

But I am getting these compile errors that I dont understand what is wrong?

1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.h(15): error C2059: syntax error : '__declspec(dllimport)'
1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.h(39): error C2065: 'TestWndProc' : undeclared identifier
1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.cpp(7): warning C4273: 'testStaticVar' : inconsistent dll linkage
1> c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.h(21) : see previous definition of 'public: static bool MyTest::TestClass::testStaticVar'
1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.cpp(7): error C2491: 'MyTest::TestClass::testStaticVar' : definition of dllimport static data member not allowed
1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.cpp(8): warning C4273: 'instance' : inconsistent dll linkage
1> c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.h(35) : see previous definition of 'private: static MyTest::TestClass * MyTest::TestClass::instance'
1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.cpp(8): error C2491: 'MyTest::TestClass::instance' : definition of dllimport static data member not allowed

My Simple header file:

#ifndef DLLTEST_H
#define DLLTEST_H

#include <windows.h>

// This is from a tutorial I am following
#ifdef _CLASSINDLL
#define CLASSINDLL_CLASS_DECL __declspec(dllexport)
#else
#define CLASSINDLL_CLASS_DECL __declspec(dllimport)
#endif

namespace MyTest
{
    LRESULT CALLBACK CLASSINDLL_CLASS_DECL TestWndProc( HWND hwnd, UINT msg, LPARAM lParam, WPARAM wParam );

    class CLASSINDLL_CLASS_DECL TestClass
    {
        // Singleton class
        public:
            static bool testStaticVar;

            static TestClass* getInstance()
            {
                if ( instance == NULL ) { instance = new TestClass(); }
                return instance;
            }

            void add()
            {
                myMember++;
            }

        private:
            static TestClass* instance;
            WNDPROC myProc;
            int myMember;

            TestClass() : myMember(0) { myProc = (WNDPROC)&TestWndProc; }
            ~TestClass()              {}

    };
}

#endif // DLLTEST_H

My simple cpp file:

#include "stdafx.h"
#include "DLLTest.h"

namespace MyTest
{
    // Create/Initialise? Class Static variables
    bool TestClass::testStaticVar = false;
    TestClass* TestClass::instance = NULL;

    LRESULT CALLBACK TestWndProc( HWND hwnd, UINT msg, LPARAM lParam, WPARAM wParam )
    {
        switch (msg)
        {
            case WM_CREATE:
            {

            }
            break;
            default:
            break;
        }

        return DefWindowProc(hwnd, msg, wParam, lParam);
    }

}

Upvotes: 1

Views: 906

Answers (1)

Karel Petranek
Karel Petranek

Reputation: 15154

I think you are missing _CLASSINDLL preprocessor definition. Add it in Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions.

Upvotes: 0

Related Questions