windows 如何向 mingw-gcc 编译的可执行文件添加图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/708238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:13:44  来源:igfitidea点击:

How do I add an icon to a mingw-gcc compiled executable?

windowsgcciconsmingw

提问by Evan

In Windows, using mingw's gcc, is there anyway to specify that the output exe file is to take an icon file, so that the exe file shows with that icon in explorer?

在 Windows 中,使用 mingw 的 gcc,无论如何指定输出 exe 文件是采用图标文件,以便 exe 文件在资源管理器中显示该图标?

回答by Evan

You need to create the icon first. Then you need to create a RC file with the below content. Here we'll name it as my.rc.

您需要先创建图标。然后您需要创建一个包含以下内容的 RC 文件。在这里,我们将其命名为my.rc.

id ICON "path/to/my.ico"

The idmentioned in the above command can be pretty much anything. It doesn't matter unless you want to refer to it in your code. Then run windres as follows:

id上面提到的命令可以是相当多的东西。除非您想在代码中引用它,否则这无关紧要。然后按如下方式运行windres:

windres my.rc -O coff -o my.res

Then while building the executable, along with other object files and resource files, include my.reswhich we got from the above step. e.g.:

然后在构建可执行文件以及其他目标文件和资源文件时,包含my.res我们从上述步骤中获得的内容。例如:

g++ -o my_app obj1.o obj2.o res1.res my.res

And that should be all there is to it.

这应该就是它的全部内容。



And, at no extra charge, if you want to include version information in your application, add the following boilerplate to a new .rcfile and follow the above mentioned steps.

而且,如果您想在应用程序中包含版本信息,无需额外费用,请将以下样板添加到新.rc文件中并按照上述步骤操作。

1 VERSIONINFO
FILEVERSION     1,0,0,0
PRODUCTVERSION  1,0,0,0
BEGIN
  BLOCK "StringFileInfo"
  BEGIN
    BLOCK "080904E4"
    BEGIN
      VALUE "CompanyName", "My Company Name"
      VALUE "FileDescription", "My excellent application"
      VALUE "FileVersion", "1.0"
      VALUE "InternalName", "my_app"
      VALUE "LegalCopyright", "My Name"
      VALUE "OriginalFilename", "my_app.exe"
      VALUE "ProductName", "My App"
      VALUE "ProductVersion", "1.0"
    END
  END
  BLOCK "VarFileInfo"
  BEGIN
    VALUE "Translation", 0x809, 1252
  END
END

Note, the langID is for U.K. English (which is the closest localisation to Australia I could identify.) If you want U.S. "English" then change the BLOCKline to:

请注意,langID 用于英国英语(这是我能识别的最接近澳大利亚的本地化。)如果您想要美国“英语”,则将该BLOCK行更改为:

BLOCK "040904E4"

and the translation line to:

和翻译行:

VALUE "Translation", 0x409, 1252

See VERSIONINFO resourcefor for info.

有关信息,请参阅VERSIONINFO 资源

回答by Steven Penny

In the RC file, the nameIDdoes not even have to be a name, it can just be an integer. The filenamemust be quoted only if it contains a space. Instead of:

在 RC 文件中,nameID甚至不必是名称,它可以是一个整数。该文件名必须唯一,如果它包含空格报价。代替:

windres my.rc -O coff -o my.res

You can use:

您可以使用:

windres my.rc my.o

回答by noabody

Try Resource Hacker. I was able to cross compile my project in Linux (WSL) and generate an icon from the logo on the homepage. Just needed a simple way to embed it in the exe and this program worked great. Resource Hacker by Angus Johnson

试试资源黑客。我能够在 Linux (WSL) 中交叉编译我的项目并从主页上的徽标生成一个图标。只需要一种简单的方法将它嵌入到 exe 中,这个程序运行良好。 Angus Johnson 的资源黑客