I am trying to create a shared library called -lrfc7539
with the structure below:
rfc7539.o: rfc7539.c chacha20poly1305.o
$(CC) $(CFLAGS) -c -o $@ $<
chacha20poly1305.o: chacha20poly1305.c chacha20.o poly1305.o
$(CC) $(CFLAGS) -c -o $@ $<
chacha20.o: chacha_merged.c
$(CC) -fPIC $(CFLAGS) -c -o $@ $<
poly1305.o: poly1305-donna.c
$(CC) -fPIC $(CFLAGS) -DPOLY1305_16BIT -c -o $@ $<
rfc7539_test: rfc7539.o chacha20poly1305.o poly1305.o chacha20.o
.PHONY: clean
clean:
@rm -f *.o
@rm -f rfc7539_test
I then do this command gcc -shared -o lrfc7539.so *.o
to create .so
file Is there a better practice for my makefile to be able to do this automatically ?
You need to create a target in your makefile that runs the gcc command you gave. If this library is the main output, make an all
target pointing to in.
all: lrfc7539.so
lrfc7539.so: rfc7539.o chacha20poly1305.o chacha20.o poly1305.o
gcc -shared -o $@ *.o
You can just make a rule with that target:
librfc7539.so: rfc7539.o chacha20poly1305.o poly1305.o chacha20.o
$(LINK.c) $(OUTPUT_OPTION) -shared $^ $(LOADLIBES) $(LDLIBS)
I copied the command from make --print-data-base
(you could update your other commands likewise). You may need to add -fPIC
to LDFLAGS
, too.
I called your library librfc7539.so
so that you can link to it using -lrfc7539
- I think that's what you're wanting.
I believe it's best practice to explicitly specify the object files you intend to link, but some like to assume that every source file must be compiled and linked:
sources := $(wildcard *.c)
librfc7539.so: $(sources:.c=.o)
This wouldn't work for you, though, unless you renamed the source files that are compiled to differently-named object files.
I note that it's strange that your object files depend on other object files. That shouldn't be the case, although they may need dependencies on some header files.
Here's a complete Makefile (assuming GNU Make):
CFLAGS += -Wall -Wextra
CFLAGS += -fPIC
LDFLAGS += -fPIC
%.so: LDFLAGS += -shared
all: rfc7539_test librfc7539.so
librfc7539.so: rfc7539.o chacha20poly1305.o poly1305.o chacha20.o
$(LINK.c) $(OUTPUT_OPTION) $^ $(LOADLIBES) $(LDLIBS)
rfc7539_test: rfc7539.o chacha20poly1305.o poly1305.o chacha20.o
$(LINK.c) $(OUTPUT_OPTION) $^ $(LOADLIBES) $(LDLIBS)
# Default %.o:%.c rule works, except for these files with misnamed sources:
chacha20.o: chacha_merged.c
$(COMPILE.c) $(OUTPUT_OPTION) $<
poly1305.o: poly1305-donna.c
$(COMPILE.c) $(OUTPUT_OPTION) $<
# Specific flags for this source file
poly1305.o: CFLAGS += -DPOLY1305_16BIT
.PHONY: clean
clean:
@$(RM) *.o
@$(RM) rfc7539_test
.DELETE_ON_ERROR:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.