<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 김세현</title>
    <description>The latest articles on DEV Community by 김세현 (@sehyeonkim).</description>
    <link>https://dev.to/sehyeonkim</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2683830%2F0f92dc94-cd95-419a-8a46-36e8bda0cbbb.jpg</url>
      <title>DEV Community: 김세현</title>
      <link>https://dev.to/sehyeonkim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sehyeonkim"/>
    <language>en</language>
    <item>
      <title>Makefile</title>
      <dc:creator>김세현</dc:creator>
      <pubDate>Fri, 10 Jan 2025 07:19:44 +0000</pubDate>
      <link>https://dev.to/sehyeonkim/makefile-oob</link>
      <guid>https://dev.to/sehyeonkim/makefile-oob</guid>
      <description>&lt;h2&gt;
  
  
  What is a Makefile?
&lt;/h2&gt;

&lt;p&gt;A Makefile is a special file, traditionally named Makefile or makefile, that contains a set of rules to automate the building, testing, and deployment of projects. It is used with the make utility, a build automation tool that interprets these rules to execute tasks efficiently.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Use a Makefile?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Automation&lt;/strong&gt;: Simplify repetitive tasks like compiling code or running tests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency Management&lt;/strong&gt;: Ensure that only the necessary parts of your project are rebuilt, saving time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Portability&lt;/strong&gt;: Share build instructions across team members to maintain consistency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Customizable&lt;/strong&gt;: Define custom commands for deployment, cleaning, or other project-specific tasks.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Anatomy of a Makefile
&lt;/h2&gt;

&lt;p&gt;A Makefile consists of targets, dependencies, and recipes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Target&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The target is the name of the task to be executed. It can represent a file to be created or a label for a custom task.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dependencies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dependencies are files or targets that must exist or be up-to-date before the target is executed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Recipe&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The recipe is the command or set of commands to execute. These commands must be indented with a tab (not spaces).&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Basic Syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;target: dependencies
&amp;lt;TAB&amp;gt; command
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Directory Structure&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/
|-- main.c
|-- utils.c
|-- utils.h
|-- Makefile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Makefile&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Variables
CC = gcc
CFLAGS = -Wall -g

# Targets
all: program

program: main.o utils.o
    $(CC) $(CFLAGS) -o program main.o utils.o

main.o: main.c utils.h
    $(CC) $(CFLAGS) -c main.c

utils.o: utils.c utils.h
    $(CC) $(CFLAGS) -c utils.c

clean:
    rm -f *.o program
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Variables&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;CC&lt;/code&gt;: Compiler to use (e.g., &lt;code&gt;gcc&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CFLAGS&lt;/code&gt;: Compiler flags like warnings (&lt;code&gt;-Wall&lt;/code&gt;) and debugging (&lt;code&gt;-g&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Targets&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;all&lt;/code&gt;: Default target. It depends on &lt;code&gt;program&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;program&lt;/code&gt;: Builds the final executable using main.o and &lt;code&gt;utils.o&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main.o&lt;/code&gt; and &lt;code&gt;utils.o&lt;/code&gt;: Object files compiled from their respective .c files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;clean&lt;/code&gt;: A custom target to delete generated files.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Dependencies&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;main.o&lt;/code&gt; depends on &lt;code&gt;main.c&lt;/code&gt; and &lt;code&gt;utils.h&lt;/code&gt;, so changes in these files will trigger a recompilation.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Commands&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Commands like &lt;code&gt;gcc&lt;/code&gt; and &lt;code&gt;rm&lt;/code&gt; are executed in the shell.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Features of Makefile
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Implicit Rules&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Make can infer rules, so you don’t always need to specify them explicitly. For example, if you omit the main.o rule, make can deduce how to compile main.c into main.o.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phony Targets&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use .PHONY to define targets that don’t create files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.PHONY: clean
clean:
    rm -f *.o program
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Wildcards&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Automate file selection with wildcards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Conditional Statements&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Add conditional logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ifeq ($(DEBUG), 1)
CFLAGS += -DDEBUG
endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>cli</category>
      <category>utility</category>
    </item>
  </channel>
</rss>
