Home / Alt manpages / x86_64-linux-gnu-gcc-ar-13(1)

  • x86_64-linux-gnu-gcc-ar-13(1)
  • User command
  • linux

Build a GCC 13 Static Archive with gcc-ar

Plain ar works fine, until link-time optimisation needs a plugin option it does not know to add. That is the whole reason x86_64-linux-gnu-gcc-ar-13 exists: it wraps ar, adds the GCC 13 plugin flag for you, and otherwise behaves exactly like the tool it wraps.

Allow about ten minutes. You need the GCC 13 compiler, the installed x86_64-linux-gnu-gcc-ar-13 wrapper, a writable working directory, and ordinary shell access. The examples do not need sudo.

Warning

These commands create and replace files in the directory you choose, so do not run them in a directory containing an archive you care about.

This machine provides GCC package version 13.3.0-6ubuntu2~24.04.1, and the wrapper invokes GNU ar 2.47.20260726. Package and binutils versions differ across distributions; check yours before relying on diagnostic wording.

1. Check the wrapper before using it

The installed manpage defines gcc-ar as a wrapper around ar that adds the appropriate --plugin option for GCC 13. It accepts ar's own options and arguments rather than defining a separate archive format.

$ command -v x86_64-linux-gnu-gcc-ar-13
/usr/bin/x86_64-linux-gnu-gcc-ar-13
$ x86_64-linux-gnu-gcc-ar-13 --version
GNU ar (GNU Binutils) 2.47.20260726
$ dpkg-query -W -f='${Package} ${Version}\n' gcc-13 gcc-13-x86-64-linux-gnu
gcc-13 13.3.0-6ubuntu2~24.04.1
gcc-13-x86-64-linux-gnu 13.3.0-6ubuntu2~24.04.1

Checkpoint

The first command must resolve to the wrapper you intend to use. If it prints a different path, use that path consistently in the remaining commands, or fix the toolchain selection before building.

2. Compile a small object file

Make a source file in a clean temporary directory, then compile it without linking. -c produces an object file and creates no executable.

$ workdir=$(mktemp -d)
$ printf '%s\n' 'int answer(void) { return 42; }' > "$workdir/answer.c"
$ x86_64-linux-gnu-gcc-13 -c "$workdir/answer.c" -o "$workdir/answer.o"
$ file "$workdir/answer.o"
/tmp/tmp.ABC123/answer.o: ELF 64-bit LSB relocatable, x86-64, ...

The temporary directory name and the full file description vary. What matters is a successful compiler exit status and an object file at the requested path. If x86_64-linux-gnu-gcc-13 is missing, install or select the matching compiler through your normal package process; changing the archive command will not fix a missing compiler.

3. Create the archive with the wrapper

Combine ar's operations: r inserts or replaces a member, c suppresses the warning when creating a new archive, and s creates or updates the symbol index. The compact form is rcs.

$ x86_64-linux-gnu-gcc-ar-13 rcs "$workdir/libanswer.a" "$workdir/answer.o"
$ test -s "$workdir/libanswer.a" && echo "archive created"
archive created

Safety warning

That command changes the archive named by libanswer.a. The r operation replaces an existing member with the same name, so treat it as an overwrite. Choose a new output path first if you want to preserve an existing archive. Do not add sudo: elevated privileges do not improve the wrapper's plugin handling, and can leave root-owned build files behind.

The wrapper supplies the plugin selection that GCC's link-time optimisation workflow may need. For an ordinary non-LTO object, the result is still a normal ar archive: the wrapper does not compile source, link a program, or turn the archive into a shared library.

4. Inspect the member and its index

Use the same wrapper for read-only ar operations. t lists archive members, while nm -s prints the archive's symbol table when one is present.

$ x86_64-linux-gnu-gcc-ar-13 t "$workdir/libanswer.a"
answer.o
$ nm -s "$workdir/libanswer.a"

Archive index:
answer

answer.o:
0000000000000000 T answer

Exact addresses and formatting can vary with the toolchain. The checkpoint is that answer.o appears in the table and the index names answer. If the member is present but no index shows, run the wrapper with the standalone s operation, then inspect again:

$ x86_64-linux-gnu-gcc-ar-13 s "$workdir/libanswer.a"
$ nm -s "$workdir/libanswer.a" | sed -n '1,5p'

The installed ar documentation says an index speeds linking against the library. It is metadata inside the archive, not a separate file you need to copy.

Compile a caller and link it against the archive with the GCC 13 driver. Put the object before the library on the command line, and use -L and -lanswer to select libanswer.a.

$ printf '%s\n' '#include <stdio.h>' 'int answer(void);' 'int main(void) { printf("%d\n", answer()); }' > "$workdir/main.c"
$ x86_64-linux-gnu-gcc-13 "$workdir/main.c" -L"$workdir" -lanswer -o "$workdir/app"
$ "$workdir/app"
42

If the linker reports an undefined reference, check the archive name, the library search path, and the symbol index. If it reports that the archive cannot be found, inspect the exact path with ls -l "$workdir/libanswer.a". These are ordinary commands and need no elevated privileges.

6. Clean up without touching other files

Once you have checked the result, remove only the temporary directory created for this exercise:

$ rm -rf -- "$workdir"
$ test ! -e "$workdir" && echo "temporary files removed"
temporary files removed

Warning

This deletion is irreversible. If you need the archive or executable, copy them to a deliberate project path before running it. If you accidentally used an existing directory instead of the value from mktemp -d, stop and restore from your project's backup rather than deleting more files.

Common traps

  • Using plain ar for a GCC 13 LTO workflow. The wrapper exists to add the appropriate GCC plugin option: use the matching gcc-ar wrapper when the build uses GCC's archive tooling.
  • Confusing an archive with a shared library. A .a file stores object members for static linking. It is not loaded at runtime like a .so.
  • Forgetting s. A member can exist while the symbol index is missing or stale. Create or update it, then verify with nm -s.
  • Overwriting by accident. rcs replaces a same-named member. Use a new archive path when testing changes you may need to undo.
  • Trusting a successful archive command too much. Inspect the member, inspect the index, and run a small link test. Each checks a different part of the workflow.

Done means

  • Wrapper checked: the GCC 13 wrapper and its package version were confirmed.
  • Object compiled: a relocatable object built without elevated privileges.
  • Archive verified: libanswer.a contains answer.o and has a visible symbol index.
  • Link test passed: the GCC 13 link test ran and printed 42.
  • Overwrite risk understood: you know rcs can replace an existing archive member.
  • Cleaned up: temporary files were removed only after the output was checked.