What is the easiest way to create a libray from list of .o s
that is libtest.a from a.o,b.o ...z.o
I am looking for a command line option.

Dani AI

Generated

As discovered, the standard tool for packaging object files into a static library is the archiver ar. Below are concise, practical tips that complement the example already posted and cover common pitfalls you may hit later.

A common, portable pattern that both creates the archive and builds the symbol index in one step is:

ar rcs libtest.a a.o b.o c.o

Explanation: r inserts or replaces members, c creates the archive if needed, and s writes the symbol index so a separate ranlib step is normally unnecessary. See the for details.

Quick archive maintenance commands:

ar t libtest.a
ar x libtest.a member.o
ar d libtest.a member.o

If your toolchain does not write an index automatically, run ranlib libtest.a to generate it (see the ranlib man page).

Linking notes that commonly bite newcomers: place the static library after the object files that reference it (linkers search left-to-right and only pull needed members). Example usage:

gcc -o prog main.o -L. -ltest

To force inclusion of every object in the archive (rarely needed), wrap the library with the linker options --whole-archive / --no-whole-archive. For more on linker behavior see the ld documentation.

Final cautions: keep ABI and compiler flags consistent when creating C++ static libs, and check object file architecture (32 vs 64-bit). Use nm or objdump to inspect symbols if linking fails.

Found the solution using ar

ar rvu lib.a x.o y.o z.o
commented: Thanks :) +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.