11

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?

pythonic
  • 20,589
  • 43
  • 136
  • 219

3 Answers3

22

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"

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
7

*.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.

another.anon.coward
  • 11,087
  • 1
  • 32
  • 38
0x90
  • 39,472
  • 36
  • 165
  • 245
3

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.

TJD
  • 11,800
  • 1
  • 26
  • 34