wxwidgets dll example Topic is solved

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

wxwidgets dll example

Post by rafae11 » Mon Oct 23, 2017 5:35 pm

I am using visual studio 2015 with wxwidgets 3.1.0
I have a main project with a library and a unit test project.
basically I would like to use wxwidgets in the library and compile it as a dll which both the main project and unit test will use.
when i am using stl i can export the dll no problem.
I am having difficulty compiling my library project when i start including wxwidgets in the source code.
could somebody show please direct me as to where to find some examples on how to do this if there are any existing for wxwidgts 3.1.0
thanks.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxwidgets dll example

Post by PB » Mon Oct 23, 2017 7:38 pm

From your post it is unclear what is the acual issue?

I am not sure I understand: all you need is to link wxWidgets dynamically? This is supported out of the box, just build wxWidgets as DLL, link the import library, and define WXUSINGDLL.

VC 2015 solutions (*_vc14.sln) for samples have DLL configurations for both Release and Debug builds so you check those to see how it works.

rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

Re: wxwidgets dll example

Post by rafae11 » Mon Oct 23, 2017 10:03 pm

originally i have the one application that uses wxwidgets and its been working fine.
i would like to split my solution into 3 projects.
a main project , a library , and a unit test project.

the sample code with the mathlibrary i can use with my unit test fine.
could you show me an example for the wxwidgets case.

Code: Select all

// MathLibrary.h - Contains declaration of Function class  
#pragma once  

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

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

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

        // Returns a + (a * b)  
        static MATHLIBRARY_API double AddMultiply(double a, double b);  
    };  
}  
how do i export the class below as a dll?
what do i put in the preprocessor?

Code: Select all

class output
{
    public:
	XXXXX wxString hello()
	{
		return wxString("hello world");
	};
}

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxwidgets dll example

Post by PB » Tue Oct 24, 2017 5:55 am

Now I am legitimately confused. AFAIK, you do not need to do anything for a wxWidgets class such as wxString, it is done by
1. wxWidgets declaring all classes with WXDLLIMPEXP_* macro (basically a sophisticated version of your MATHLIBRARY_API).
2. You defining WXUSINGDLL macro in your project when linking with wxWidgets DLL.

If you want to export your own class, you should do the same. For example, this is how export/import macro looks in my wxAutoExcel library

Code: Select all

#ifdef WXMAKINGDLL_WXAUTOEXCEL
    #define WXDLLIMPEXP_WXAUTOEXCEL                  WXEXPORT
    #define WXDLLIMPEXP_DATA_WXAUTOEXCEL(type)       WXEXPORT type
#elif defined(WXUSINGDLL_WXAUTOEXCEL)
    #define WXDLLIMPEXP_WXAUTOEXCEL                  WXIMPORT
    #define WXDLLIMPEXP_DATA_WXAUTOEXCEL(type)       WXIMPORT type
#else // not making nor using DLL
    #define WXDLLIMPEXP_WXAUTOEXCEL
    #define WXDLLIMPEXP_DATA_WXAUTOEXCEL(type)	    type
#endif
WXDLLIMPEXP_DATA_WXAUTOEXCEL is used for declaring variables to be exported. You may also need to create a macro for forward class declarations, similar to WXDLLIMPEXP_FWD_*

And the class itself can be declared as

Code: Select all

class WXDLLIMPEXP_WXAUTOEXCEL wxExcelApplication : public wxExcelRangeOwner
The individual class methods do not require you to do tag them.

Of course, WXEXPORT and WXIMPORT macros are for MSVC defined as

Code: Select all

#define WXEXPORT __declspec(dllexport)
#define WXIMPORT __declspec(dllimport) 
You could also take a look at how it is done in one of popular 3rd party wxWidgets libraries, such as wxSQLite but it is basically always more or less the same...

rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

Re: wxwidgets dll example

Post by rafae11 » Tue Oct 24, 2017 9:58 am

thanks for the input i narrowed my problem down.

this is what i am using now

Code: Select all

#pragma once
#include <wx/wx.h>

#ifdef CONTROLLERLIBRARY_EXPORTS  
#define CONTROLLERLIBRARY_EXPORT __declspec(dllexport)   
#else  
#define CONTROLLERLIBRARY_EXPORT __declspec(dllimport)   
#endif  

class CONTROLLERLIBRARY_EXPORT Controller
{	
	public:
	wxString hello();
};
In environment i added the code below

Code: Select all

PATH=$(WX31)\lib\vc140_x64_dll;
but the unit test project doesn't pick up the path.
i copied all the dlls needed to the unit test executable path and it is working now.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxwidgets dll example

Post by PB » Tue Oct 24, 2017 10:28 am

rafae11 wrote: In environment i added the code below

Code: Select all

PATH=$(WX31)\lib\vc140_x64_dll;
If you want to refer to a system environment variable such as PATH on a command line, you need to use the %var_name% syntax, e.g.

Code: Select all

PATH=%WX31%\lib\vc140_x64_dll;%PATH%

Post Reply