Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

command to compile c files with .a files

Tags:

c

linux

gcc

x86-64

I have several .c files and one .a object file. What command with gcc should I use to compile them to one exe file? If we use a makefile, how will it look like?

like image 968
pythonic Avatar asked May 04 '12 18:05

pythonic


People also ask

What is .a file in C compiler?

The . a file is a library, already compiled. You compile your . c file to a .o, then you use the linker to link your .o with the . a to produce an executable.

What is the command to compile C?

Run the gcc command to compile your C program. The syntax you'll use is gcc filename. c -o filename.exe . This compiles the program and makes it executable.


1 Answers

For simple cases you can probably do this:

gcc -o maybe.exe useful.a something.c

Makefiles for non-trivial projects usually first invoke gcc to compile each .c file to a .o object.

gcc -c something.c

Then they invoke the linker (these days often using gcc as a wrapper for it) with a list of .o and .a files to link into an output executable.

gcc -o maybe.exe useful.a something.o

Note also that for most installed libraries, it's typical not to explicitly specify the .a file but instead to say -lhandy which would be short for "try to find something called libhandy.a in the configured (or specified with -L) search directories"

like image 147
Chris Stratton Avatar answered Sep 28 '22 03:09

Chris Stratton