Compile wxWidgets source using __stdcall instead of __cdecl on Windows

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
Sridhar@Work
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 221
Joined: Fri Jan 08, 2021 8:19 am

Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by Sridhar@Work »

Hi

We have all our source code compiled using _stdcall calling convention. We are planning to implement wxWidgets dll dialog code in our source. This requires wxWidgets dependent libraries to be compiled using stdcall calling convention instead of _cdecl calling convension.

When we tried to compile the source on windows using "nmake /f makefile.vc BUILD=release SHARED=1 TARGET_CPU=X86", it is creating the dependent libraries using cdecl calling convention (by default) on windows and we are not able to link them with our wxWidgets dll code (compiled using stdcall). It is giving linker errors as our code is compiled using stdcall and wxWidgets src is compiled in cdecl.

What is best approach to compile the wxWidgets code using stdcall convention. Is there a easy way to use the stdcalls with wxWidgets code?

I tried compiling the source using "nmake /f makefile.vc BUILD=release SHARED=1 TARGET_CPU=X86 CPPFLAGS="/Gz"" but it is givining linker error whilst compiling the code.

did anyone try this? Need some quick help. Thanks in advance.

Regards
Sree
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by ONEEYEMAN »

Hi,
Why do you need to use _cdecl?
Since you are on Windows - did you look at the dll sample?
I myself am using windows and building dll with default wxwidgets build...

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by doublemax »

Shouldn't it be enough to use _stdcall only for the exported functions of your DLL?
Use the source, Luke!
Sridhar@Work
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 221
Joined: Fri Jan 08, 2021 8:19 am

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by Sridhar@Work »

Why do you need to use _cdecl?
Sree>> When we compile the wxWidget source using "nmake /f makefile.vc BUILD=release SHARED=1 TARGET_CPU=X86" on windows, it is using _cdecl by default and not _stdcall. So how do we compile the source using _stdcall calling convention?

Since you are on Windows - did you look at the dll sample?
Sree>> Yes, the dll sample is compiled using _cdecl and not _stdcall. Since our dependent code and libraries are compiled using _stdcall, we also need to compile the dll sample in stdcall and it is giving linker erros as wxWidgets code is compiled using _cdecl.

I myself am using windows and building dll with default wxwidgets build...
Sree>> You might be using _cdecl on windows and hence there is no issue with linking your code. Please cross check.

Thank you for the response.
Sridhar@Work
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 221
Joined: Fri Jan 08, 2021 8:19 am

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by Sridhar@Work »

Shouldn't it be enough to use _stdcall only for the exported functions of your DLL?
Sree>> My wxWidget dll code has to link wxWidget libraries (which are compiled using _cdecl) and also our internal dll libraries (which are compiled using _stdcall). So my wxWidget dll code has to be compiled using _stdcall so that it can work with wxWidgets and also our internal code.

This means, I have to compile the wxWidgets source using _stdcall instead of _cdecl. So is there a easy way to do it.

Note: I cannot use "extern __stdcall" for only the calls which are referenced in wxWidgets because my wxwidget dll is linking lot wxWidget libraries and I might end up using extern for all most all of the funtions exported in wxWidgets core libraries. So better approach would be to compile wxWidget source using _stdcall (I assume?).

Please suggest the best approach. Thanks
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by doublemax »

There is no built-in mechanism, configure switch etc. to change the calling convention. But i'm still not convinced you actually need to change anything, if your app only communicates with the DLL should its exported functions.

But i hardly ever use DLLs, so i don't know too much about this topic.

Maybe try asking on the wx-users Google group. https://groups.google.com/g/wx-users
Use the source, Luke!
Sridhar@Work
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 221
Joined: Fri Jan 08, 2021 8:19 am

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by Sridhar@Work »

Our dll is compiled using stdcall calling convention (i.e. with /Gz compiler flag in visual studio compiler). WxWidget source is by default compiled using cdecl calling convention(i.e with /Gr compiler flag in visual studio). So the linker cannot link the wxWidget libraries when referred from our code which is compiled using cdecl calling convention . It cannot find the exported signatures. I hope you understood the issue.

We get the following errors:
{{{
1>ui_wx_signon.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class wxString __stdcall wxString::FromAscii(char const *)" (__imp_?FromAscii@wxString@@SG?AV1@PBD@Z) referenced in function "public: __thiscall CustomDialog::CustomDialog(class wxWindow *,int,class wxString const &,class wxPoint const &,class wxSize const &,long)" (??0CustomDialog@@QAE@PAVwxWindow@@HABVwxString@@ABVwxPoint@@ABVwxSize@@J@Z)
1>ui_wx_signon_wrapper.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static class wxString __stdcall wxString::FromAscii(char const *)" (__imp_?FromAscii@wxString@@SG?AV1@PBD@Z)
1>ui_wx_signon.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __stdcall wxMessageBox(class wxString const &,class wxString const &,long,class wxWindow *,int,int)" (__imp_?wxMessageBox@@YGHABVwxString@@0JPAVwxWindow@@HH@Z) referenced in function "protected: virtual void __thiscall CustomDialog::m_sdbSizer1OnOKButtonClick(class wxCommandEvent &)" (?m_sdbSizer1OnOKButtonClick@CustomDialog@@MAEXAAVwxCommandEvent@@@Z)
1>ui_wx_signon_wrapper.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) int __stdcall wxMessageBox(class wxString const &,class wxString const &,long,class wxWindow *,int,int)" (__imp_?wxMessageBox@@YGHABVwxString@@0JPAVwxWindow@@HH@Z)
1>ui_wx_signon.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) class wxString __stdcall operator+(class wxString const &,class wxString const &)" (__imp_??H@YG?AVwxString@@ABV0@0@Z) referenced in function "protected: virtual void __thiscall CustomDialog::m_sdbSizer1OnOKButtonClick(class wxCommandEvent &)" (?m_sdbSizer1OnOKButtonClick@CustomDialog@@MAEXAAVwxCommandEvent@@@Z)
}}}
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by PB »

Disclaimer: I cannot say I really understand the issue (changing the calling convention being a best solution), so I may be missing something.

All wxWidgets classes (e.g., wxString) and functions (e.g., wxSplit) exported from wxWidgets DLLs are declared with WXDLLIMPEXP_<library> macro (e.g., WXDLLIMPEXP_BASE). For example,

Code: Select all

class WXDLLIMPEXP_BASE wxString
or

Code: Select all

WXDLLIMPEXP_BASE wxArrayString wxSplit
When building wxWidgets DLL WXDLLIMPEXP_<library> basically expands to WXEXPORT, when linking to wxWidgets it expands to WXIMPORT instead. These are with MSVC set to

Code: Select all

#define WXEXPORT __declspec(dllexport)
#define WXIMPORT __declspec(dllimport) 
Do you mean that you want change the calling convention for this?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by ONEEYEMAN »

Hi,
What is the reason your software is built using "wrong" calling convention?

Do you use some 3rd party closed source DLL that link this way? Or you just like it?

Or maybe you can build a DLL yourself and just load it dynamically and then call a function in that DLL?

Please explain the reasoning.

Thank you.
stahta01
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 548
Joined: Fri Nov 03, 2006 2:00 pm

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by stahta01 »

Sridhar@Work wrote: Mon Jan 18, 2021 2:28 pm I tried compiling the source using "nmake /f makefile.vc BUILD=release SHARED=1 TARGET_CPU=X86 CPPFLAGS="/Gz"" but it is givining linker error whilst compiling the code.
Please post the wxWidgets version?
Please post the error message you got when you compile wxWidgets?
Edit: Your code that uses wxWidgets need to be compatible with the wxWidgets libraries you use!
In other words, you can not do some build related changes in only your code without doing compatible changes to wxWidgets!

Tim S.
Sridhar@Work
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 221
Joined: Fri Jan 08, 2021 8:19 am

Re: Compile wxWidgets source using __stdcall instead of __cdecl on Windows

Post by Sridhar@Work »

Hi

We are using wxWidgets-3.1.4 verison.

We changed our wxWidgets dll to build using cdecl and exported our internal library code functions to use stdcall using extern "c". It fixed the issue for us.

Resolution:
1. wxWidgets source can be built using cdecl calling convention as done.
2. Our wxWidgets dll is built using cdecl calling convention.
3. Our internal libraries are built using stdcall calling convention.
4. Our internal libraries are linked with our wxWidgets dll as cdecl calling convention. Any references to our exported functions are referred using stdcall and any references from our code to wxWidget dll are referred using cdecl explictly to fix the issue.

Thank you for the support.

Regards
Sree
Post Reply