[Makefile] How to set the code for different directories (. c), generate to the specified directory (./debug/.o))

Source: Internet
Author: User

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;

  1. OUTPUT =./debug
  2. SOURCES = $ (wildcard./*.C) $ (wildcard.. /SRC2/*.C)
  3. DIR_OBJS = $ (patsubst%.c,%.o,$ (SOURCES))
  4. Objs_name = $ (Notdir $ (patsubst%.c,%.o,$ (SOURCES)))
  5. OBJS = $ (Addprefix $ (OUTPUT)/,$ (Notdir $ (patsubst%.c,%.o,$ (SOURCES)))
  6. Ldflags + =
  7. #vpath Mode 2 Required
  8. Vpath%.c $ (addsuffix:, $ (dir $ (patsubst%.c,%.o,$ (SOURCES)))))
  9. All: $ (TARGET)
  10. $ (TARGET): $ (OUTPUT) $ (OBJS)
  11. $ (CC) $ (OBJS) $ (ldflags)-O $ (TARGET)
  12. Method 1:
  13. $ (OBJS): $ (SOURCES)
  14. $ (CC) $ (CFLAGS)-o [email protected]-C $ (filter%$ (*f). c,$ (SOURCES))
  15. #该方式, 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;
  16. # Way 2
  17. $ (OBJS): $ (objs_name)
  18. CP $ (*f). O $ (OUTPUT)/
  19. # #若此时目标 (objs_name) with a path, the. C Search cannot use the specified search path
  20. $ (Objs_name):%.o:%.c
  21. $ (CC) $ (CFLAGS)-O $ (OUTPUT)/[email protected]-C $<
  22. #该方式, 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.
  23. # Way 3
  24. $ (OBJS): $ (DIR_OBJS)
  25. #
  26. $ (DIR_OBJS):%.o:%.c
  27. $ (CC) $ (CFLAGS)-o [email protected]-C $<
  28. MV [email protected]./debug/
  29. #该方式, 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))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.