makeref.txt 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # make reference
  2. Learn Makefiles - https://makefiletutorial.com/
  3. $ make
  4. makefile:2: *** missing separator. Stop.
  5. $ cat -etv makefile
  6. [it show line starting by ^I if <tab> is given to that line and it end the line by $]
  7. Note: makefile must be indented using <tabs> and not <spaces> or 'make' will fail
  8. # makefile syntax
  9. targets: prerequisites
  10. command
  11. * targets are file names, separated by spaces
  12. (typically, there is only one per rule)
  13. * prerequisites are also file names, separated by spaces
  14. (these files need to exist before the commands for the target are run)
  15. * commands are a series of steps typically used to make the targets
  16. (these need to start with a tab character, not spaces)
  17. # the essence of make
  18. file:
  19. echo "This line will print if the file does not exist."
  20. * we have one target called file
  21. * target file has no prerequisites
  22. * target file has one command
  23. Note: as long as the 'file' does not exist, the command will run
  24. (if 'file' does exist, no command will run)
  25. example:
  26. Let's create a more typical makefile - one that compiles a single C file. But
  27. before we do, make a file called program.c that has the following contents:
  28. int main() {
  29. return 0;
  30. }
  31. then create the makefile:
  32. program:
  33. cc program.c -o program
  34. This time, try simply running make. Since there's no target supplied as anargument
  35. to the make command, the first target is run. In this case, there's only one target
  36. (program). The first time you run this, program will be created. The second time, you
  37. will see make: 'program' is up to date. That's because the program file already exists.
  38. But there a problem: if we modify program.c and then run make, nothing gets recompiled.
  39. We solve this by adding a prerequisite:
  40. program: program.c
  41. cc program.c -o program
  42. When we run make again, the following set of steps happens:
  43. * the first target is selected, because the first target is the default target
  44. * this has a prerequisite of program.c
  45. * make decides if it should run the program target
  46. (it will only run if program doesn't exist, or program.c is newer than program)
  47. The last step is critical, and is the essence of make.
  48. make -d # trace debug output
  49. make --debug=b # basic debug
  50. make -p # print rules and variables