From 9a7ca2ce530c59abcdccb8a730c53c6098b3da3a Mon Sep 17 00:00:00 2001 From: John Ogness Date: Mon, 28 Jan 2019 19:40:24 +0106 Subject: linking/hello: fixup Makefiles It is good to have Makefile and autotool versions for compare. But the Makefiles should not be intentially horrible. Clean them up to the level that we teach in the training. Signed-off-by: John Ogness --- schulung_tools/linking/hello/Makefile | 43 ++++++++++++++----------------- schulung_tools/linking/hello/Makefile-lib | 37 ++++++++++++++++++++++++++ schulung_tools/linking/hello/func.h | 7 +++++ schulung_tools/linking/hello/hello.c | 4 +-- 4 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 schulung_tools/linking/hello/Makefile-lib create mode 100644 schulung_tools/linking/hello/func.h diff --git a/schulung_tools/linking/hello/Makefile b/schulung_tools/linking/hello/Makefile index a5b4c26..c383eae 100644 --- a/schulung_tools/linking/hello/Makefile +++ b/schulung_tools/linking/hello/Makefile @@ -1,33 +1,30 @@ -hello: hello.c libfunc.so - $(CROSS_COMPILE)gcc -g -ohello hello.c -L. -lfunc +CC = $(CROSS_COMPILE)gcc +LD = $(CROSS_COMPILE)gcc -hello2: hello.c libfunc.a - rm -f libfunc.so - $(CROSS_COMPILE)gcc -g -ohello2 hello.c -L. -lfunc +OBJS = hello.o -libfunc.so: libfunc.so.0.0.1 - ln -s libfunc.so.0.0.1 libfunc.so +CFLAGS = -g -Wall -Werror -I. +LDFLAGS = -L. -lfunc -libfunc.so.0.0.1: func1.o func2.o - $(CROSS_COMPILE)gcc -shared -olibfunc.so.0.0.1 -Wl,-soname,libfunc.so.0 func1.o func2.o +hello: $(OBJS) libfunc.so + $(CC) -ohello $(OBJS) $(LDFLAGS) -libfunc.a: func1.o func2.o - $(CROSS_COMPILE)ar cr libfunc.a func1.o func2.o - $(CROSS_COMPILE)ranlib libfunc.a +hello-static: $(OBJS) libfunc.a + rm -f libfunc.so + $(CC) -o$@ $(OBJS) $(LDFLAGS) -func1.o: func1.c - $(CROSS_COMPILE)gcc -fPIC -g -c func1.c +%.c: %.o + $(CC) -c -o$@ $(CFLAGS) $< -func2.o: func2.c - $(CROSS_COMPILE)gcc -fPIC -g -c func2.c +libfunc.%: + $(MAKE) -f Makefile-lib $@ -install_lib: libfunc.so.0.0.1 - echo "/opt/acme/lib" > /etc/ld.so.conf.d/acme.conf - mkdir -p /opt/acme/lib - cp libfunc.so.0.0.1 /opt/acme/lib - ldconfig +install: hello + $(MAKE) -f Makefile-lib install + cp $< /usr/bin/ clean: - rm -f hello hello2 *.o lib* + rm -f hello hello-static $(OBJS) + $(MAKE) -f Makefile-lib $@ -.PHONY: clean +.PHONY: install clean diff --git a/schulung_tools/linking/hello/Makefile-lib b/schulung_tools/linking/hello/Makefile-lib new file mode 100644 index 0000000..f3ac0de --- /dev/null +++ b/schulung_tools/linking/hello/Makefile-lib @@ -0,0 +1,37 @@ +CC = $(CROSS_COMPILE)gcc +LD = $(CROSS_COMPILE)gcc +AR = $(CROSS_COMPILE)ar +RANLIB = $(CROSS_COMPILE)ranlib + +REALNAME = libfunc.so.0.0.1 +SONAME = libfunc.so.0 +LINKERNAME = libfunc.so +STATICNAME = libfunc.a +OBJS = func1.o func2.o + +CFLAGS = -fPIC -g -Wall -Werror +LDFLAGS = -shared -Wl,-soname,$(SONAME) + +$(LINKERNAME): $(REALNAME) + ln -sf $< $@ + +$(REALNAME): $(OBJS) + $(CC) -o$@ $(LDFLAGS) $(OBJS) + +$(STATICNAME): $(OBJS) + $(AR) cr $@ $(OBJS) + $(RANLIB) $@ + +%.o: %.c + $(CC) -c -o$@ $(CFLAGS) $< + +install: $(REALNAME) + echo "/opt/acme/lib" > /etc/ld.so.conf.d/acme.conf + mkdir -p /opt/acme/lib + cp $< /opt/acme/lib/ + ldconfig + +clean: + rm -f $(OBJS) $(REALNAME) $(SONAME) $(LINKERNAME) $(STATICNAME) + +.PHONY: install clean diff --git a/schulung_tools/linking/hello/func.h b/schulung_tools/linking/hello/func.h new file mode 100644 index 0000000..cefd769 --- /dev/null +++ b/schulung_tools/linking/hello/func.h @@ -0,0 +1,7 @@ +#ifndef FUNC_H +#define FUNC_H + +extern void func1(void); +extern void func2(void); + +#endif /* FUNC_H */ diff --git a/schulung_tools/linking/hello/hello.c b/schulung_tools/linking/hello/hello.c index 82b8d53..9b8d43a 100644 --- a/schulung_tools/linking/hello/hello.c +++ b/schulung_tools/linking/hello/hello.c @@ -1,7 +1,5 @@ #include - -void func1(void); -void func2(void); +#include int main(void) { -- cgit v1.2.3