通过命令行使用 gcc 编译 win32 应用程序时如何链接 .rc(资源)文件?

发布于 2024-10-04 07:16:25 字数 878 浏览 3 评论 0原文

我一直在关注伪造者 win32 教程,特别是 部分,并且是想知道在编译 win32 程序时如何链接 .rc(资源)文件? (我正在通过命令行编译)。

我正在阅读这篇文章,其中说你可以做这样的事情windres Chocolate-doom- res.rc Chocolate-doom-res.o 并以这种方式编译gcc other.o files.o etc.o Chocolate-doom-res.o -o Chocolate-doom.exe

但是当我尝试执行 windres res.rc res.o (res.rc 是我的资源文件)时,它给了我这个 windres: res.rc:3: 语法错误

res。 rc

#include "resource.h"
IDR_MYMENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit", ID_FILE_EXIT
    END

    POPUP "&Stuff"
    BEGIN
        MENUITEM "&Go", ID_STUFF_GO
        MENUITEM "G&o somewhere else",0,GRAYED
    END
END

IDI_MYICON ICON "menu_one.ico"

有什么想法吗?

I have been following the forgers win32 tutorial, specifically this section as of this moment, and was wondering how you would link the .rc(resource) file when compiling a win32 program? (I'm compiling through command line).

I was reading this article which says you could do something like this windres chocolate-doom-res.rc chocolate-doom-res.o and compile this waygcc other.o files.o etc.o chocolate-doom-res.o -o chocolate-doom.exe

But when I tried doing windres res.rc res.o (res.rc is my resource file) it gives me this windres: res.rc:3: syntax error

res.rc

#include "resource.h"
IDR_MYMENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit", ID_FILE_EXIT
    END

    POPUP "&Stuff"
    BEGIN
        MENUITEM "&Go", ID_STUFF_GO
        MENUITEM "G&o somewhere else",0,GRAYED
    END
END

IDI_MYICON ICON "menu_one.ico"

Any ideas?.

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

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

发布评论

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

评论(2

三五鸿雁 2024-10-11 07:16:25

您缺少 MENU 资源类型。你应该写:

#include "resource.h"
IDR_MYMENU MENU
BEGIN
.
.
.
END

You're missing the MENU resource type. You should write:

#include "resource.h"
IDR_MYMENU MENU
BEGIN
.
.
.
END
江挽川 2024-10-11 07:16:25

FFWD 至 2020 年第四季度。在VS Code时代,很多人在尝试在没有Visual Studio的情况下编译WIN32 GUI“Hello World”时都有些“超越基础”的挣扎。是的,资源文件可能是主要的绊脚石。这是一个更广泛的主题。

由于问题只是关于如何“编译”rc 文件,所以我只回答这个问题。

不知何故,您已经拥有了 my_app.rcresource.h

  • 为了避免一些潜在的大麻烦,请确保在 rc 文件顶部有 #include
  • 资源编译器称为 rc。如果 cl.exe 位于路径上,则 rc.exe 也在路径上。
  • rcrc 文件创建二进制 res 文件。
  • rc my_app.rc 将生成 my_app.res
    • 当然,前提是您的 rc 文件中没有错误。
    • 如果rc找不到windows.h你可以像这样添加它的路径
      rc /i"C:\Windows Kits\10\Include\10.0.18362.0\um" my_app.rc
    • 当然是您的本地路径。
  • 要使用 res,在 cl 命令行上,您需要将 res 文件传递​​给链接器,就像这个虚构的示例 cl 编译命令一样确保
cl /Zi /EHsc /Fe:my_app.exe my_app.cpp /link my_app.res

/linkcl 命令行的最后一个参数。

编辑

假设您的项目文件夹包含:

my_app.cpp
resource.h
my_app.rc

首先,您需要如上所述生成my_app.res
其次,在 VS Code .vscode/tasks.json 中,您将拥有:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "/link /SUBSYSTEM:WINDOWS ${fileDirname}\\${fileBasenameNoExtension}.res",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: cl.exe"
        }
    ]
}

注意您需要添加到由 < 生成的其他标准任务文件中的 /link 参数。代码>VS代码。打开 my_app.cpp 并执行 CTRL+SHIFT+B。这将编译活动文件并将其链接到您的 WIN32 应用程序中,其中包括您的资源。

FFWD to 2020 Q4. In the era of VS Code, a lot of people are somewhat struggling with "beyond the basics" when trying to compile WIN32 GUI "Hello World" without Visual Studio. And yes, the resource file is probably the main stumbling block. That is a wider subject.

Since the question is only about how to "compile in" the rc file, let me answer only that.

Somehow you got to the point at which you also have my_app.rc and resource.h.

  • to avoid some potential big hassle make sure you have #include <windows.h> at the top of the rc file
  • resource compiler is called rc. If cl.exe is on the path, rc.exe is too.
  • rc makes a binary res file from the rc file.
  • rc my_app.rc will produce my_app.res
    • of course only if your rc file has no mistakes in there.
    • if rc can not find windows.h you can add the path to it like so
      rc /i"C:\Windows Kits\10\Include\10.0.18362.0\um" my_app.rc
    • with your local path of course.
  • to use the res, on the cl command line you need to pass the res file to the linker, like on this imaginary example cl compilation command line
cl /Zi /EHsc /Fe:my_app.exe my_app.cpp /link my_app.res

make sure /link is the last argument to your cl command line.

EDIT

Let's assume your project folder contains:

my_app.cpp
resource.h
my_app.rc

First, you will need to produce my_app.res as described above.
Second, in the VS Code .vscode/tasks.json, you will have:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "/link /SUBSYSTEM:WINDOWS ${fileDirname}\\${fileBasenameNoExtension}.res",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: cl.exe"
        }
    ]
}

Notice the /link argument you need to add, to the otherwise standard tasks file generated by VS Code. Open my_app.cpp and perform CTRL+SHIFT+B. That will compile and link the active file into your WIN32 App, with your resources included.

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