Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

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
delt
Earned a small fee
Earned a small fee
Posts: 24
Joined: Mon Dec 26, 2016 11:17 pm

Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by delt » Mon Apr 22, 2019 4:15 am

Hello everyone, it's been a while since i posted here.

I'm having a problem compiling my app for windows (using mingw32 in MXE), i get several of the following errors just on the first source file. I vaguely remember solving something like this maybe 3 or 4 years ago, but i can't remember at all what caused the problem.

Various google search results in my browser history (and some other ones) say stuff about order of include files with OpenGL, #define'ing the token 'UNICODE' from the build system, and a few other things i've tried, but no luck.

Here's the compiler output on one of the src files:

Code: Select all

GNU C++11 (GCC) version 5.4.0 (i686-w64-mingw32.static)
	compiled by GNU C version 4.7.2, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 19c0564a5a5cc655914b93344281471a
In file included from /usr/src/lng-cashflow/src/ui.h:47:0,
                 from /usr/src/lng-cashflow/src/main.h:21,
                 from /usr/src/lng-cashflow/src/main.cpp:2:
/usr/src/lng-cashflow/src/wxwidgets/prefsdialog.h:47:0: warning: "ID_TREECTRL" redefined
 #define ID_TREECTRL 10009
 ^
In file included from /usr/src/lng-cashflow/src/ui.h:45:0,
                 from /usr/src/lng-cashflow/src/main.h:21,
                 from /usr/src/lng-cashflow/src/main.cpp:2:
/usr/src/lng-cashflow/src/wxwidgets/mainwindow.h:42:0: note: this is the location of the previous definition
 #define ID_TREECTRL 10003
 ^
In file included from /net/deimos/usr/lib/mxe/usr/i686-w64-mingw32.static/include/windows.h:72:0,
                 from /net/deimos/usr/src/sigslot/sigslot_qt.h:90,
                 from /usr/src/lng-cashflow/src/main.h:18,
                 from /usr/src/lng-cashflow/src/main.cpp:2:
/usr/mxe/include/wx-3.1/wx/msw/winundef.h: In function 'HWND__* CreateDialog(HINSTANCE, LPCTSTR, HWND, DLGPROC)':
/usr/mxe/include/wx-3.1/wx/msw/winundef.h:48:20: error: cannot convert 'LPCTSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' for argument '2' to 'HWND__* CreateDialogParamA(HINSTANCE, LPCSTR, HWND, DLGPROC, LPARAM)'
             return CreateDialogA(hInstance, pTemplate, hwndParent, pDlgProc);
                    ^
In file included from /usr/mxe/include/wx-3.1/wx/defs.h:33:0,
                 from /usr/mxe/include/wx-3.1/wx/wx.h:14,
                 from /usr/src/lng-cashflow/src/ui.h:6,
                 from /usr/src/lng-cashflow/src/main.h:21,
                 from /usr/src/lng-cashflow/src/main.cpp:2:
/usr/mxe/include/wx-3.1/wx/msw/winundef.h: In function 'HFONT__* CreateFont(int, int, int, int, int, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, LPCTSTR)':
/usr/mxe/include/wx-3.1/wx/msw/winundef.h:82:48: error: cannot convert 'LPCTSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' for argument '14' to 'HFONT__* CreateFontA(int, int, int, int, int, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, LPCSTR)'
                                family, facename);
                                                ^
In file included from /net/deimos/usr/lib/mxe/usr/i686-w64-mingw32.static/include/windows.h:72:0,
                 from /net/deimos/usr/src/sigslot/sigslot_qt.h:90,
                 from /usr/src/lng-cashflow/src/main.h:18,
                 from /usr/src/lng-cashflow/src/main.cpp:2:
/usr/mxe/include/wx-3.1/wx/msw/winundef.h: In function 'HWND__* CreateWindow(LPCTSTR, LPCTSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)':
/usr/mxe/include/wx-3.1/wx/msw/winundef.h:105:20: error: cannot convert 'LPCTSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' for argument '2' to 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'
             return CreateWindowA(lpClassName, lpWndClass, dwStyle, x, y, w, h,
                    ^
In file included from /usr/mxe/include/wx-3.1/wx/defs.h:33:0,
                 from /usr/mxe/include/wx-3.1/wx/wx.h:14,
                 from /usr/src/lng-cashflow/src/ui.h:6,
                 from /usr/src/lng-cashflow/src/main.h:21,
                 from /usr/src/lng-cashflow/src/main.cpp:2:
/usr/mxe/include/wx-3.1/wx/msw/winundef.h: In function 'HMENU__* LoadMenu(HINSTANCE, LPCTSTR)':
/usr/mxe/include/wx-3.1/wx/msw/winundef.h:121:44: error: cannot convert 'LPCTSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' for argument '2' to 'HMENU__* LoadMenuA(HINSTANCE, LPCSTR)'
             return LoadMenuA(instance, name);
                                            ^
/usr/mxe/include/wx-3.1/wx/msw/winundef.h: In function 'HWND__* FindText(LPFINDREPLACE)':
/usr/mxe/include/wx-3.1/wx/msw/winundef.h:136:43: error: cannot convert 'LPFINDREPLACE {aka tagFINDREPLACEW*}' to 'LPFINDREPLACEA {aka tagFINDREPLACEA*}' for argument '1' to 'HWND__* FindTextA(LPFINDREPLACEA)'
             return FindTextA(lpfindreplace);
                                           ^
/usr/mxe/include/wx-3.1/wx/msw/winundef.h: In function 'HICON__* LoadIcon(HINSTANCE, LPCTSTR)':
/usr/mxe/include/wx-3.1/wx/msw/winundef.h:312:51: error: cannot convert 'LPCTSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' for argument '2' to 'HICON__* LoadIconA(HINSTANCE, LPCSTR)'
             return LoadIconA(hInstance, lpIconName);
                                                   ^
/usr/mxe/include/wx-3.1/wx/msw/winundef.h: In function 'HBITMAP__* LoadBitmap(HINSTANCE, LPCTSTR)':
/usr/mxe/include/wx-3.1/wx/msw/winundef.h:325:55: error: cannot convert 'LPCTSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' for argument '2' to 'HBITMAP__* LoadBitmapA(HINSTANCE, LPCSTR)'
             return LoadBitmapA(hInstance, lpBitmapName);
                                                       ^
src/CMakeFiles/lng_cashflow.dir/build.make:66: recipe for target 'src/CMakeFiles/lng_cashflow.dir/main.cpp.obj' failed
make[2]: *** [src/CMakeFiles/lng_cashflow.dir/main.cpp.obj] Error 1
make[2]: Leaving directory '/tmp/lng-build/windoze'
CMakeFiles/Makefile2:176: recipe for target 'src/CMakeFiles/lng_cashflow.dir/all' failed
make[1]: *** [src/CMakeFiles/lng_cashflow.dir/all] Error 2
make[1]: Leaving directory '/tmp/lng-build/windoze'
Makefile:86: recipe for target 'all' failed
make: *** [all] Error 2

delt
Earned a small fee
Earned a small fee
Posts: 24
Joined: Mon Dec 26, 2016 11:17 pm

Re: Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by delt » Mon Apr 22, 2019 4:22 am

Oh forgot to mention, this is wxWidgets 3.1.2, i built the windows version without openGL support, and wxUSE_UNICODE seems to be defined in the headers installed by the makefile.

User avatar
doublemax
Moderator
Moderator
Posts: 15833
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by doublemax » Mon Apr 22, 2019 7:26 am

When/where was the last time you built this successfully? What changed since then?

The errors indicate a Unicode mismatch, but it's hard to tell what exactly you need to change. Make sure that both _UNICODE and UNICODE are defined and have the same value.

Can you build the "minimal" sample that comes with wxWidgets? If yes, try to experiment what you need to add to that to reproduce the error.
Use the source, Luke!

delt
Earned a small fee
Earned a small fee
Posts: 24
Joined: Mon Dec 26, 2016 11:17 pm

Re: Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by delt » Mon Apr 22, 2019 9:24 am

Thanks for your reply Doublemax - also apologies for posting this in the wrong section of the messageboard. I noticed you moved it here.

I'm rebuilding the whole wx tree right now - incidentally, what should the UNICODE and _UNICODE constants be defined to, let's say for using normal UTF-8?

Also a bit off-topic, but is there any advantage to compile with SDL on windows (and mac) if that's even possible?

User avatar
doublemax
Moderator
Moderator
Posts: 15833
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by doublemax » Mon Apr 22, 2019 1:32 pm

what should the UNICODE and _UNICODE constants be defined to, let's say for using normal UTF-8?
They just need to be defined. But usually you don't have to care about that, as it's done through the make file. Can you build the "minimal" sample?
but is there any advantage to compile with SDL on windows (and mac) if that's even possible?
No. It's only supported under Linux and it only provides an additional sound backend.
Use the source, Luke!

delt
Earned a small fee
Earned a small fee
Posts: 24
Joined: Mon Dec 26, 2016 11:17 pm

Re: Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by delt » Mon Apr 22, 2019 1:40 pm

Thanks! Getting cmake to define _UNICODE to 1 as well as UNICODE fixed the problem.

delt
Earned a small fee
Earned a small fee
Posts: 24
Joined: Mon Dec 26, 2016 11:17 pm

Re: Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by delt » Mon Apr 22, 2019 5:30 pm

Ok, next hurdle in porting this app... under windows, trying to copy a wxString.c_str () to a char [] buffer, ie.

Code: Select all

char buf [4096];
wxString s = some_func_returning_a_wxString ();
strncpy (buf, s.c_str (), 4096);
causes a page fault: Unhandled exception: page fault on read access to 0x00000030 in 32-bit code (0x00870170).

The crash doesn't happen right at the strncpy () call, but shortly after, ie. usually next time any function is called.

I'm looking at the wxString docs but i can't find any helpful info.... :(

User avatar
doublemax
Moderator
Moderator
Posts: 15833
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by doublemax » Mon Apr 22, 2019 6:02 pm

The crash doesn't happen right at the strncpy () call, but shortly after, ie. usually next time any function is called.
This would indicate that the stack is overwritten, because more than 4096 bytes were copied. But i don't see how this is possible. What does the string contain when it crashes?

Is there any other code that could be responsible?
Use the source, Luke!

delt
Earned a small fee
Earned a small fee
Posts: 24
Joined: Mon Dec 26, 2016 11:17 pm

Re: Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by delt » Mon Apr 22, 2019 7:21 pm

Looks OK, the string is as it's supposed to be, so i don't think it's stack corruption. On x86_64 the stack still grows "downward" like in the old days, right? (ie. SP register is decremented when pushing stack frames) In the last 10 years or so, i've assumed this was the case but never actually made sure.

Anyway in the mean time i wrote a "safe" (but potentially very ineffective) workaround function that copies the characters directly from the wxString one by one using the [] operator overload. Gets the job done, at least for now.

User avatar
doublemax
Moderator
Moderator
Posts: 15833
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Cross-compiling for windows - errors about 'LPCTSTR' vs 'LPCSTR' and other mismatches

Post by doublemax » Mon Apr 22, 2019 8:17 pm

Anyway in the mean time i wrote a "safe" (but potentially very ineffective) workaround function that copies the characters directly from the wxString one by one using the [] operator overload. Gets the job done, at least for now.
This will only work for ASCII characters.

Besides, i wouldn't ignore that crash too quickly.
Use the source, Luke!

Post Reply