clinuxstatic-librariesstatic-linkinglibtool

How do I compile a static library


I am trying to compile a static library. I followed the steps which were given in an answer to this question but it is not working. Following is my makefile.

PROJECT = lq.a
OBJECTS = dlmalloc.o queue.o.o
CFLAGS  = -o -Wall -pedantic

all: $(PROJECT)

.c.o:
    gcc -c $(CFLAGS) $<

$(PROJECT): $(OBJECTS)
    libtool -o  $(PROJECT) -static $(OBJECTS)

And I get the following error.

libtool: unrecognized option `-o'

What is the correct way of writing this makefile?


Solution

  • You can use the program ar to create static libraries using the following syntax:

     ar rcs my_library.a file1.o file2.o
    

    So, in your case:

    $(PROJECT): $(OBJECTS)
         ar rcs $(PROJECT) $(OBJECTS)
    

    You can find an explanation of the options on the man page, but basically: