how do I link a .rc (resource) file when compiling a win32 app with gcc through command line?
You're missing the MENU resource type. You should write:
#include "resource.h"
IDR_MYMENU MENU
BEGIN
.
.
.
END
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 therc
file - resource compiler is called
rc
. Ifcl.exe
is on the path,rc.exe
is too. rc
makes a binaryres
file from therc
file.rc my_app.rc
will producemy_app.res
- of course only if your rc file has no mistakes in there.
- if
rc
can not findwindows.h
you can add the path to it like sorc /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 thecl
command line you need to pass theres
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.