Skip to content

Replace add_definitions(/W4) with add_compile_options to avoid rc.exe breakage - #1393

Merged
frankslin merged 1 commit into
BYVoid:masterfrom
frankslin:fix-librime-compliation
Jul 3, 2026
Merged

frankslin merged 1 commit into
BYVoid:masterfrom
frankslin:fix-librime-compliation

Conversation

@frankslin

Copy link
Copy Markdown
Collaborator

librime exposes this latent bug by building OpenCC with Ninja: add_definitions(/W4) propagates /W4 into the RC file compile command, causing rc.exe to fail with RC1106. OpenCC's own CI uses the Visual Studio generator (MSBuild), which keeps C++ and RC compiler flags in separate vcxproj sections, so the issue was not visible there.

… breakage

librime exposes this latent bug by building OpenCC with Ninja: add_definitions(/W4)
propagates /W4 into the RC file compile command, causing rc.exe to fail with RC1106.
OpenCC's own CI uses the Visual Studio generator (MSBuild), which keeps C++ and RC
compiler flags in separate vcxproj sections, so the issue was not visible there.
@frankslin

Copy link
Copy Markdown
Collaborator Author

為什麼改 add_definitions 為 add_compile_options 就能解決 /W4 和 rc.exe 的問題?

關鍵在於 CMake 兩個函式的語義差異,以及 Ninja generator 如何把它們翻譯成編譯命令。

add_definitions() 的問題

add_definitions() 的本意是新增前處理器巨集(-D 字首的 define)。但它也接受非 -D 的引數——當你傳 /W4
時,CMake 會把它扔進 directory 的 COMPILE_DEFINITIONS 屬性。

CMake 在生成 Ninja build 規則時,對目錄內 所有語言的編譯步驟 都會展開 COMPILE_DEFINITIONS,包括:

  • .cc / .cpp → cl.exe
  • .c → cl.exe
  • .rc → rc.exe

所以 rc.exe 的命令列裡就出現了 /W4,而 rc.exe 只認 /D(前處理器巨集),不認 /W(警告級別)→ RC1106:
invalid option: -4。

add_compile_options() 為什麼沒問題

add_compile_options() 寫入的是 COMPILE_OPTIONS 屬性,而不是 COMPILE_DEFINITIONS。

CMake 在生成 RC 編譯規則時,展開的是:

<CMAKE_RC_COMPILER>

  • ← 來自 COMPILE_DEFINITIONS(add_definitions 寫入的地方)
  • ← 來自 CMAKE_RC_FLAGS,不含 COMPILE_OPTIONS

C/CXX 編譯規則才會同時展開 COMPILE_OPTIONS,RC 規則不會。改用 add_compile_options(/W4) 之後 /W4
就只進 cl.exe,rc.exe 拿不到它。

為什麼 Visual Studio generator 沒問題

Visual Studio generator 生成的是 .vcxproj,MSBuild 裡 C++ 編譯配置(ClCompile)和 RC
編譯配置(ResourceCompile)是兩個獨立的 XML section。CMake 填 add_definitions(/W4) 時只寫入
ClCompile 的 PreprocessorDefinitions,不會流入 ResourceCompile,所以 OpenCC 自己的 CI 看不到這個
bug。Ninja 的 build rule 是平鋪的顯式命令,沒有這層隔離。

@frankslin
frankslin merged commit e9f3bb3 into BYVoid:master Jul 3, 2026
37 of 42 checks passed
@frankslin
frankslin deleted the fix-librime-compliation branch July 3, 2026 06:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants