Calling a statically linked static method in C ++

I am trying to call a statically linked static method of a C ++ class, but I am getting VS LNK2019 linker error, "unresolved external symbol". Here's the library source:

// in header file
#define DllExport __declspec (dllexport)
class MyClass{
public:
    DllExport
    static HWND WINAPI myFunc();
};
// in cpp file
DllExport
HWND WINAPI MyClass::myFunc(){ /* create a GUI window that has instance of MyClass set as its property (using ::SetProp) */ }

      

myFunc should serve as the entry point for creating MyClass objects that are in the hidden library. Only such static functions can be used to influence the functionality of the MyClass instance (by providing an appropriate HWND). Here's the consumer of the library:

#define DllImport __declspec(dllimport)
DllImport
HWND WINAPI myFunc();
...
int main(){
    HWND hWnd=myFunc();
    ... // work with the window and attached MyClass instance
}

      

(I believe) all file links are set correctly - originally myFunc was designed as a standalone function and everything worked fine. I suspect it must be a misaligned call mismatch that is causing the linker to throw an error on myFunc. Read several articles on this topic, namely

http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL

and

https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx

but they didn't solve my problem.

Thanks for the offer!

+3


source to share


2 answers


Since your goal is to create a static library, the first thing we want to do is remove the mention dllexport/dllimport

. Such specifiers are only used when you are actually creating a DLL project.

So, for lib.h

we only need this (with some of them added defenders for a good score):

// lib.h
#ifndef LIB_H
#define LIB_H

class MyClass{
public:
    static void myFunc();
};

#endif

      

The spec is WINAPI

also unnecessary as you are the one calling this method and can just use the default convention with no problems with the ABI (although if you want to use anyway WINAPI

, then you need to include <windows.h>

in your header file).

For lib.cpp

we only need the following:



// lib.cpp
#include <Windows.h>
#include "lib.h"

void MyClass::myFunc(){
    ::MessageBox(0,"myFunc call!",NULL,0);
}

      

For main.cpp

in your project app

we only need the following:

// main.cpp
#include <iostream>
#include <D:\staticlink\lib\lib.h>

int main(){
    std::cout << "ahoj";
    MyClass::myFunc();
    char buf[10];
    std::cin >> buf;
    return 0;
}

      

I would recommend lib.h

setting up your include paths to find through your project options rather than using absolute paths in your source code, but maybe you can do that later after everything works.

After that, if the problem persists, you only need to make sure your project app

is linked correctly to lib.lib

(linker settings).

+3


source


Your import header file should look bigger:



#define DllApi __declspec (dllexport)
class MyClass{
public:
    DllApi
    static HWND WINAPI myFunc();
};

      

+1


source







All Articles