Abhinav
Abhinav

Reputation: 38162

How to list files inside a .a file

How can I get to know what files are part of my .a file? Which command should I use?

Upvotes: 19

Views: 17680

Answers (3)

Antoine Pelisse
Antoine Pelisse

Reputation: 13129

I think it would be simpler to use ar for what it's meant (manipulating archives)

ar t foo.a

From man page:

-t List the specified files in the order in which they appear in the archive, each on a separate line. If no files are specified, all files in the archive are listed.

man 1 ar for more info.

Upvotes: 19

Abhinav
Abhinav

Reputation: 38162

This worked with below command: nm <> | grep ".o" | sed "s/.(//g" | sed "s/).//g" | uniq

Upvotes: 0

greg
greg

Reputation: 4953

If you didn't strip symbols, you can use nm to see the names of the object files (.o).

$ nm foo.a
foo.a(objectFile1.o):
0000000000000000 T _function1
foo.a(objectFile2.o):
0000000000000000 T _function2

This output will differ on different operating systems. For instance, here is the output of the symbols within libz.a on a Ubuntu system:

$ nm libz.a | grep ".o:"
adler32.o:
compress.o:
crc32.o:
gzio.o:
uncompr.o:
deflate.o:
trees.o:
zutil.o:
inflate.o:
infback.o:
inftrees.o:
inffast.o:

Each of the above 12 .o files were translation units in the original code, compiled with the -c option to gcc. I used grep to ignore the countless symbols defined and referenced in each of the object files. After compilation, these object files were then combined into a static library with ar.

ar can also extract the object files. On the same libz.a:

$ ar xv libz.a 
x - adler32.o
x - compress.o
x - crc32.o
x - gzio.o
x - uncompr.o
x - deflate.o
x - trees.o
x - zutil.o
x - inflate.o
x - infback.o
x - inftrees.o
x - inffast.o

The x option is for extract and v is verbose, which prints the name of the object file as it is extracted.

Upvotes: 18

Related Questions