Part of the code and makefile is not the same directory, there is no good way to set the dependency, I found three methods, but the feeling is not perfect, the following I will list him and explain, I do not know whether there is a better way, makefile itself is not very familiar, please advise;
The requirements are as follows:
1) The directory structure is shown below
[SRC1] (directory):
src11.c
Src12.c
Src13.c ...
Makefile
[SRC2] (directory):
src21.c
Src22.c
...
2) write Makefie, do not want each file is written on the dependency, want to be able to let it automatically deduced, but will list. c files and paths, as shown in the Code section;
3) The generated. O is not in the same directory as makefile, such as./debug, if it is the same directory, or can be resolved, but feel the temporary file too much, not good;
- OUTPUT =./debug
- SOURCES = $ (wildcard./*.C) $ (wildcard.. /SRC2/*.C)
- DIR_OBJS = $ (patsubst%.c,%.o,$ (SOURCES))
- Objs_name = $ (Notdir $ (patsubst%.c,%.o,$ (SOURCES)))
- OBJS = $ (Addprefix $ (OUTPUT)/,$ (Notdir $ (patsubst%.c,%.o,$ (SOURCES)))
- Ldflags + =
- #vpath Mode 2 Required
- Vpath%.c $ (addsuffix:, $ (dir $ (patsubst%.c,%.o,$ (SOURCES)))))
- All: $ (TARGET)
- $ (TARGET): $ (OUTPUT) $ (OBJS)
- $ (CC) $ (OBJS) $ (ldflags)-O $ (TARGET)
- Method 1:
- $ (OBJS): $ (SOURCES)
- $ (CC) $ (CFLAGS)-o [email protected]-C $ (filter%$ (*f). c,$ (SOURCES))
- #该方式, there is a disadvantage, is dependent on each SRC, as long as a src change, will cause all. o All need to be re-dependent;
- # Way 2
- $ (OBJS): $ (objs_name)
- CP $ (*f). O $ (OUTPUT)/
- # #若此时目标 (objs_name) with a path, the. C Search cannot use the specified search path
- $ (Objs_name):%.o:%.c
- $ (CC) $ (CFLAGS)-O $ (OUTPUT)/[email protected]-C $<
- #该方式, if the. O path is generated, then. c cannot be searched by the path specified by Vpath. C, it will be directly to the specified. O Directory search, if in this way, then makefile is located in the. O Way too much.
- # Way 3
- $ (OBJS): $ (DIR_OBJS)
- #
- $ (DIR_OBJS):%.o:%.c
- $ (CC) $ (CFLAGS)-o [email protected]-C $<
- MV [email protected]./debug/
- #该方式, if the. O already exists (DIR_OBJS) under SRC, then it will not be recompiled, to assume the environment ...
[Makefile] How to set the code for different directories (. c), generate to the specified directory (./debug/.o))