Multiple CPP files, single common custom event Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
RealityMage
Experienced Solver
Experienced Solver
Posts: 75
Joined: Tue Aug 23, 2005 3:47 am

Multiple CPP files, single common custom event

Post by RealityMage »

How can I do this? On wxGTK, I simply DECLARE_EVENT_TYPE is one header file, and IMPLEMENT_EVENT_TYPE in a different implementation file. I then include the header in every file I want to use the custom event in.

For wxWIN, I get linker errors - like this.

Code: Select all

obj/acctdiag.o(.text+0x1bc6):acctdiag.cpp: undefined reference to `_imp__wx_CEVT_CONNECTION_STATE'
obj/acctdiag.o(.text+0x345a):acctdiag.cpp: undefined reference to `_imp__wx_CEVT_CONNECTION_STATE'
obj/acctdiag.o(.text+0x41ed):acctdiag.cpp: undefined reference to `_imp__wx_CEVT_CONNECTION_STATE'
clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 »

Have you also included the header in the *.cpp of your event-implementation?

For your event-header something like:

Code: Select all

// myevent.h

#ifndef MY_EVENT_H
#define MY_EVENT_H

#include <wx/wxprec.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP

#include <wx/wx.h>
#endif


#include <wx/event.h>

DECLARE_EVENT_TYPE(MY_EVENT, -1)

#endif
and for the implementation:

Code: Select all

// myevent.cpp

#include "myevent.h"

DEFINE_EVENT_TYPE(MY_EVENT)
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Have you included the event cpp file in your linker options? When the file is not added as cpp file to create an object from, it will not do that and fail at linker time.

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
micros
Super wx Problem Solver
Super wx Problem Solver
Posts: 317
Joined: Sat Mar 18, 2006 10:41 am
Location: Ustek, Bohemia

Post by micros »

You need DECLARE_LOCAL_EVENT_TYPE and DEFINE_LOCAL_EVENT_TYPE. There's no difference between LOCAL and non-LOCAL, until you use wxWidgets DLL. It should be mentioned in docs that one probably wants LOCAL when declaring custom event types.
clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 »

DECLARE_LOCAL_... what? I'm using wxWidgets DLL's. And I want to port my code later to Linux. I just use the "non-LOCAL" event declaration. Could you give me some more information about the "LOCAL" and "non-LOCAL" stuff? On what do I have to pay attention?
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp
RealityMage
Experienced Solver
Experienced Solver
Posts: 75
Joined: Tue Aug 23, 2005 3:47 am

Post by RealityMage »

Yes, I have included the header file in the implementation, and have the implementation linked in.

I suspect the "LOCAL" difference is the case. As to the last poster, you could write portable code by testing the WXUSINGDLL definition (or something like that), and using preprocessor commands to conditionally execute different macros.
micros
Super wx Problem Solver
Super wx Problem Solver
Posts: 317
Joined: Sat Mar 18, 2006 10:41 am
Location: Ustek, Bohemia

Post by micros »

RealityMage wrote:As to the last poster, you could write portable code by testing the WXUSINGDLL definition (or something like that), and using preprocessor commands to conditionally execute different macros.
Not necessary. You can write portable code by just using DECLARE_LOCAL_EVENT_TYPE for all event types declared and defined by the app (by which I mean in one of statically-linked units). Luckily all the quirks only apply to DECLARE macros; DEFINE_LOCAL_EVENT_TYPE is (currently) just a synonym for the non-LOCAL.

The non-LOCAL is used for events residing inside wxWidgets library -- and it makes a difference only when you're building/using wx.dll. If building, it declares the event type __declspec(dllexport); if using, it's __declspec(dllimport). In all other cases (includes linking against wx static lib), it's the same as LOCAL, that is no __declspec. I really don't know why there are non-LOCALs in the docs' examples, and I think it's wrong. Probably a left-over from the days when there was no need for LOCALs.

If you plan to have a custom DLL exporting some custom event types, you'll need your own MYDECLSPEC (defined conditionally based on whether your dll is being built/used like in the previous paragraph; you can use WXEXPORT and WXIMPORT macros from "wx/dlimpexp.h" to simplify it a bit and avoid the ugly __declspec). Then put DECLARE_EXPORTED_EVENT_TYPE(MYDECLSPEC, MYEVENT, -1) in your dll's interface header instead of the ordinary. Each such dll must have it's own declspec #define, just like the above-mentioned dlimpexp.h defines WXMAKINGDLL_BASE, WXMAKINGDLL_NET, WXMAKINGDLL_CORE etc. for building wxWidgets broken into multiple dlls.
basos
Earned a small fee
Earned a small fee
Posts: 18
Joined: Wed Aug 20, 2008 4:52 pm
Location: greexe

Confirmed

Post by basos »

I also had the same problem. With DECLARE_EVENT_TYPE link problems occured __imp_wxEVT .... on MSW build only (and not on GTK ). The case was than one module (say a.o) was handling an event that was defined on b.o.

When DECLARE_LOCAL_EVENT_TYPE was used things worked fine.
Post Reply