{"id":1635,"date":"2024-02-24T00:00:00","date_gmt":"2024-02-24T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1635"},"modified":"2024-02-02T14:33:27","modified_gmt":"2024-02-02T19:33:27","slug":"linux-make-cheat-sheet","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/linux-make-cheat-sheet\/","title":{"rendered":"Linux &#8220;make&#8221; Cheat Sheet"},"content":{"rendered":"\n<p>Make is a powerful build automation tool that simplifies the process of compiling and building projects. This cheat sheet provides essential commands and directives to efficiently use Make for managing software builds.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Introduction:<\/strong><\/h4>\n\n\n\n<p>Make uses a file called <code>Makefile<\/code> to define rules and dependencies for building software. It automates the build process by executing predefined commands based on the dependencies and their modification timestamps.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Basic Commands:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>make<\/code><\/td><td>Build the default target in the Makefile.<\/td><\/tr><tr><td><code>make [target]<\/code><\/td><td>Build a specific target in the Makefile.<\/td><\/tr><tr><td><code>make -f [makefile]<\/code><\/td><td>Use a specified Makefile.<\/td><\/tr><tr><td><code>make -n<\/code> or <code>make --dry-run<\/code><\/td><td>Display the commands without executing them.<\/td><\/tr><tr><td><code>make -B<\/code> or <code>make --always-make<\/code><\/td><td>Force a rebuild of all targets.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Variables and Assignments:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>CC = gcc<\/code><\/td><td>Assign the compiler (e.g., gcc) to a variable.<\/td><\/tr><tr><td><code>CFLAGS = -Wall -O2<\/code><\/td><td>Set compilation flags for the C compiler.<\/td><\/tr><tr><td><code>LDFLAGS = -lm<\/code><\/td><td>Set linker flags for the linker.<\/td><\/tr><tr><td><code>SOURCES = file1.c file2.c<\/code><\/td><td>Define a list of source files.<\/td><\/tr><tr><td><code>OBJECTS = $(SOURCES:.c=.o)<\/code><\/td><td>Generate object file names from source files.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Targets and Dependencies:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>all: [dependencies]<\/code><\/td><td>Define the default target and its dependencies.<\/td><\/tr><tr><td><code>target: [dependencies]<\/code><\/td><td>Define a custom target and its dependencies.<\/td><\/tr><tr><td><code>$(target): [dependencies]<\/code><\/td><td>Define a variable-based target and dependencies.<\/td><\/tr><tr><td><code>.PHONY: [target]<\/code><\/td><td>Declare a target as phony (non-file target).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Pattern Rules and Wildcards:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>%.o: %.c<\/code><\/td><td>Define a pattern rule for compiling source files.<\/td><\/tr><tr><td><code>$(wildcard pattern)<\/code><\/td><td>Expand to a list of files matching a pattern.<\/td><\/tr><tr><td><code>$(patsubst pattern,replacement,text)<\/code><\/td><td>Replace occurrences of a pattern in a text.<\/td><\/tr><tr><td><code>$(foreach var,list,text)<\/code><\/td><td>Iterate over elements in a list.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Conditionals and Control Flow:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>ifeq ($(variable), value)<\/code><\/td><td>Conditionally execute commands if equal.<\/td><\/tr><tr><td><code>ifdef [variable]<\/code><\/td><td>Conditionally execute commands if defined.<\/td><\/tr><tr><td><code>ifndef [variable]<\/code><\/td><td>Conditionally execute commands if not defined.<\/td><\/tr><tr><td><code>else<\/code><\/td><td>Execute commands if the previous condition is false.<\/td><\/tr><tr><td><code>endif<\/code><\/td><td>End a conditional block.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Miscellaneous:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>$(shell command)<\/code><\/td><td>Execute a shell command and use the result.<\/td><\/tr><tr><td><code>$(error message)<\/code><\/td><td>Display an error message and stop execution.<\/td><\/tr><tr><td><code>$(warning message)<\/code><\/td><td>Display a warning message during execution.<\/td><\/tr><tr><td><code>.SUFFIXES: .c .o<\/code><\/td><td>Define the default suffixes for implicit rules.<\/td><\/tr><tr><td><code>.DEFAULT_GOAL := [target]<\/code><\/td><td>Set the default goal if none is specified.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Make is a versatile tool for automating software builds and managing project dependencies. This cheat sheet provides a quick reference for common Make commands and syntax. Whether you are a beginner or an experienced developer, use these commands to streamline your build process and enhance your understanding of Makefile structures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Make is a powerful build automation tool that simplifies the process of compiling and building projects. This cheat sheet provides essential commands and directives to efficiently use Make for managing software builds. Introduction: Make uses a file called Makefile to define rules and dependencies for building software. It automates the build process by executing predefined [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,265],"tags":[35],"class_list":["post-1635","post","type-post","status-publish","format-standard","hentry","category-linux","category-reference","tag-linux"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1635"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1635\/revisions"}],"predecessor-version":[{"id":1636,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1635\/revisions\/1636"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}