Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make static library in makefile

I have the following makefile so far right now...

# Beginning of Makefile

OBJS = obj/shutil.o obj/parser.o obj/sshell.o
HEADER_FILES = include/shell.h include/parser.h
STATLIB = lib/libparser.a lib/libshell.a
EXECUTABLE = sshell
CFLAGS = -Wall
CC = gcc
# End of configuration options

#What needs to be built to make all files and dependencies
all: $(EXECUTABLE) $(STATLIB)

#Create the main executable
$(EXECUTABLE): $(OBJS)
        $(CC) -o $(EXECUTABLE) $(OBJS)

$(STATLIB): $(
#Recursively build object files
obj/%.o: src/%.c
        $(CC) $(CFLAGS) -I./include  -c -o $@ $<


#Define dependencies for objects based on header files
#We are overly conservative here, parser.o should depend on parser.h only
$(OBJS) : $(HEADER_FILES)

clean:
        -rm -f $(EXECUTABLE) obj/*.o
        -rm -f lib/*.a

run: $(EXECUTABLE)
        ./$(EXECUTABLE)

tarball:
        -rm -f $(EXECUTABLE) *.o
        (cd .. ; tar czf Your_Name_a1.tar.z shell )

# End of Makefile

I am trying to generate static libraries libparser.a and libshell.a

I have no idea how to create these static libraries...

like image 336
Anthony Ku Avatar asked Mar 04 '13 05:03

Anthony Ku


1 Answers

You create static libraries with the ar command:

lib/libparser.a: $(OBJECT_FILES_FOR_LIBPARSER)
        ar rcs $@ $^

lib/libshell.a: $(OBJECT_FILES_FOR_LIBSHELL)
        ar rcs $@ $^

If your ar command doesn't understand the s option, you'll have to run ranlib on the .a file produced by ar as well. In that case, replace ar rcs $@ $^ with ar rc $@ $^ && ranlib $@.

like image 119
Idelic Avatar answered Oct 08 '22 10:10

Idelic