Good day! There was an ordinary human dll with one function, everything worked. I wrapped the function in a class, made the static method just in case, and now nothing works, is it possible to call the class method from dlls at all, and how, and whether it can be non-static, now on the working source code:

.cpp

#include <iostream> #include <windows.h> using namespace std; class F{ public: static int foo(int i){ return i + 1; } }; 

.def

 LIBRARY "DLL" EXPORTS F::foo @1 

Here he writes that: unresolved external symbol F :: foo. Thanks for attention!

  • Try specifying __declspec (dllexport) or __declspec (dllimport) after the word class. - manking
  • He added, but he still swears to write in the .def file. - fortunado
  • and it does not need to be specified, because there is a .def file - fortunado

1 answer 1

Good day! I think you can try the following method: you need two projects - the first with a library, the second with a test program (exe). We add two files to the library project: dllclass.h and dllclass.cpp, here is their implementation:

 // dllclass.h #ifdef MAKE_DLL #define CLASS_EXPORT __declspec(dllexport) #else #define CLASS_EXPORT __declspec(dllimport) #endif class F{ public: static int foo(int i); }; // dllclass.cpp int F::foo(int i) { return i + 1; } 

Next, in the compiler options, add the preprocessor directive (predefined): MAKE_DLL

The magic is that when you compile a project with a library, the class will be declared as exported , in all other cases it will be imported (see the preprocessor directives in dllclass.h).

Now you need to write a library testing program. In the project with the application, create the file main.cpp and write there something like the following:

 // main.cpp #include <dllclass.h> #include <iostream> int main(int argc, char* argv[]) { using namespace std; cout << F::foo(3) << endl; return 0; } 

Important: before compiling, you need to specify in the options the paths to the header files of your library and the * .lib file!

Try it, it should help!