如何使用 cmake 和 MinGW 构建带有资源文件的 Win32 应用程序?

发布于 2024-09-15 00:10:44 字数 984 浏览 2 评论 0原文

理论上,使用 cmake 构建带有资源文件的 Win32 应用程序非常容易。在 add_executable 命令中,可以像列出 C 或 C++ 源文件一样轻松地列出资源文件。然而,使用 MinGW 工具构建时存在一个已知错误。

我找到了一个解决方法,即在 CMakeFiles.txt 中包含以下内容...

if(MINGW)
  set(CMAKE_RC_COMPILER_INIT windres)
  ENABLE_LANGUAGE(RC)
  SET(CMAKE_RC_COMPILE_OBJECT
      "<CMAKE_RC_COMPILER> <FLAGS> <DEFINES> -o <OBJECT> <SOURCE>")
endif(MINGW)

不幸的是,这似乎不起作用。似乎发生的情况是 windres 生成了 .rc.res 文件,而 ld 无法理解该文件。

在我的搜索中,我强烈怀疑 Win32 支持被视为非常低的优先级,尤其是在 Visual Studio 之外。这是可以理解的,因为 Win32 显然不像以前那么重要了。当然,Visual Studio Express 版本很容易免费获得。

即便如此,如果我可以将 MinGW GCC 用于我仍在使用的一些旧 Win32 应用程序,那对我来说会很方便。如果不出意外,我可以获得 GCOV 测试覆盖率统计数据。

显然,如果一切都失败了,我总是可以使用自定义构建命令来处理资源文件。一个问题是我不熟悉 Windres 或 ld,也不熟悉 MinGW 是如何处理 Win32 资源文件的。另一个是,如果有人已经拥有愿意与我分享的高级轮子,我真的不想重新发明轮子。

基本上就是这样 - 我如何支持使用 cmake 和 MinGW 构建带有资源文件的 Win32 应用程序(但不破坏对 Visual Studio 的支持)?

In theory, it's very easy to build a Win32 app with a resource file using cmake. In an add_executable command, a resource file can be listed as easily as a C or C++ source file. There is a known bug, however, when building using MinGW tools.

I found a workaround, which is to include the following in CMakeFiles.txt...

if(MINGW)
  set(CMAKE_RC_COMPILER_INIT windres)
  ENABLE_LANGUAGE(RC)
  SET(CMAKE_RC_COMPILE_OBJECT
      "<CMAKE_RC_COMPILER> <FLAGS> <DEFINES> -o <OBJECT> <SOURCE>")
endif(MINGW)

Unfortunately, this doesn't seem to work. What seems to happen is that windres generates a <whatever>.rc.res file which ld doesn't understand.

In my searches, I've developed a strong suspicion that Win32 support is seen as a very low priority, especially outside of Visual Studio. This is understandable, as Win32 obviously isn't as important as it once was. And of course Visual Studio Express Editions are easily available for free.

Even so, it would be convenient for me if I could use MinGW GCC for a few old Win32 apps I still use. If nothing else, I can get GCOV test coverage stats.

Obviously if all else fails, I could always handle resource files using a custom build command. One issue is that I'm not familiar with either windres or ld, or how MinGW is meant to handle Win32 resource files. Another is that I don't really want to reinvent the wheel if someone already has a superior wheel they'd like to share with me.

So that's basically it - how can I support building Win32 apps with resource files using cmake, and using MinGW (but not breaking support for Visual Studio)?

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

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

发布评论

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

评论(1

自演自醉 2024-09-22 00:10:44

我想,你的问题就在这里:

<CMAKE_RC_COMPILER> <FLAGS> <DEFINES> -o <OBJECT> <SOURCE>

也许你应该写这样的东西:

<CMAKE_RC_COMPILER> <SOURCE> <OBJECT>

或者更正式:

<CMAKE_RC_COMPILER> -i <SOURCE> -o <OBJECT>

另一个可能的问题可能是 - cmake 替换哪个扩展?因为 windress 会从中猜测所需的输出文件格式。

参考资料在这里:
www.mingw.org/wiki/MS_resource_compiler

“res”文件不适合 ld,因为您已经知道了,windres 的例子
sourceware.org/binutils/docs/binutils/windres.html

Windres man

<由问题作者强>编辑...

固定的cmake代码片段如下...

if(MINGW)
  set(CMAKE_RC_COMPILER_INIT windres)
  ENABLE_LANGUAGE(RC)
  SET(CMAKE_RC_COMPILE_OBJECT
    "<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>")
endif(MINGW)

它可能应该设置一个标志变量而不是在命令模板中插入 -O 选项,但这有效。

I think, your problem is here:

<CMAKE_RC_COMPILER> <FLAGS> <DEFINES> -o <OBJECT> <SOURCE>

Maybe you should write something like this:

<CMAKE_RC_COMPILER> <SOURCE> <OBJECT>

Or more formal:

<CMAKE_RC_COMPILER> -i <SOURCE> -o <OBJECT>

The other possible problem could be with - which extension does cmake substitutes? As windress will guess the needed output file format from that.

References are here:
www.mingw.org/wiki/MS_resource_compiler

"res" files are unappropriate for ld, as you already know, and the windres example
sourceware.org/binutils/docs/binutils/windres.html

windres man

EDIT by question author...

The fixed cmake code snippet is as follows...

if(MINGW)
  set(CMAKE_RC_COMPILER_INIT windres)
  ENABLE_LANGUAGE(RC)
  SET(CMAKE_RC_COMPILE_OBJECT
    "<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>")
endif(MINGW)

It should probably be setting a flags variable rather than inserting the -O option in the command template, but this works.

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