Saturday, October 26, 2019

Building open source C++ libraries on Windows for 32 bit and 64 bit

In my experience, most open source C/C++ library projects don't do a good job of providing the ability to build the library in all four builds, i.e. all combinations of release mode and debug mode with 32 bits and 64 bits. These days it is usually just 64 bit and sometimes it's just 64 bit release. To get all four build modes one has to start hacking but there is a little gotcha that nobbles me every now and then, so I thought I would blog about the solution so I never have to strain my brain to remember it in future. I can just go to my blog.

Add a new configuration from the configuration manager. Pick Win32 from the pick list and say you want to inherit from the 64 bit configuration (that's so you get all that the 64 bit configuration has). This configuration will claim to be 32 bit but there will be a problem. The linker will be set for 32 bit but the compilation will be in 64 bit. This is not apparent from the settings dialog. So when you build you will see an error like:

fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'

To fix this, edit the Visual Studio project file, removing this line from the 32 bit sections:

<AdditionalOptions>%<AdditionalOptions> /machine:x64</AdditionalOptions>

That's it!