0

I followed microsoft documentation and stumbled at moment when DLL project should compile like this:

1>------ Build started: Project: MathLibrary, Configuration: Debug Win32 ------  
1>  stdafx.cpp  
1>  dllmain.cpp  
1>  MathLibrary.cpp  
1>     Creating library c:\users\username\documents\visual studio 2015\Projects\MathLibraryAndClient\Debug\MathLibrary.lib and object c:\users\username\documents\visual studio 2015\Projects\MathLibraryAndClient\Debug\MathLibrary.exp  
1>  MathLibrary.vcxproj -> c:\users\username\documents\visual studio 2015\Projects\MathLibraryAndClient\Debug\MathLibrary.dll  
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========  

But i compiling my DLL project successfully but i have less in output. And in a result my main app can't compile because "there is no .lib file of your DLL" as compiler said.

Output of DLL project looks like this:

1>------ Rebuild All started: Project: testDLL, Configuration: Debug Win32 ------
1>stdafx.cpp
1>dllmain.cpp
1>someClass.cpp
1>testDLL.cpp
1>Generating Code...
1>testDLL.vcxproj -> D:\stud\VStest\testDLL\Debug\testDLL.dll
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

My output obviously lacks "Creating library" part. I don't understand what's wrong, my VS is 2017.

P.S. what MS mean by that comment in CPP file?

// Compile by using: cl /EHsc /DMATHLIBRARY_EXPORTS /LD MathLibrary.cpp 
Engelard
  • 137
  • 1
  • 1
  • 7
  • If you don't export symbols you don't get an import library. My guess is you did not do the preprocessor definition step. – drescherjm Aug 20 '18 at 03:13
  • @drescherjm Well, i add to .h file preprocessor stuff as they did, nothing changed. One thing i noticed which i dont have, they have such comment in CPP file of class: // Compile by using: cl /EHsc /DMATHLIBRARY_EXPORTS /LD MathLibrary.cpp i'm completely ignorant about what said in here, i don't even get how to google it. What they want me to do? – Engelard Aug 20 '18 at 16:49
  • So my guess was correct you skipped the preprocessor definition step. defining `MATHLIBRARY_EXPORTS` is essential to make the import library. Without that it will not work since the dll will not export any symbols. In your project settings in Visual Studio look under `c++` for preprocessor definitions. – drescherjm Aug 20 '18 at 16:55

1 Answers1

2

In your DLL project goto

Project Settings -> C/C++ -> Preprocessor -> Preprocessor definitions

And add

MATHLIBRARY_EXPORTS definition

So MATHLIBRARY_API macros

#ifdef MATHLIBRARY_EXPORTS  
#define MATHLIBRARY_API __declspec(dllexport)   
#else  
#define MATHLIBRARY_API __declspec(dllimport)   
#endif  

will be solved to __declspec(dllexport). This will instruct compiler/linker to export function marked with this attribute into DLL export section as well as into import library (if you have the project setting to create import library ).

As well as, you can implement your class as following:

namespace MathLibrary  
{  
    // This class is exported from the MathLibrary.dll  
    class MATHLIBRARY_API Functions  
    {  
    public:  
        // Returns a + b  
        static double Add(double a, double b);  

        // Returns a * b  
        static double Multiply(double a, double b);  

        // Returns a + (a * b)  
        static double AddMultiply(double a, double b);  
    };  
}  

Alternatively you can use def files instead of __declspec

Victor Gubin
  • 2,782
  • 10
  • 24
  • Tnx! But one more crucial thing was strange addition of MATHLIBRARY_API at the beginning of functions declarations. Have no clue what is that, but after i add that, i compile properly as it should. But now it gives me couple errors(not DLL project, project of my main test program which should use that DLL) - https://image.ibb.co/jbSBse/dllfuck.jpg – Engelard Aug 21 '18 at 10:01
  • Check this https://stackoverflow.com/questions/31886670/add-a-dependency-reference-to-your-own-dll-project-in-visual-studio-2013 – Victor Gubin Aug 21 '18 at 10:24
  • Well, i've already add reference to main project, and when i went to project dependencies(after your link) there already was chosen in check-box my dll project(project: main, and check-box for my .dll proj). But i glanced in build order, first stays main project, and second dll, i dont know how to swap them... – Engelard Aug 21 '18 at 13:02
  • I rly can't understand why this error comes to my main project, in build order first build .dll project, then main. What the hell this pop-up error if i press "start debugging" https://image.ibb.co/gP7uJU/fcngmsvs.jpg – Engelard Aug 24 '18 at 13:27
  • My problem was object creation issue. I made my test functions in .dll project static so no object need to create and now it all work! – Engelard Aug 24 '18 at 15:17