Specify target glibc at command line

tl;dr.

“how do I target a glibc version via cmdline.”

Preamble

Hello I’m working towards being able to target an older version of glibc as my company has upgraded from centos7 to rocky9.

Background

So,

I am close to building from source via a build.zig fully for rocky9. (Close enough that I can hand it a couple things I’ve done manually and successfully compile and run.)

So I’d like to try to target the older glibc now. (Specifically 2.17.0 I think).

Unfortunately, one of the manual steps I’m doing is compiling spdlog with zig cc via cmake.

CC="zig cc" CXX="zig c++" cmake -B build_zig

Which will then emit a static lib I can use later. Writing the build.zig for spdlog looked to be a headache so if possible I’d like to avoid fully fleshing that out at present even if I’m going to do it eventually in the long term.

So all that to say how do I target a glibc version via cmdline. It’s likely something to do with -target.

Then I can see if my backwards compilation proof of concept would work.

Thanks in advance,

P.S. Do I need to be recompiling the libraries. It seems like an obvious yes givin my builds won’t run if I don’t recompile spdlog. The usual method would be to compile spdlog with g++ and then link. But if I try to do that with zig (Clang) it gets mad at me. Recompiling with the aforementioned CC=... cmd seems to fix that though.

I think my company is using one of the package installs of spdlog so recompiling spdlog from source may be unnecessary if it’s not required. (It seems like it is.)

The gnu libc version follows the gnu abi:
zig cc -target x86_64-linux-gnu.2.17

To list all the available versions: zig targets | jq -r .glibc[] (2.0-2.38)

4 Likes

Hey dimdin,

I always appreciate the help.

I’ve another question for you.

So I’m playing with zig at work, and my code works and all, but if I want to get people to actually use my thing it needs to be as frictionless as possible.

So here is the new problem. I’m getting the following error.

undefined reference to `thingy::Subscribe(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,
 std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool)

I assume this is because I’m building a shared library with zig and therefore clang while the rest of my company will be building with g++.

I’ve done a bit of research and a bit of asking chatgpt about the problem.

It sounds like I may need to pass -stdlib=libstdc++ to clang to make sure I’m creating the .so with the same backend std libs. I tried to just toss that into my cpp flags in build.zig but I got the following.

masking service names with -----

install
└─ install -----
   └─ zig build-exe ------- Debug native
      └─ zig build-lib ------ Debug native 4 errors
:1:1: error: argument unused during compilation: '-stdlib=libstdc++'
:1:1: error: argument unused during compilation: '-stdlib=libstdc++'
:1:1: error: argument unused during compilation: '-stdlib=libstdc++'
:1:1: error: argument unused during compilation: '-stdlib=libstdc++'
error: the following command failed with 4 compilation errors:

So I clearly can’t just do that.

Do you know how to pass this argument successfully to clang. I perused the functions attached to my lib in neovim and didn’t see anything obvious.

Oh and to be clear I know for a fact that function is defined.

I don’t know and I cannot find a way to prevent linking to libstdc++
I noticed that -nostdinc++ is accepted as an argument.
An idea is to compile with:

zig c++ -nostdinc++ -I/path/to/libc++/include -o file.o file.cpp

and then link using:

zig ld.lld --nostdlib file.o -L/path/to/libc++/lib -lc++

The gnu libc version follows the gnu abi:
zig cc -target x86_64-linux-gnu.2.17

@dimdin sorry to wake that old thread up but do you happen to know how this works internally? Looking at the command invoked by zig I don’t see anything obvious.

cc "/snap/zig/12069/zig" -cc1 -triple x86_64-unknown-linux-gnu \ -emit-obj -mrelax-all \ -disable-free \ -clear-ast-before-backend \ -disable-llvm-verifier \ -discard-value-names \ -main-file-name test.c \ -mrelocation-model pic \ -pic-level 2 \ -fhalf-no-semantic-interposition \ -mframe-pointer=all \ -fmath-errno \ -ffp-contract=on \ -fno-rounding-math \ -mconstructor-aliases \ -funwind-tables=2 \ -target-cpu x86-64 \ -tune-cpu generic \ -debug-info-kind=constructor \ -dwarf-version=4 \ -debugger-tuning=gdb \ -gdwarf32 \ -fdebug-compilation-dir=/tmp \ -v \ -fcoverage-compilation-dir=/tmp \ -nostdsysteminc \ -nobuiltininc \ -resource-dir /snap/zig/lib/clang/18 \ -dependency-file /home/user/.cache/zig/tmp/8cb6fd8962396662-test.o.d \ -MT /home/user/.cache/zig/tmp/8cb6fd8962396662-test.o \ -sys-header-deps \ -MV \ -isystem /snap/zig/12069/lib/include \ -isystem /snap/zig/12069/lib/libc/include/x86_64-linux-gnu \ -isystem /snap/zig/12069/lib/libc/include/generic-glibc \ -isystem /snap/zig/12069/lib/libc/include/x86-linux-any \ -isystem /snap/zig/12069/lib/libc/include/any-linux-any \ -D __GLIBC_MINOR__=3 \ -D _DEBUG \ -O0 \ -ferror-limit 19 \ -fsanitize=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,integer-divide-by-zero,nonnull-attribute,null,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,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound \ -fno-sanitize-memory-param-retval \ -fno-sanitize-address-use-odr-indicator \ -stack-protector 2 \ -stack-protector-buffer-size 4 \ -fgnuc-version=4.2.1 \ -fskip-odr-check-in-gmf \ -fcolor-diagnostics \ -fno-spell-checking \ -target-cpu x86-64 \ -target-feature \ -16bit-mode \ -target-feature \ -32bit-mode \ -target-feature \ -3dnow \ -target-feature \ -3dnowa \ -target-feature +64bit \ -target-feature \ -adx \ -target-feature \ -aes \ -target-feature \ -allow-light-256-bit \ -target-feature \ -amx-bf16 \ -target-feature \ -amx-complex \ -target-feature \ -amx-fp16 \ -target-feature \ -amx-int8 \ -target-feature \ -amx-tile -target-feature -avx -target-feature -avx10.1-256 -target-feature -avx10.1-512 -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 \ -avx512fp16 \ -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 \ -avxifma \ -target-feature \ -avxneconvert \ -target-feature \ -avxvnni \ -target-feature \ -avxvnniint16 \ -target-feature \ -avxvnniint8 \ -target-feature \ -bmi \ -target-feature \ -bmi2 \ -target-feature \ -branchfusion \ -target-feature \ -ccmp \ -target-feature \ -cf \ -target-feature \ -cldemote \ -target-feature \ -clflushopt \ -target-feature \ -clwb \ -target-feature \ -clzero \ -target-feature +cmov \ -target-feature \ -cmpccxadd \ -target-feature \ -crc32 \ -target-feature \ -cx16 \ -target-feature +cx8 \ -target-feature \ -egpr \ -target-feature \ -enqcmd \ -target-feature \ -ermsb \ -target-feature \ -evex512 \ -target-feature \ -f16c \ -target-feature \ -false-deps-getmant \ -target-feature \ -false-deps-lzcnt-tzcnt \ -target-feature \ -false-deps-mulc \ -target-feature \ -false-deps-mullq \ -target-feature \ -false-deps-perm \ -target-feature \ -false-deps-popcnt \ -target-feature \ -false-deps-range \ -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-movbe \ -target-feature \ -fast-scalar-fsqrt \ -target-feature \ -fast-scalar-shift-masks \ -target-feature \ -fast-shld-rotate \ -target-feature \ -fast-variable-crosslane-shuffle \ -target-feature \ -fast-variable-perlane-shuffle \ -target-feature \ -fast-vector-fsqrt \ -target-feature \ -fast-vector-shift-masks \ -target-feature \ -faster-shift-than-shuffle \ -target-feature \ -fma \ -target-feature \ -fma4 \ -target-feature \ -fsgsbase \ -target-feature \ -fsrm \ -target-feature +fxsr \ -target-feature \ -gfni \ -target-feature \ -harden-sls-ijmp \ -target-feature \ -harden-sls-ret \ -target-feature \ -hreset \ -target-feature \ -idivl-to-divb \ -target-feature +idivq-to-divl \ -target-feature \ -invpcid \ -target-feature \ -kl \ -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 +mmx \ -target-feature \ -movbe \ -target-feature \ -movdir64b \ -target-feature \ -movdiri \ -target-feature \ -mwaitx \ -target-feature \ -ndd \ -target-feature \ -no-bypass-delay \ -target-feature \ -no-bypass-delay-blend \ -target-feature \ -no-bypass-delay-mov \ -target-feature \ -no-bypass-delay-shuffle \ -target-feature +nopl \ -target-feature \ -pad-short-functions \ -target-feature \ -pclmul \ -target-feature \ -pconfig \ -target-feature \ -pku \ -target-feature \ -popcnt \ -target-feature \ -ppx \ -target-feature \ -prefer-128-bit \ -target-feature \ -prefer-256-bit \ -target-feature \ -prefer-mask-registers \ -target-feature \ -prefer-movmsk-over-vtest \ -target-feature \ -prefer-no-gather \ -target-feature \ -prefer-no-scatter \ -target-feature \ -prefetchi \ -target-feature \ -prefetchwt1 \ -target-feature \ -prfchw \ -target-feature \ -ptwrite \ -target-feature \ -push2pop2 \ -target-feature \ -raoint \ -target-feature \ -rdpid \ -target-feature \ -rdpru \ -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 \ -sbb-dep-breaking \ -target-feature \ -serialize \ -target-feature \ -seses \ -target-feature \ -sgx \ -target-feature \ -sha \ -target-feature \ -sha512 \ -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 \ -sm3 \ -target-feature \ -sm4 \ -target-feature \ -soft-float \ -target-feature +sse \ -target-feature +sse2 \ -target-feature \ -sse3 \ -target-feature \ -sse4.1 \ -target-feature \ -sse4.2 \ -target-feature \ -sse4a \ -target-feature \ -sse-unaligned-mem \ -target-feature \ -ssse3 \ -target-feature \ -tagged-globals \ -target-feature \ -tbm \ -target-feature \ -tsxldtrk \ -target-feature \ -tuning-fast-imm-vector-shift \ -target-feature \ -uintr \ -target-feature \ -use-glm-div-sqrt-costs \ -target-feature \ -use-slm-arith-costs \ -target-feature \ -usermsr \ -target-feature \ -vaes \ -target-feature \ -vpclmulqdq \ -target-feature +vzeroupper \ -target-feature \ -waitpkg \ -target-feature \ -wbnoinvd \ -target-feature \ -widekl \ -target-feature +x87 \ -target-feature \ -xop \ -target-feature \ -xsave \ -target-feature \ -xsavec \ -target-feature \ -xsaveopt \ -target-feature \ -xsaves \ -faddrsig \ -D__GCC_HAVE_DWARF2_CFI_ASM=1 \ -o /home/user/.cache/zig/tmp/8cb6fd8962396662-test.o \ -x c test.c
ld ld.lld --error-limit=0 --entry _start -z stack-size=16777216 --image-base=16777216 --eh-frame-hdr -znow -m elf_x86_64 -o a.out \ /home/jedi/.cache/zig/o/1d1ad9560471a44302c499aa7385f0bc/Scrt1.o \ /home/jedi/.cache/zig/o/c56284889c252163c8d7a9560acd85c9/crti.o \ -dynamic-linker /lib64/ld-linux-x86-64.so.2 /home/jedi/.cache/zig/o/868d78c4e45cd194c709a41da2c94140/test.o --as-needed /home/jedi/.cache/zig/o/1f5224a38a51734217e98151d200475f/libm.so.6 /home/jedi/.cache/zig/o/1f5224a38a51734217e98151d200475f/libpthread.so.0 /home/jedi/.cache/zig/o/1f5224a38a51734217e98151d200475f/libc.so.6 /home/jedi/.cache/zig/o/1f5224a38a51734217e98151d200475f/libdl.so.2 /home/jedi/.cache/zig/o/1f5224a38a51734217e98151d200475f/librt.so.1 /home/jedi/.cache/zig/o/1f5224a38a51734217e98151d200475f/libld.so.2 /home/jedi/.cache/zig/o/1f5224a38a51734217e98151d200475f/libutil.so.1 /home/jedi/.cache/zig/o/1f5224a38a51734217e98151d200475f/libresolv.so.2 /home/jedi/.cache/zig/o/75105516053499aedff2b96fbdd26bf4/libc_nonshared.a /home/jedi/.cache/zig/o/e57bf572e4fe567ea6c753fe52a9003f/libcompiler_rt.a /home/jedi/.cache/zig/o/0a555821cb19386cbf7c211cfd117a9f/crtn.o --allow-shlib-undefined

And is it possible to achieve the same think with vanilla ld ? I saw this project trying to achieve that but it seems a little convoluted.

The readme here might be helpful:

2 Likes

Wow ok so there is actually a lot of work to make this happen. My conclusion is that there is definitely no magical ld option for this.