ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.78k stars 2.54k forks source link

zig cc: don't invoke the linker for non-ELF targets when -c is passed #7094

Open demizer opened 3 years ago

demizer commented 3 years ago

I'm getting the following error with zig build (c++).

build.zig

clang version 11.0.0 (git@github.com:ziglang/zig-bootstrap.git ddf83b9ed342a061dc82003c9e74e9b32f04393f)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/10.2.0
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/10.2.0
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.2.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
 (in-process)
 "/home/demizer/zig-linux-x86_64-0.7.0/zig" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name webview.cc -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -v -nostdsysteminc -nobuiltininc -resource-dir /home/demizer/lib/clang/11.0.0 -dependency-file zig-cache/tmp/c94025227052edd4-webview.o.d -MT zig-cache/tmp/c94025227052edd4-webview.o -sys-header-deps -MV -isystem /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libcxx/include -isystem /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libcxxabi/include -isystem /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/include -isystem /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libc/include/x86_64-linux-gnu -isystem /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libc/include/generic-glibc -isystem /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libc/include/x86_64-linux-any -isystem /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libc/include/any-linux-any -isystem /usr/local/include -isystem /usr/include/x86_64-linux-musl -isystem /usr/include -D _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D _LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -D _DEBUG -D WEBVIEW_GTK -I /usr/include/gtk-3.0 -I /usr/include/pango-1.0 -I /usr/include/glib-2.0 -I /usr/lib/glib-2.0/include -I /usr/include/harfbuzz -I /usr/include/freetype2 -I /usr/include/libpng16 -I /usr/include/libmount -I /usr/include/blkid -I /usr/include/fribidi -I /usr/include/cairo -I /usr/include/pixman-1 -I /usr/include/gdk-pixbuf-2.0 -I /usr/include/gio-unix-2.0 -I /usr/include/cloudproviders -I /usr/include/atk-1.0 -I /usr/include/at-spi2-atk/2.0 -I /usr/include/dbus-1.0 -I /usr/lib/dbus-1.0/include -I /usr/include/at-spi-2.0 -I /usr/include/webkitgtk-4.0 -I /usr/include/libsoup-2.4 -I /usr/include/libxml2 -Og -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /data/code/zig/markzig -ferror-limit 19 -fsanitize=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound -fsanitize-trap=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound -pthread -stack-protector 2 -stack-protector-buffer-size 4 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -fno-spell-checking -target-cpu znver1 -target-feature -3dnow -target-feature -3dnowa -target-feature +64bit -target-feature +adx -target-feature +aes -target-feature -amx-bf16 -target-feature -amx-int8 -target-feature -amx-tile -target-feature +avx -target-feature +avx2 -target-feature -avx512bf16 -target-feature -avx512bitalg -target-feature -avx512bw -target-feature -avx512cd -target-feature -avx512dq -target-feature -avx512er -target-feature -avx512f -target-feature -avx512ifma -target-feature -avx512pf -target-feature -avx512vbmi -target-feature -avx512vbmi2 -target-feature -avx512vl -target-feature -avx512vnni -target-feature -avx512vp2intersect -target-feature -avx512vpopcntdq -target-feature +bmi -target-feature +bmi2 -target-feature +branchfusion -target-feature -cldemote -target-feature +clflushopt -target-feature -clwb -target-feature +clzero -target-feature +cmov -target-feature +cx16 -target-feature +cx8 -target-feature -enqcmd -target-feature -ermsb -target-feature +f16c -target-feature -false-deps-lzcnt-tzcnt -target-feature -false-deps-popcnt -target-feature -fast-11bytenop -target-feature +fast-15bytenop -target-feature -fast-7bytenop -target-feature +fast-bextr -target-feature -fast-gather -target-feature -fast-hops -target-feature +fast-lzcnt -target-feature -fast-scalar-fsqrt -target-feature +fast-scalar-shift-masks -target-feature -fast-shld-rotate -target-feature -fast-variable-shuffle -target-feature -fast-vector-fsqrt -target-feature -fast-vector-shift-masks -target-feature +fma -target-feature -fma4 -target-feature +fsgsbase -target-feature +fxsr -target-feature -gfni -target-feature -idivl-to-divb -target-feature -idivq-to-divl -target-feature -invpcid -target-feature -lea-sp -target-feature -lea-uses-ag -target-feature -lvi-cfi -target-feature -lvi-load-hardening -target-feature -lwp -target-feature +lzcnt -target-feature -macrofusion -target-feature -merge-to-threeway-branch -target-feature +mmx -target-feature +movbe -target-feature -movdir64b -target-feature -movdiri -target-feature -mpx -target-feature +mwaitx -target-feature +nopl -target-feature -pad-short-functions -target-feature +pclmul -target-feature -pconfig -target-feature -pku -target-feature +popcnt -target-feature -prefer-128-bit -target-feature -prefer-256-bit -target-feature -prefer-mask-registers -target-feature -prefetchwt1 -target-feature +prfchw -target-feature -ptwrite -target-feature -rdpid -target-feature +rdrnd -target-feature +rdseed -target-feature -retpoline -target-feature -retpoline-external-thunk -target-feature -retpoline-indirect-branches -target-feature -retpoline-indirect-calls -target-feature -rtm -target-feature +sahf -target-feature -serialize -target-feature -seses -target-feature -sgx -target-feature +sha -target-feature -shstk -target-feature -slow-3ops-lea -target-feature -slow-incdec -target-feature -slow-lea -target-feature -slow-pmaddwd -target-feature -slow-pmulld -target-feature +slow-shld -target-feature -slow-two-mem-ops -target-feature -slow-unaligned-mem-16 -target-feature -slow-unaligned-mem-32 -target-feature -soft-float -target-feature +sse -target-feature -sse-unaligned-mem -target-feature +sse2 -target-feature +sse3 -target-feature +sse4.1 -target-feature +sse4.2 -target-feature +sse4a -target-feature +ssse3 -target-feature -tbm -target-feature -tsxldtrk -target-feature -use-aa -target-feature -use-glm-div-sqrt-costs -target-feature -vaes -target-feature -vpclmulqdq -target-feature +vzeroupper -target-feature -waitpkg -target-feature -wbnoinvd -target-feature +x87 -target-feature -xop -target-feature +xsave -target-feature +xsavec -target-feature +xsaveopt -target-feature +xsaves -faddrsig -o zig-cache/tmp/c94025227052edd4-webview.o -x c++ src/webview/webview.cc
clang -cc1 version 11.0.0 based upon LLVM 11.0.0 default target x86_64-linux-musl
ignoring nonexistent directory "/usr/include/x86_64-linux-musl"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/gtk-3.0
 /usr/include/pango-1.0
 /usr/include/glib-2.0
 /usr/lib/glib-2.0/include
 /usr/include/harfbuzz
 /usr/include/freetype2
 /usr/include/libpng16
 /usr/include/libmount
 /usr/include/blkid
 /usr/include/fribidi
 /usr/include/cairo
 /usr/include/pixman-1
 /usr/include/gdk-pixbuf-2.0
 /usr/include/gio-unix-2.0
 /usr/include/cloudproviders
 /usr/include/atk-1.0
 /usr/include/at-spi2-atk/2.0
 /usr/include/dbus-1.0
 /usr/lib/dbus-1.0/include
 /usr/include/at-spi-2.0
 /usr/include/webkitgtk-4.0
 /usr/include/libsoup-2.4
 /usr/include/libxml2
 /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libcxx/include
 /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libcxxabi/include
 /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/include
 /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libc/include/x86_64-linux-gnu
 /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libc/include/generic-glibc
 /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libc/include/x86_64-linux-any
 /home/demizer/zig-linux-x86_64-0.7.0/lib/zig/libc/include/any-linux-any
 /usr/local/include
 /usr/include
End of search list.
lld: error: unable to find library -lwebkit2gtk-4.0
lld: error: unable to find library -lgtk-3
lld: error: unable to find library -lgdk-3
lld: error: unable to find library -lpangocairo-1.0
lld: error: unable to find library -lpango-1.0
lld: error: unable to find library -lharfbuzz
lld: error: unable to find library -latk-1.0
lld: error: unable to find library -lcairo-gobject
lld: error: unable to find library -lcairo
lld: error: unable to find library -lgdk_pixbuf-2.0
lld: error: unable to find library -lsoup-2.4
lld: error: unable to find library -lgio-2.0
lld: error: unable to find library -ljavascriptcoregtk-4.0
lld: error: unable to find library -lgobject-2.0
lld: error: unable to find library -lglib-2.0
error: LLDReportedFailure
The following command exited with error code 1:
zig c++ -v -c src/webview/webview.cc -DWEBVIEW_GTK -std=c++11 -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cloudproviders -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -I/usr/include/at-spi-2.0 -I/usr/include/webkitgtk-4.0 -I/usr/include/libsoup-2.4 -pthread -I/usr/include/libxml2 -lwebkit2gtk-4.0 -lgtk-3 -lgdk-3 -lz -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lsoup-2.4 -lgio-2.0 -ljavascriptcoregtk-4.0 -lgobject-2.0 -lglib-2.0 -o src/webview/webview.o
error: the following build command failed with exit code 1:

When I change zig c++ to g++ the it works. I haven't used this bit of my project in a while, so not sure when it broke. I assume it occurred after the llvm 11 upgrade.

My setup:

OS: Arch Linux Zig Version: 0.7.0 installed from the tar.gz available on the downloads page.

This looks similar to #6996

LemonBoy commented 3 years ago

zig c++ is trying to statically link all the libraries you've specified and LLD rightfully complains because there's no .a available on the system.

Using clang++ instead of zig c++ works because it defaults to dynamic linking every library and if you specify -static you're greeted with a bunch of warnings:

warning: -lgdk-3: 'linker' input unused [-Wunused-command-line-argument]
demizer commented 3 years ago

@LemonBoy thanks for the reply.

Arch Linux does not package static libraries and it would be a huge mess to compile all of these dependencies to get the static libs. As I stated in the bug description, this worked before with zig c++, does anyone know if this feature of can be disabled? When i try to compile with -shared i get new errors:

lld: error: attempted static link of dynamic object /lib64/libwebkit2gtk-4.0.so
lld: error: attempted static link of dynamic object /lib64/libgtk-3.so
lld: error: attempted static link of dynamic object /lib64/libgdk-3.so
lld: error: attempted static link of dynamic object /lib64/libz.so
lld: error: attempted static link of dynamic object /lib64/libpangocairo-1.0.so
lld: error: attempted static link of dynamic object /lib64/libpango-1.0.so
lld: error: attempted static link of dynamic object /lib64/libharfbuzz.so
lld: error: attempted static link of dynamic object /lib64/libatk-1.0.so
lld: error: attempted static link of dynamic object /lib64/libcairo-gobject.so
lld: error: attempted static link of dynamic object /lib64/libcairo.so
lld: error: attempted static link of dynamic object /lib64/libgdk_pixbuf-2.0.so
lld: error: attempted static link of dynamic object /lib64/libsoup-2.4.so
lld: error: attempted static link of dynamic object /lib64/libgio-2.0.so
lld: error: attempted static link of dynamic object /lib64/libjavascriptcoregtk-4.0.so
lld: error: attempted static link of dynamic object /lib64/libgobject-2.0.so
lld: error: attempted static link of dynamic object /lib64/libglib-2.0.so
LemonBoy commented 3 years ago

As I stated in the bug description, this worked before with zig c++, does anyone know if this feature of can be disabled?

It's not a feature, it's a bug :)

zig cc keeps invoking the linker even though -c is specified (the presence of -c, -S or -E skips the linker invocation.

It doesn't also handle the -c <file1> <file2> -o <name> case that clang/gcc rightfully reject as you can't specify a single -o for two files.

andrewrk commented 3 years ago

It doesn't also handle the -c <file1> <file2> -o <name> case that clang/gcc rightfully reject as you can't specify a single -o for two files.

Zig actually supports this case, taking advantage of the -r flag of LLD to link multiple objects into one. However that only works for WASM and ELF.

For the purposes of this issue, I think for 0.7.1 this will look like simply omitting the -l arguments when the output mode is an object in the LLD linker line. That will only solve it for ELF and WASM.

For completely solving this issue, it will look like skipping the linking step altogether when building objects. This will be more involved because we currently rely on the fact that the linker step produces a new build artifact based on another one. So we will either do a simple copy or make the caching system more complex.