Search code examples
cwinapimingwwindres

Windres not compiling .rc file no matter what I do


So I am making a desktop application using C and the Win32 API. I am also using CMake/Make in conjunction with MinGW. Everything went smoothly until I wanted to add an .rc file to the executable. From my understanding you write an .rc file which is then compiled to a .res file, then presumably you are supposed to embed it in the executable. Here's the problem however, when I attempt to compile the .rc file with the GNU utility windres it fails to compile. I always get the following error messages:

C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\bin\windres.exe: can't open file `page:': Invalid argument
C:\Code\C\test\resources.rc:4: fatal error: when writing output to : No such file or directory
4 | IDI_TEST_ICON    ICON    "test.ico"
  |
compilation terminated.
C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\bin\windres.exe: preprocessing failed.

This occurs with every .rc file I've tried, for completeness however, here is the current test file I am trying to compile:

#include <windows.h>
#include "resource.h"
IDI_TEST_ICON   ICON    "test.ico"

And the resource.h file:

#define IDI_TEST_ICON 101

So the final question is the following: Why doesn't windres compile the .rc file successfully? And what can I do about in the context of using MinGW?

Edit 1: Worth noting is that I also converted the .rc file to ANSI format since windres is notorious for yielding peculiar errors when formatted in UTF-8. Yet, the same errors occur.


Solution

  • After a lot of digging in old forum threads I managed to find a "solution" that works. However I still find it peculiar that this solves the problem. I followed Brecht Sanders advise and downloaded a standalone build from winlibs.com. Even this didn't solve the problem which led me to investigate possible RC_COMPILER_FLAGS which ultimately led me to the --use-temp-file flag.

    This flag acts as an alternate approach to compile .rc files since it excludes the use of popen (or the equivalent of it in Windows). According to the documentation this flag should be used if the implemenation of popen is buggy on the host. What is interesting here is that the documentation states that this is known to happen on certain non-english versions of Windows. In my case I am using the Swedish language version of Windows which might explain why this is occuring.

    Thus the final solution turns out to be:

    windres -i resource.rc -o resource.o --use-temp-file
    

    Which ultimately yields an object file which can then be included in the add_executable call in CMake.