无论我做什么

发布于 2025-02-09 06:02:24 字数 1086 浏览 1 评论 0 原文

因此,我正在使用C和Win32 API制作桌面应用程序。我还与Mingw一起使用了Cmake/Make。一切都顺利进行,直到我想将.rc文件添加到可执行文件中。从我的理解来看,您可以编写一个.rc文件,然后将其编译到.RES文件中,然后可能您应该将其嵌入可执行文件中。但是,这是问题,当我尝试使用GNU实用程序 Windres 编译.RC文件时,它就无法编译。我总是会收到以下错误消息:

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.

这是我尝试过的每个.rc文件发生的,但是,这是我试图编译的当前测试文件:

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

resource.h 文件:

#define IDI_TEST_ICON 101

因此,最后的问题是:为什么Windres不成功编译.RC文件?在使用MingW的背景下,我该怎么办?

编辑1 : 值得注意的是,我还将.RC文件转换为ANSI格式,因为Windres在UTF-8中格式化时会产生特殊错误。然而,发生同样的错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2025-02-16 06:02:24

Windres 实际生成一个对象文件(以COFF格式),

因此您应该以这样的方式运行命令:

windres resource.rc -o resource.o

当我检查生成的文件的格式时:

file resource.o

我得到以下结果:

resource.o: Intel amd64 COFF object file, no line number info, not stripped, 1 section, symbol offset=0x3c4c, 1 symbols

因此,您只需要在链接步骤中包含生成的 .o 文件,以包括资源。

例如,当您运行以下命令时,您将获得不会运行的 .exe ,但它将在Explorer中显示图标:

gcc -shared -o resource.exe resource.o

windres actually generates an object file (in COFF format)

So you should run the command like this:

windres resource.rc -o resource.o

When I check the format of the generated file like this:

file resource.o

I get the following result:

resource.o: Intel amd64 COFF object file, no line number info, not stripped, 1 section, symbol offset=0x3c4c, 1 symbols

So you just need to include the generated .o file in the link step to include the resource.

For example when you run the following command you will get an .exe that won't run, but it will show the icon in Explorer:

gcc -shared -o resource.exe resource.o
独守阴晴ぅ圆缺 2025-02-16 06:02:24

在旧论坛线程中进行了大量挖掘之后,我设法找到了一个可行的“解决方案”。但是,我仍然发现这可以解决问题。我跟随Brecht Sanders建议并从 - use-temp-file flag。

该标志是编译 .rc 文件的替代方法,因为它不包括 popen 的使用(或在Windows中它等效的)。根据文档,如果 popen 在主机上插入 popen ,则应使用此标志。这里有趣的是,文档指出这是在某些非英语版本的Windows上发生的。就我而言,我使用的是瑞典语版本的Windows版本,这可能解释了为什么会发生这种情况。

因此,最终解决方案事实证明是:

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

最终产生一个对象文件,然后可以将其包含在 add_executable CMAKE中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文