Home / Alt manpages / x86_64-w64-mingw32-ar(1)

  • x86_64-w64-mingw32-ar(1)
  • User command
  • linux

Build and Inspect a MinGW-W64 Static Library with ar

You will create a Windows x86-64 static library from object or sample files, inspect its members and extract one again, using the installed x86_64-w64-mingw32-ar. This guide covers GNU Binutils 2.41.90.20240122, from Debian package binutils-mingw-w64-x86-64 version 2.41.90.20240122-1ubuntu1+11.4. The x86_64-w64-mingw32ucrt-ar name is an alias for the same archive tool in the installed manpage set.

Allow about ten minutes. You need a shell, the MinGW-W64 binutils package, and files you are allowed to read. The examples write only to a new working directory and do not require elevated privileges. Replace the example filenames with your own object files when you are ready.

1. Confirm the toolchain before changing files

Check both the command and the package version first:

$ x86_64-w64-mingw32-ar --version
GNU ar (GNU Binutils) 2.41.90.20240122

$ dpkg-query -W -f='${Package} ${Version}\n' binutils-mingw-w64-x86-64
binutils-mingw-w64-x86-64 2.41.90.20240122-1ubuntu1+11.4

The archive format is not a compiler output directory. It is one file containing members such as .o files. The archive can be passed to a linker later, but ar itself does not compile source code or link an executable.

Checkpoint

If the version command fails, stop here and fix the toolchain path or package installation. Do not substitute the host's unprefixed ar without checking its target format.

2. Create a normal static archive

Use r to replace a member with the same name or insert it if it is new. Add c to say that creating the archive is expected, and s to create or update the archive's symbol index:

$ mkdir -p build/lib-demo
$ cd build/lib-demo
$ x86_64-w64-mingw32-ar rcs libdemo.a calc.o strings.o

For a real build, calc.o and strings.o are normally produced by the MinGW-W64 compiler. If either file does not exist, ar reports an error and leaves an existing matching member undisturbed. The new members are appended by default.

The compact command is read as operation r plus modifiers c and s. You can write the same operation with a leading dash if preferred, but keep the operation and modifiers together so the command is easy to recognise in build logs.

Checkpoint

The command should return status 0 and create libdemo.a. Verify that without relying on a file browser:

$ test -s libdemo.a && printf '%s\n' 'archive created'
archive created

3. Inspect members and the index

Use t to list member names. Add v for permissions, owner, group, size and timestamp, or O for archive offsets:

$ x86_64-w64-mingw32-ar t libdemo.a
calc.o
strings.o

$ x86_64-w64-mingw32-ar tv libdemo.a
rw-r--r-- 0/0     123 Jan  1 01:00 1970 calc.o
rw-r--r-- 0/0     456 Jan  1 01:00 1970 strings.o

The zero ownership and epoch-like timestamp are expected for this installed tool because deterministic archives are enabled by default. That makes repeated builds reproducible when the inputs and options are identical. If preserving actual metadata is a requirement, U disables deterministic mode, but that makes the result depend on local file metadata.

A library needs an object-file index for normal linking. The s modifier creates it during the update. To rebuild the index on an existing archive, run the equivalent of ranlib:

$ x86_64-w64-mingw32-ar s libdemo.a
$ x86_64-w64-mingw32-nm -s libdemo.a

If your archive was deliberately made with S, it has no symbol table and cannot be used by the linker until you rebuild the index. That can be useful while assembling a large library in stages, but omit S on the final update or run ar s afterwards.

Checkpoint

ar t libdemo.a should show every intended member, and the final archive should have an index before you hand it to the linker.

4. Update or remove a member carefully

Run the same rcs command when an object has been rebuilt:

$ x86_64-w64-mingw32-ar rcs libdemo.a calc.o strings.o

Existing members with those names are replaced; new names are appended. Add u as rcsu only when timestamp comparison is genuinely useful. The installed manual disallows qu, and deterministic mode makes u ineffective because replacement is always selected. Check the result with t after an update.

Deletion is irreversible from the archive's point of view:

$ cp libdemo.a libdemo.a.before-delete
$ x86_64-w64-mingw32-ar d libdemo.a strings.o
$ x86_64-w64-mingw32-ar t libdemo.a
calc.o

The copy is the recovery path. If the deletion was a mistake, restore it with an ordinary file move or copy from libdemo.a.before-delete. Do not delete the backup until the consuming build has passed. No sudo is needed unless you deliberately placed the archive in a directory your user cannot write.

5. Extract a member into a controlled directory

Extraction writes files to disk, so use a new directory and list the member explicitly when you only need one:

$ mkdir -p extracted
$ x86_64-w64-mingw32-ar x --output extracted libdemo.a calc.o
$ test -f extracted/calc.o && printf '%s\n' 'member extracted'
member extracted

The --output option selects the destination, but x is still required. Without it, the current directory is used. Add o if the extracted file must retain its original archive date rather than receiving the extraction time.

Do not extract an untrusted archive into a directory containing important files. GNU ar applies restrictions to paths in archives, including rejecting absolute paths and parent-directory components in ordinary extraction. Full-path archives created with P are less portable and need particular care. Review the listing first, then extract to a disposable directory.

Thin archives are a separate boundary. A thin archive stores references to the original member files rather than their contents, and its members cannot be extracted with x. It is suitable for a local build tree whose object files will remain in place, not for a self-contained library you plan to copy elsewhere.

6. Keep paths and target formats explicit

By default, matching is based on the member name rather than the full input path. The P modifier preserves full paths, but archives made that way are not POSIX compliant and may not work with older tools. Avoid it unless the path is part of a deliberate, tested archive layout.

When you need to inspect an archive for a different object format, use --target with a supported BFD name. Ask this installed binary for its accepted targets rather than guessing:

$ x86_64-w64-mingw32-ar --help | sed -n '/supported targets:/p'
x86_64-w64-mingw32-ar: supported targets: pe-x86-64 pei-x86-64 pe-bigobj-x86-64 ...

Keep the target setting consistent with the objects you are packaging. A successful archive operation does not prove that a later linker can combine incompatible object formats.

Done means

  • The version and target-specific x86_64-w64-mingw32-ar command were confirmed.
  • The archive was created with r, c and s, and its member list was checked.
  • The symbol index is present, or the deliberate S choice has been followed by ar s.
  • Updates and deletions have a verified listing, with a backup made before destructive changes.
  • Extraction, when needed, was directed to a controlled directory and thin archives were not treated as self-contained.