How should I handle character encoding/conversion issues when cross-compiling from Linux to Windows using mingw?
I am attempting to cross-compile a Qt project on a Linux system for a Windows target. I’ve installed (what I believe to be) all the required mingw packages, and I think my environment is configured correctly. However, when I try to configure the Qt libraries, it fails when trying to compile project.cpp:
project.cpp: In member function 'QStringList& QMakeProject::values(const QString&, QMap<QString, QStringList>&)':
project.cpp:3062:51: error: cannot convert 'wchar_t*' to 'LPSTR {aka char*}' for argument '1' to 'WINBOOL GetComputerNameA(LPSTR, LPDWORD)'
gmake: *** [project.o] Error 1
I was able to hack around this single error by replacing wchar_t* with LPSTR in the code, but then I just ran into another error. It seems obvious that I am now facing a character encoding problem. I’m imagining that there might be some compiler options that would handle the conversion properly (I was looking at fexec-charset), but I’m not even really sure about the problem. I understand that LPSTR and the like are Microsoft typedefs, but how should mingw handle them when cross-compiling from Linux for a Windows target?
Thanks!
Here is the offending lines of code from qmake/project.cpp:
DWORD name_length = 1024;
wchar_t name[1024];
if (GetComputerName(name, &name_length))
ret = QString::fromWCharArray(name);
It seems GetComputerName is a macro which is being expanded to GetComputerNameA, when apparently I need GetComputerNameW.
By default, almost all Windows API functions (e.g.
GetComputerName) will resolve to the ANSI implementations (e.g.GetComputerNameA) as opposed to their wide char variants (e.g.GetComputerNameW).Try
#define UNICODEbefore your#include <windows.h>or pass the macro as an argument to your compiler.For more info, see this and this.