How to merge two makefiles?

I'm not so skilled with makefile and I'm getting some configuration problem in marging two different makefiles.
I've two directories with different projects, made up by several .c/.cpp files and one makefile for each project.
I would like to drop file1.c of project A to project B, obviously I can't simply drop the file but I've to change correctly make file of project B.
MAKEFILE project A:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
DESTDIR ?=
PREFIX ?= /usr/local

MAKEFLAGS := -k

CFLAGS := -O2 -Wall -Wno-parentheses

CPPFLAGS += \
	-Iinclude \
	-DAF_CAN=PF_CAN \
	-DPF_CAN=29 \
	-DSO_RXQ_OVFL=40 \
	-DSCM_TIMESTAMPING_OPT_STATS=54 \
	-D_FILE_OFFSET_BITS=64 \
	-D_GNU_SOURCE

PROGRAMS_CANGW := \
	cangw

PROGRAMS_ISOTP := \
	isotpdump \
	isotpperf \
	isotprecv \
	isotpsend \
	isotpserver \
	isotpsniffer \
	isotptun

PROGRAMS_J1939 := \
	j1939acd \
	j1939cat \
	j1939spy \
	j1939sr \
	testj1939

PROGRAMS_SLCAN := \
	slcan_attach \
	slcand

PROGRAMS := \
	$(PROGRAMS_CANGW) \
	$(PROGRAMS_ISOTP) \
	$(PROGRAMS_J1939) \
	$(PROGRAMS_SLCAN) \
	asc2log \
	bcmserver \
	can-calc-bit-timing \
	canbusload \
	candump \
	canfdtest \
	cangen \
	cansequence \
	canlogserver \
	canplayer \
	cansend \
	cansniffer \
	log2asc \
	log2long \
	mcp251xfd-dump \
	slcanpty

all: $(PROGRAMS)

clean:
	rm -f $(PROGRAMS) *.o

install:
	mkdir -p $(DESTDIR)$(PREFIX)/bin
	cp -f $(PROGRAMS) $(DESTDIR)$(PREFIX)/bin

distclean:
	rm -f $(PROGRAMS) $(LIBRARIES) *.o *~

asc2log.o:	lib.h
canbusload.o:	lib.h
candump.o:	lib.h
cangen.o:	lib.h
canlogserver.o:	lib.h
canplayer.o:	lib.h
cansend.o:	lib.h
cansequence.o:	lib.h
log2asc.o:	lib.h
log2long.o:	lib.h
j1939acd.o:	libj1939.h
j1939cat.o:	libj1939.h
j1939spy.o:	libj1939.h
j1939sr.o:	libj1939.h
testj1939.o:	libj1939.h
canframelen.o:  canframelen.h

asc2log:	asc2log.o	lib.o
candump:	candump.o	lib.o
cangen:		cangen.o	lib.o
canlogserver:	canlogserver.o	lib.o
canplayer:	canplayer.o	lib.o
cansend:	cansend.o	lib.o
cansequence:	cansequence.o	lib.o
log2asc:	log2asc.o	lib.o
log2long:	log2long.o	lib.o
j1939acd:	j1939acd.o	libj1939.o
j1939cat:	j1939cat.o	libj1939.o
j1939spy:	j1939spy.o	libj1939.o
j1939sr:	j1939sr.o	libj1939.o
testj1939:	testj1939.o	libj1939.o
canbusload:	canbusload.o	canframelen.o

mcp251xfd-dump:	mcp251xfd/mcp251xfd-dev-coredump.o mcp251xfd/mcp251xfd-dump.o mcp251xfd/mcp251xfd-main.o mcp251xfd/mcp251xfd-regmap.o
	$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@


MAKEFILE project B:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
PROG = validNet
COMPILER = g++
STD = c++17
FILES.CPP = GUI.cpp Pipe.cpp
CFLAGS = `pkg-config --cflags gtk+-3.0`
LIBS =  `pkg-config --libs gtk+-3.0`
WARNING_FLAGS = -w

${PROG}: ${PROG}.cpp
	
	time ${COMPILER} -std=${STD} ${WARNING_FLAGS} ${CFLAGS} ${PROG}.cpp ${FILES.CPP} -o ${PROG} ${LIBS}

clean: 
	rm ${PROG}


in particular I just need to use cangen.c in project B for an university project.
Can I simply drop cangen.o in project B directory and linking it as an object file? Is it bad practise?
Is there some simple way to marge two makefile? It's look simple but I haven't find a correct solution, I've tried with this makefile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
PROG = validNet
COMPILER = g++ -Wall -Wno-parentheses
STD = c++17
FILES.CPP = GUI.cpp Pipe.cpp cangen.c lib.c
CPPFLAGS += \
	-Iinclude \
	-DAF_CAN=PF_CAN \
	-DPF_CAN=29 \
	-DSO_RXQ_OVFL=40 \
	-DSCM_TIMESTAMPING_OPT_STATS=54 \
	-D_FILE_OFFSET_BITS=64 \
	-D_GNU_SOURCE

CFLAGS = `pkg-config --cflags gtk+-3.0`
LIBS =  `pkg-config --libs gtk+-3.0`
WARNING_FLAGS = -w

${PROG}: ${PROG}.cpp
	
	time ${COMPILER} -std=${STD} {$CPPFLAGS} ${WARNING_FLAGS} ${CFLAGS} ${PROG}.cpp ${FILES.CPP} -o ${PROG} ${LIBS}

clean: 
	rm ${PROG}
Last edited on
The complication is that you have different compiler flags for the two projects. However, as it's just one file/program that has special rules, we can just add the special stuff for that one item.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
DESTDIR ?=
PREFIX ?= /usr/local

MAKEFLAGS := -k

CFLAGS := -O2 -Wall -Wno-parentheses

CPPFLAGS += \
	-Iinclude \
	-DAF_CAN=PF_CAN \
	-DPF_CAN=29 \
	-DSO_RXQ_OVFL=40 \
	-DSCM_TIMESTAMPING_OPT_STATS=54 \
	-D_FILE_OFFSET_BITS=64 \
	-D_GNU_SOURCE

PROGRAMS_VALIDNET = validNet
CFLAGS_VALIDNET   = `pkg-config --cflags gtk+-3.0`
LIBS_VALIDNET     = `pkg-config --libs gtk+-3.0`

PROGRAMS_CANGW := \
	cangw

PROGRAMS_ISOTP := \
	isotpdump \
	isotpperf \
	isotprecv \
	isotpsend \
	isotpserver \
	isotpsniffer \
	isotptun

PROGRAMS_J1939 := \
	j1939acd \
	j1939cat \
	j1939spy \
	j1939sr \
	testj1939

PROGRAMS_SLCAN := \
	slcan_attach \
	slcand

PROGRAMS := \
	$(PROGRAMS_VALIDNET) \
	$(PROGRAMS_CANGW) \
	$(PROGRAMS_ISOTP) \
	$(PROGRAMS_J1939) \
	$(PROGRAMS_SLCAN) \
	asc2log \
	bcmserver \
	can-calc-bit-timing \
	canbusload \
	candump \
	canfdtest \
	cangen \
	cansequence \
	canlogserver \
	canplayer \
	cansend \
	cansniffer \
	log2asc \
	log2long \
	mcp251xfd-dump \
	slcanpty

all: $(PROGRAMS)

clean:
	rm -f $(PROGRAMS) *.o

install:
	mkdir -p $(DESTDIR)$(PREFIX)/bin
	cp -f $(PROGRAMS) $(DESTDIR)$(PREFIX)/bin

distclean:
	rm -f $(PROGRAMS) $(LIBRARIES) *.o *~

validNet: GUI.o Pipe.o
	$(LINK.cpp) $^ -o $@ $(LIBS_VALIDNET)

GUI.o: GUI.cpp
	g++ -std=c++17 -w $(CFLAGS_VALIDNET) $^ -o $@

Pipe.o: Pipe.cpp
	g++ -std=c++17 -w $(CFLAGS_VALIDNET) $^ -o $@

asc2log.o:	lib.h
canbusload.o:	lib.h
candump.o:	lib.h
cangen.o:	lib.h
canlogserver.o:	lib.h
canplayer.o:	lib.h
cansend.o:	lib.h
cansequence.o:	lib.h
log2asc.o:	lib.h
log2long.o:	lib.h
j1939acd.o:	libj1939.h
j1939cat.o:	libj1939.h
j1939spy.o:	libj1939.h
j1939sr.o:	libj1939.h
testj1939.o:	libj1939.h
canframelen.o:  canframelen.h

asc2log:	asc2log.o	lib.o
candump:	candump.o	lib.o
cangen:		cangen.o	lib.o
canlogserver:	canlogserver.o	lib.o
canplayer:	canplayer.o	lib.o
cansend:	cansend.o	lib.o
cansequence:	cansequence.o	lib.o
log2asc:	log2asc.o	lib.o
log2long:	log2long.o	lib.o
j1939acd:	j1939acd.o	libj1939.o
j1939cat:	j1939cat.o	libj1939.o
j1939spy:	j1939spy.o	libj1939.o
j1939sr:	j1939sr.o	libj1939.o
testj1939:	testj1939.o	libj1939.o
canbusload:	canbusload.o	canframelen.o

mcp251xfd-dump:	mcp251xfd/mcp251xfd-dev-coredump.o mcp251xfd/mcp251xfd-dump.o mcp251xfd/mcp251xfd-main.o mcp251xfd/mcp251xfd-regmap.o
	$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
Last edited on
Topic archived. No new replies allowed.