.o files versus .a files

What is the difference between these two types of files. I see that my C ++ application binds to both types while creating an executable.

How to create .a files? Links, links and especially examples are highly appreciated.

+43
c ++ c gcc linux build-process
Mar 17 '09 at 15:20
source share
5 answers

.o files are objects. This is the result of the compiler and the input for the linker / librarian.

.a files are archives. They are groups of objects or static libraries, and are also entered into the linker.

Additional content

I did not notice part of the "examples" of your question. Typically, you will use a makefile to create static libraries.

 AR = ar CC = gcc objects := hello.o world.o libby.a: $(objects) $(AR) rcu $@ $(objects) %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ 

This will compile hello.c and world.c into objects and then archive them into a library. Depending on the platform, you may also need to run a utility called ranlib to create a table of contents in the archive.

An interesting note: .a files are files with technical archives, not libraries. They are similar to uncompressed zip files, although they use a much older file format. A table of contents created by utilities such as ranlib is what makes an archive a library. Java archive files ( .jar ) are similar in that they are zip files that have some special directory structures created by the Java archiver.

+46
Mar 17 '09 at 15:24
source share

An .o file is the result of compiling a single compilation unit (mainly a source file with corresponding header files), while a .a file is one or more .o files packaged as a library.

+13
Mar 17 '09 at 15:30
source share

D Good answer Shawley, I just wanted to add a couple of points, because the other answers reflect an incomplete understanding of what is happening.

Keep in mind that archive files (.a) are not limited to containing object files (.o). They may contain arbitrary files. Not often useful, but watch the dynamic linking of dependent information built into the archive for a silly linker trick.

Also note that object files (.o) are not necessarily the result of a single compilation unit. You can partially link several smaller object files into one larger file.

http://www.mihaiu.name/2002/library_development_linux/ - search on this page for "partial"

+11
Mar 17 '09 at 17:13
source share

You can use ar to create an .a file (static library) from .o files (object files)

See man ar more details.

+5
Mar 17 '09 at 16:02
source share

I believe a .a file is an archive that can contain multiple object files.

+1
Mar 17 '09 at 15:25
source share



All Articles