target_link_options "LINKER:" prefix does not work with clang on Windows
When compiling with clang (not clang-cl) on Windows, the LINKER: prefix of target_link_options() is not honoured.
Tested with CMake 3.18.1.
Workaround is to manually define CMAKE_${lang}_LINKER_WRAPPER_FLAG
, e.g.:
set(CMAKE_CXX_LINKER_WRAPPER_FLAG "-Xlinker" " ")
To repro:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(LinkOptsTest CXX)
add_executable(test_exe
"main.cpp"
)
target_link_options(test_exe
PRIVATE
"LINKER:/WX" # Warnings as errors
)
// main.cpp
int main() {}
Clang-cl build output
$ mkdir build-clangcl && cd build-clangcl
$ cmake .. -DCMAKE_CXX_COMPILER=clang-cl -GNinja
$ ninja -v
[1/2] C:\Users\NICKHU~1\scoop\shims\clang-cl.exe /nologo -TP /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\test_exe.dir\main.cpp.obj /FdCMakeFiles\test_exe.dir\ -c ..\main.cpp
[2/2] cmd.exe /C "cd . && "C:\Users\Nick Hutchinson\scoop\apps\cmake\3.18.1\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\test_exe.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\mt.exe --manifests -- C:\Users\NICKHU~1\scoop\shims\lld-link.exe /nologo CMakeFiles\test_exe.dir\main.cpp.obj /out:test_exe.exe /implib:test_exe.lib /pdb:test_exe.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console /WX kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
Clang build output
$ mkdir build-clang && cd build-clang
$ cmake .. -DCMAKE_CXX_COMPILER=clang -GNinja
$ ninja -v
[1/2] C:\Users\NICKHU~1\scoop\shims\clang.exe -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -MD -MT CMakeFiles/test_exe.dir/main.cpp.obj -MF CMakeFiles\test_exe.dir\main.cpp.obj.d -o CMakeFiles/test_exe.dir/main.cpp.obj -c ../main.cpp
[2/2] cmd.exe /C "cd . && C:\Users\NICKHU~1\scoop\shims\clang.exe -fuse-ld=lld-link -nostartfiles -nostdlib -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd /WX CMakeFiles/test_exe.dir/main.cpp.obj -o test_exe.exe -Xlinker /implib:test_exe.lib -Xlinker /pdb:test_exe.pdb -Xlinker /version:0.0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames && cd ."
FAILED: test_exe.exe
cmd.exe /C "cd . && C:\Users\NICKHU~1\scoop\shims\clang.exe -fuse-ld=lld-link -nostartfiles -nostdlib -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd /WX CMakeFiles/test_exe.dir/main.cpp.obj -o test_exe.exe -Xlinker /implib:test_exe.lib -Xlinker /pdb:test_exe.pdb -Xlinker /version:0.0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames && cd ."
clang: error: no such file or directory: '/WX'
ninja: build stopped: subcommand failed.