Repository navigation
Replace add_definitions(/W4) with add_compile_options to avoid rc.exe breakage - #1393
Conversation
… 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.
為什麼改 add_definitions 為 add_compile_options 就能解決 /W4 和 rc.exe 的問題?關鍵在於 CMake 兩個函式的語義差異,以及 Ninja generator 如何把它們翻譯成編譯命令。 add_definitions() 的問題add_definitions() 的本意是新增前處理器巨集(-D 字首的 define)。但它也接受非 -D 的引數——當你傳 /W4 CMake 在生成 Ninja build 規則時,對目錄內 所有語言的編譯步驟 都會展開 COMPILE_DEFINITIONS,包括:
所以 rc.exe 的命令列裡就出現了 /W4,而 rc.exe 只認 /D(前處理器巨集),不認 /W(警告級別)→ RC1106: add_compile_options() 為什麼沒問題add_compile_options() 寫入的是 COMPILE_OPTIONS 屬性,而不是 COMPILE_DEFINITIONS。 CMake 在生成 RC 編譯規則時,展開的是: <CMAKE_RC_COMPILER>
C/CXX 編譯規則才會同時展開 COMPILE_OPTIONS,RC 規則不會。改用 add_compile_options(/W4) 之後 /W4 為什麼 Visual Studio generator 沒問題Visual Studio generator 生成的是 .vcxproj,MSBuild 裡 C++ 編譯配置(ClCompile)和 RC |
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.