无法使用windres向exe添加图标

时间:2016-11-07 10:27:44

标签: windows gcc icons mingw windres

我想在使用mingw-gcc编译的exe中添加一个Icon。

我按照this SO post中的说明操作,但是在Windows资源管理器中我的exe上没有显示Icon。

[编辑] 同时我发现windres会破坏我的可执行文件。在应用windres之前,可执行文件按预期运行。在应用windres调用可执行结果后,在Windows错误消息中告知(大致)此可执行文件与此Windows版本不兼容。

我做错了什么?

这是我的目录布局:

$ ls -lR launcher/
launcher/:
total 508
drwxr-xr-x 1 me 1049089      0 Aug 20  2015 src/
drwxr-xr-x 1 me 1049089      0 Nov  7 10:56 target/

launcher/src:
total 0
drwxr-xr-x 1 me 1049089 0 Nov  7 10:51 main/

launcher/src/main:
total 4
drwxr-xr-x 1 me 1049089 0 Nov  7 10:52 cpp/
drwxr-xr-x 1 me 1049089 0 Apr 14  2016 resources/
drwxr-xr-x 1 me 1049089 0 Nov  4 15:11 scripts/

launcher/src/main/cpp:
total 8
-rw-r--r-- 1 me 1049089 6793 Nov  7 10:41 JavaLauncher.cpp

launcher/src/main/resources:
total 5
-rw-r--r-- 1 me 1049089   47 Nov  7 10:47 javaLauncher.rc
-rw-r--r-- 1 me 1049089 2238 Apr 14  2016 JavaLauncher.ico

launcher/src/main/scripts:
total 1
-rw-r--r-- 1 me 1049089 389 Nov  7 10:56 makefile

launcher/target:
total 4
-rwxr-xr-x 1 me 1049089 2502 Nov  7 10:56 JavaLauncher.exe*

这是我的资源档案:

0 ICON "launcher/src/main/resources/JavaLauncher.ico"

这是我的makefile:

all: launcher/target/JavaLauncher.exe

launcher/target/JavaLauncher.exe: launcher/src/main/cpp/JavaLauncher.cpp launcher\target
    /Absolute/Path/to/mingw64/bin/g++.exe $< -o $@ -static -l winpthread
    /Absolute/Path/to/mingw64/bin/windres.exe -v -i launcher/src/main/resources/javaLauncher.rc -o $@


launcher\target:
    cmd /c md $@

这是make的输出:

/Project/root>/Absolute/Path/to/mingw64\bin\make.exe -f launcher\src\main\scripts\makefile
cmd /c md launcher\target
/Absolute/Path/to/mingw64/bin/g++.exe launcher/src/main/cpp/JavaLauncher.cpp -o launcher/target/JavaLauncher.exe -static -l winpthread
/Absolute/Path/to/mingw64/bin/windres.exe -v -i launcher/src/main/resources/javaLauncher.rc -o launcher/target/JavaLauncher.exe
Using `/Absolute/Path/to/mingw64/bin/gcc -E -xc -DRC_INVOKED  launcher/src/main/resources/javaLauncher.rc'
Using popen to read preprocessor output

/Project/root>

这是Windows资源管理器中的结果: exe without specified image

[编辑] 最终的工作解决方案是:

mingwPath = $(realpath Path/to/mingw64/bin)
TARGET_DIR=target
TARGET_OBJECT_DIR=$(TARGET_DIR)/objects
TARGET_DIR_NAME=$(subst /,\, $(TARGET_DIR))
TARGET_OBJECT_DIR_NAME=$(subst /,\, $(TARGET_OBJECT_DIR))
SOURCE_DIR_NAME=src/main
APP_NAME=MyApp
TARGET_BASE_NAME=$(TARGET_DIR)/$(APP_NAME)
TARGET_ARCH=-m32

all: $(TARGET_OBJECT_DIR_NAME) $(TARGET_BASE_NAME).exe


$(TARGET_BASE_NAME).exe: $(TARGET_OBJECT_DIR)/$(APP_NAME).o\ 
 $(TARGET_OBJECT_DIR)/$(APP_NAME)Res.o $(TARGET_OBJECT_DIR_NAME)    
    $(mingwPath)/g++  $(TARGET_ARCH) -o $@ -static -l winpthread   $(filter %.o,$^)

$(TARGET_OBJECT_DIR)/$(APP_NAME).o: $(SOURCE_DIR_NAME)/cpp/$(APP_NAME).cpp
    $(mingwPath)/g++ $(TARGET_ARCH) -c $<  -o $@ 

$(TARGET_OBJECT_DIR)/$(APP_NAME)Res.o:  $(SOURCE_DIR_NAME)/resources/$(APP_NAME).rc
    $(mingwPath)/windres -v -i $< -o $@  --output-format=coff --target=pe-i386


$(TARGET_OBJECT_DIR_NAME):$(TARGET_DIR_NAME)
    echo $@
    cmd /c md $@

$(TARGET_DIR_NAME):
    echo $@
    cmd /c md $@

clean: 
    cmd /c del /s /q $(TARGET_DIR_NAME)

1 个答案:

答案 0 :(得分:1)

我不确定windres资源编译器是否可以编译并将资源数据直接添加到exe文件中,但这是您在此尝试执行的操作。也许这是可能的,我搜索了一下,但无法找到有关信息。

我得到了这个工作并有一个exe图标。您需要在程序对象之后将windres生成的资源对象指定给g++链接器。同时更改顺序并先运行windres,以便在g++链接器链接程序和资源对象之前生成资源对象文件。

all: launcher/target/JavaLauncher.exe

launcher/target/JavaLauncher.exe: launcher/src/main/cpp/JavaLauncher.cpp launcher\target
    /Absolute/Path/to/mingw64/bin/windres.exe -v -i launcher/src/main/resources/JavaLauncher.rc -o launcher/src/main/resources/JavaLauncherRes.o
    /Absolute/Path/to/mingw64/bin/g++.exe $< -o $@ launcher/src/main/resources/JavaLauncherRes.o -static


launcher\target:
    cmd /c md $@

enter image description here