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?
3 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"

- 39,853
- 6
- 84
- 117
-
Thanks to anonymous for catching the error in the object file generation – Chris Stratton Sep 30 '14 at 18:25
*.a
is a static library and not dynamic (*.dll
in windows and *.so
in linux)
gcc -L<here comes the library path> -l<library name>
for example for the file you have libname.a in the current path you should use:
gcc *.c -L. -lname -o myprogram.o
from the man (put man gcc in the shell command prompt)
You can mix options and other arguments. For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.

- 11,087
- 1
- 32
- 38

- 39,472
- 36
- 165
- 245
-
Changed `gcc -L
-l – another.anon.coward May 04 '12 at 19:20` from `gcc -l -l `. Hope that is what you meant -
-
1@this In gcc-speak, `-lfoo` means `libfoo.a` or `libfoo.so`. As for why gcc does it, I have no idea. – Alex Shroyer Apr 06 '21 at 18:52
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.

- 11,800
- 1
- 26
- 34
-
11Doesn't answer the question about *how* to do it. Doesn't say "how it will look like". – mmtauqir Oct 24 '14 at 03:45