LINK_WHAT_YOU_USE hides linker errors with Ninja
Originally filed as a Ninja issue. Copied here for ease of reference:
I use cmake 3.12.1 and ninja (1.8.2). When using Wl,--no-undefined -Wl,--no-allow-shlib-undefined linker options, a linker error is not recognized by ninja, error code is 0. Using Unix Makefiles generator works as expected. Sample code is here: ninjabug.zip
cmake --build . || echo "error"
[1/1] : && /usr/bin/c++ -fPIC -Wl,--no-as-needed -Wl,--no-undefined -Wl,--no-allow-shlib-undefined -shared -Wl,-soname,libninjabug.so -o libninjabug.so CMakeFiles/ninjabug.dir/ninjabug.cpp.o && /usr/bin/cmake -E __run_co_compile --lwyu=libninjabug.so || true && :
CMakeFiles/ninjabug.dir/ninjabug.cpp.o: In function `ninjabug()':
ninjabug.cpp:(.text+0x5): undefined reference to `HelloWorld()'
collect2: error: ld returned 1 exit status
Using Makefiles is like this:
cmake --build . || echo "error"
[ 50%] Linking CXX shared library libninjabug.so
CMakeFiles/ninjabug.dir/ninjabug.cpp.o: In function `ninjabug()':
ninjabug.cpp:(.text+0x5): undefined reference to `HelloWorld()'
collect2: error: ld returned 1 exit status
CMakeFiles/ninjabug.dir/build.make:83: recipe for target 'libninjabug.so' failed
make[2]: *** [libninjabug.so] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/ninjabug.dir/all' failed
make[1]: *** [CMakeFiles/ninjabug.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
error
I confirmed that commenting out the CMAKE_LINK_WHAT_YOU_USE
setting fixes the example with 3.12.1. This is due to the Ninja command line ending in || true
at the end, hiding the link failure. Instead, it should probably be && ( lwyu_command || true )
if that is possible.
It looks like LWYU was added in 3.7.0-rc1 via e4cb7d76.