That said it is relatively standard to use a capitol M. Note that this file needs to be named Makefile, however the capitol M is optional.
HOW TO CREATE A MAKEFILE FOR C PROGRAM CODE
Lets make a basic Makefile and save it to our system in the same directory as our source code named Makefile. For instance here is a possible example Makefile for the hello wold example above.
HOW TO CREATE A MAKEFILE FOR C PROGRAM HOW TO
This is where a Makefile comes in, makefiles allow us to write out a fairly complex set of rules for how to compile a program and then simply compile it by typing make on the command line. For a basic program this works well but as programs become more complex our compilation command can also become more complex. Lets say our source code is in a file called source.c, now in order to run our program we need to compile it, typically on Linux (using gcc) we would need to type $> gcc source.c -o output where output is the name of the executable to be generated. VersionsĬonsider writing a "hello world!" program in c. In order to ensure a target is up to date, it must first ensure that each of that target's prerequisites are up to date, and so on recursively. Only after all makefiles have been completely parsed and the graph is complete will make choose one node (target) and attempt to bring it up to date. Second, a makefile is not a procedural language like a script: as make parses the makefile it constructs a directed graph internally where targets are the nodes of the graph and the prerequisite relationships are the edges. The fact that recipes are not parsed by make, but instead handled by a separate shell process, is central in understanding makefiles. The make program doesn't parse this script: it runs a shell and passes the script to the shell to be run. The recipe is written as a shell script (POSIX sh by default). In addition, each target can have a recipe associated with it which specifies which commands should be invoked to cause that target to be brought up-to-date. The bulk of the file is written in a language that make can understand: this provides variable assignment and expansion, some preprocessor capabilities (including other files, conditional parsing of sections of the file, etc.) as well as the definition of targets and their prerequisites. Makefiles are somewhat unique in a few ways which can be confusing initially.įirst, a makefile consists of two completely different programming languages in the same file. The only way that make determines "out of date-ness" is by comparing the modification time of target files and their prerequisites. The makefile describes the relationship between targets and prerequisites, and also specifies the commands needed to bring the target up-to-date when one or more of the prerequisites has changed. The make program is typically used to manage the creation of programs from their source files, but it can be more generally used to handle any process where files (or targets) need to be regenerated after other files (or prerequisites) have been modified. A makefile is a text file which controls the operation of the make program.