Dawid Bautsch Dev Blog

No category

Building Qt 4 for Windows 2000

dbautsch

Introduction

Last time I have done some experiments with old releases of Qt trying to check the limitations of both supported operating systems and toolchains. This entry is a summary of these experiments.

I started my Qt adventure not so long ago, it was Qt 4.5 or 4.6 in 2010. My development environment was Microsoft Visual Studio 2008 (and QtVsAddin) and platform was Windows XP/Fedora 14. Back then QtCreator project was just started, I began using it in 2011. It worked the best using MINGW-32 (on Windows).

Because my primary platform for Qt was Windows XP, I had not very much occasions to test it against Windows 2000. The goal is simple: try to build Qt4 framework and run the sample programs on Windows 2000.

I began my experiment with installing Microsoft Visual Studio 2005 on Windows 2000 SP.4. Windows SDK package is also required. Windows SDK 7.1 will do the job. After tinstalling SDK I downloaded sources of Qt from official web page (https://download.qt.io/archive/qt/).

The first version I checked was 4.1.0. It turned out that win32-msvc2005 is not supported by this version when open-source license is selected. The same situation was with 4.2.3. Because an original toolchain MinGW 4.4 is not so easy to find on the internet I gave up with trying to compile Qt 4.1 and 4.2.3 with MinGW 4.4.

The next version I checked was 4.3.5. The situation was different here, msvc-2005 toolset is available and the build environment worked right away and I was able to compile it.

The command to configure Qt source package is:

configure -debug-and-release -platform win32-msvc2005

Then I started actual build process using nmake and nmake install. And voila – success! It turned out that Qt 4.3.5 builds successfully on Windows 2000. Even QtDesigner or Linguist were built as well as my sample “Qt Hello World!” application along with Qt sample programs.

The build takes a lot of time. On the virtual machine with 2GB of fast DDR-4 ram and dual core virtual CPU (Ryzen 7 2700X) it takes one hour. Given that the VHD file was located on ultra fast NVME PCI-E 3.0 drive it is very long. A lot of time takes the compilation of WebKit engine.

The sample program “Browser” that uses WebKit engine was compiled successfully, but strange crash occurs when trying to load web page. It throws an exception and closes immediately. QtDesigner doesn’t worked either. The same problem occurs on Qt 4.6 and later.

A sample program demonstrating affine transormations, Qt4, Windows 2000

The results of compilation of Qt source package:

As you can see versions from 4.3 to 4.5 works without any issues. Qt 4.1 and 4.2 had to be compiled using MinGW 4.4 which is unavailable on the internet so I gave up with compiling these.

“qtdemo” programs

Let’s get back to the crashing QtDesigner. I switched to debug configuration and started looking for the root cause of crashing. During debug session it turned out that rand_s is missing because it uses RtlGenRandom which is implemented by Windows XP kernel, so there is no chance to have it on Windows 2000. The problem occurs in an engine of JavaScript. Take a look at the source code:

double randomNumber
{
// ..some initialization code, then ...
#if COMPILER(MSVC) && defined(_CRT_RAND_S)
    uint32_t bits;
    rand_s(&bits);
    return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
#elif OS(DARWIN)
    uint32_t bits = arc4random();
    return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
#elif OS(UNIX)
    // ... using random()
#elif OS(WINCE)
    return genrand_res53();
#elif OS(WINDOWS)
    // .... fallback to rand()
#else
    // .... fallback to rand()
#endif
}

As you can see, when _CRT_RAND_S symbol is defined, then rand_s() function will be used. The preprocessor defines are declared in Windows SDK headers (remember – I had to use Windows 7.1 SDK because of the lack of SDK for Windows 2000). _CRT_RAND_S is enabled by default. The easiest solution would be to comment out any occurrence of rand_s and just replace it with the section “fallback to rand()”. I reviewed the code of this fallback and it seems to be OK. I’m pretty sure, that using rand() instead rand_s() would be cryptographic less secure, but what else can be done here? Maybe looking up for the sources of rand_s() and copy over the implementation would solve the problem, but that was too much for me of the time to invest.

After applying this patch I had to rebuild the whole Qt sources, and finally we got working QtDesigner on Windows 2000 as well as working WebBrowser program using WebKit.

QtDesigner and Windows 2000

Finally I was able to build the latest Qt4 release: Qt 4.8.7. All examples seems to be working just fine as well as the QtDesigner program. I can think of that as a success, but also I’m pretty sure that there are more similar fixes to be applied. The Qt sources will compile successfully, but that doesn’t necessarily means that it works at runtime. I would have to run regression tests on the compiled Qt distro, but I have no access to such tests.

Qt 4.8.7 has been released at the turn of 2011 and 2012, so it is the release almost 6 years old. Besides that fact it might work for you if you are just a hobbyist and you are looking for cheap solution to work across wide range of operating systems, because Qt4 will works on Windows 2000, Windows XP, Vista, 7, 8 and 10. On Linux it will work on Fedora 13 and newer. FreeBSD will handle such applications also very well. Many of opensource software has not been migrated to Qt5 until today. One thing worth mentioning is that there are certain areas when Qt 4.x release is just too old for today’s use. Let’s say for an example HTTP secure connections. Most of the protocols used at the time of Qt 4.8 are today obsolete and disabled by most of the web servers and thus Qt Network module is pretty unusable. Well, you can always use libCURL, but that’s another dependency to your project…


All of the builds mentioned in this post could be downloaded from my SourceForge account:

https://sourceforge.net/projects/qt-4-builds/files/msvc-2005-x86/win-2k/

Added on July 4, 2018.

I just downloaded Qt 4.8.7 built with MinGW 4.4 and after the installation it turned out that it has been linked against more recent CRT library (probably MSVC 2008) and thus it will not work on Windows 2000.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top