Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions 02_Multifile/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
OBJS = $(wildcard *.o)
TESTS = $(wildcard *.test)
GENERATES = prog prog-a prog-so liboutput.so liboutput_static.a $(OBJS) $(TESTS)

CFLAGS += -fPIC

EMPTY_TEST_CASE =
SINGLE_ARG_TEST_CASE = single_argument
TRIPLE_ARG_TEST_CASE = first_arg 2nd_arg 3

all: prog prog-a prog-so

prog: prog.o fun.o const.o

prog-a: prog.o liboutput_static.a
$(CC) -L. $< -loutput_static -o $@

liboutput_static.a: fun.o const.o
$(AR) -rcs $@ $^

prog-so: prog.o liboutput.so
$(CC) -L. $< -loutput -o $@

liboutput.so: fun.o const.o
$(CC) -shared $^ -o $@

fun.o: outlib.h

prog.o: outlib.h

# Tests are expected to fail since program names are different.
# To make them work, please remove EMPTY_TEST_CASE testing.
test: prog.test prog-a.test prog-so.test
cmp prog.test prog-a.test
cmp prog-a.test prog-so.test

%.test: % FORCE
./$< $(EMPTY_TEST_CASE) > $@ 2>&1
./$< $(SINGLE_ARG_TEST_CASE) >> $@ 2>&1
./$< $(TRIPLE_ARG_TEST_CASE) >> $@ 2>&1

prog-so.test: prog-so FORCE
LD_LIBRARY_PATH=$(shell pwd):$$LD_LIBRARY_PATH ./$< $(EMPTY_TEST_CASE) > $@ 2>&1
LD_LIBRARY_PATH=$(shell pwd):$$LD_LIBRARY_PATH ./$< $(SINGLE_ARG_TEST_CASE) >> $@ 2>&1
LD_LIBRARY_PATH=$(shell pwd):$$LD_LIBRARY_PATH ./$< $(TRIPLE_ARG_TEST_CASE) >> $@ 2>&1

FORCE:

clean:
rm -f $(GENERATES)
1 change: 1 addition & 0 deletions 02_Multifile/const.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int Count = 0;
15 changes: 15 additions & 0 deletions 02_Multifile/fun.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "outlib.h"
#include <stdio.h>

void output(char *str)
{
printf("%d: %s\012", Count++, str);
}

void usage(char *prog)
{
fprintf(stderr,
"%s v%s: Print all arguments\012\t"
"Usage: %s arg1 [arg2 […]]\012",
prog, VERSION, prog);
}
4 changes: 4 additions & 0 deletions 02_Multifile/outlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void output(char *);
void usage(char *);
extern int Count;
#define VERSION "1.0.0"
17 changes: 17 additions & 0 deletions 02_Multifile/prog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "outlib.h"
#include <stdio.h>

int main(int argc, char *argv[])
{
int i;
if ((Count = argc) > 1)
{
output("<INIT>");
for (i = 1; i < argc; i++)
output(argv[i]);
output("<DONE>");
}
else
usage(argv[0]);
return 0;
}