Monday, October 31, 2016

Cross Compiling Unicode Windows Application with Mingw-w64

Cross compiling Unicode Windows application in Linux is quite straight forward if you are using mingw-w64 cross compiler. All you have to do is turn on the -municode compiler switch. Other than that, you need to change your program entry point from main() to wmain() if your program is a command line program. I provided a complete sample code over at https://bitbucket.org/pinczakko/windows-event-object-test that you can read and use.

Now, let's look at the most important parts of the sample code with respect to Unicode support. First the CMakeLists.txt file. These are the necessary changes to support Unicode:
if (MINGW)
 message(status: " ** MINGW detected.. **")
 set(CMAKE_CXX_FLAGS_RELEASE "-municode ${CMAKE_CXX_FLAGS_RELEASE}")
 set(CMAKE_C_FLAGS_RELEASE "-municode ${CMAKE_C_FLAGS_RELEASE}")

 set(CMAKE_CXX_FLAGS_DEBUG "-municode ${CMAKE_CXX_FLAGS_DEBUG}")
 set(CMAKE_C_FLAGS_DEBUG "-municode ${CMAKE_C_FLAGS_DEBUG}")

# ..
endif()
The preceding code snippet shows the C++ and C compiler switch has been modified to use -municode if mingw compiler is detected.

Second, the entry point of the command line program is also modified:
int wmain( void )

The third change is the code also make use of wprintf() function in place of printf() in some places. This is one of the example:
wprintf(L"All threads ended, cleaning up for application exit...\n");
wprintf() is the "wide-character" version of printf(), well you could argue that you want to use the "tchar" version and so on. However, in this short article, the point is just to see how mingw-w64 support, I'm not trying to be extremely correct.
Post a Comment

No comments: