{"id":6045,"date":"2023-09-30T00:01:43","date_gmt":"2023-09-30T07:01:43","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6045"},"modified":"2023-09-23T11:24:08","modified_gmt":"2023-09-23T18:24:08","slug":"using-make-to-build-projects","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6045","title":{"rendered":"Using <em>make<\/em> to Build Projects"},"content":{"rendered":"<p>The <em>make<\/em> utility has been around since the early days of Unix. This tool is designed to create large projects by compiling and linking files based on dependencies. It takes care of a lot problems managing multi-module files to streamline the build process.<br \/>\n<!--more--><br \/>\nLike many of the early Unix tools, <em>make<\/em> is astonishingly cryptic. I&#8217;ve used it, but I don&#8217;t truly understand the details enough to consider myself even mildly proficient. Like many other coders, I typically copy and paste my makefiles.<\/p>\n<p>Before getting into the details, and if you aren&#8217;t turned off already, using <em>make<\/em> is a dream. Especially for large project with multiple source code files (modules), the <em>make<\/em> utility builds things for you, updates, compiles and links, all automatically.<\/p>\n<p>For example, I coded a game that had six or so source code files plus a custom header file. As I worked on the project, I would update various files to fix bugs or add features. Normally, to build such a file, I would use a command like:<\/p>\n<p><code>clang -Wall main.c initialize.c windows.c robots.c human.c highscore.c helpmenu.c -lncurses -o robots<\/code><\/p>\n<p>The command builds my <em>robots<\/em> program, compiling all the source code file and linking in the Ncurses library. But when I was developing the game, I never typed the above command. Instead, I just typed:<\/p>\n<p><code>make<\/code><\/p>\n<p>That&#8217;s it! The <em>make<\/em> utility looks for a text file in the current directory named <code>makefile<\/code>. This file contains the instructions for how to build the program, citing which files depend on other files, the commands required to compile and link, and other items necessary to maintain the entire package.<\/p>\n<p>In fact, it&#8217;s the <em>make<\/em> utility that builds programs you fetch and install from a Linux distro&#8217;s package manager. All that text that scrolls up the screen during installation is output from commands issued in a makefile. I hope you appreciate how vital this utility can be.<\/p>\n<p>Then there&#8217;s the issue of creating the <code>makefile<\/code>.<\/p>\n<p>The <em>make<\/em> code runs by detected dependencies. For example, my memory-file project contains these files:<\/p>\n<p><code>main.c<br \/>\nmemfile.c<br \/>\nmemfile.h<\/code><\/p>\n<p>To build the program, I use the <em>clang<\/em> command shown earlier. If I modify <code>memfile.c<\/code>, it must be re-compiled into an object file, <code>memfile.o<\/code>, then re-linked with <code>main.o<\/code> to build the program <code>memfile<\/code>.<\/p>\n<p>Likewise, if I modify <code>memfile.h<\/code>, the files that depend on this header file must be re-compiled and linked.<\/p>\n<p>A sample <code>makefile<\/code> that coordinates these activities is shown in Figure 1.<\/p>\n<div id=\"attachment_6058\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-112135.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6058\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-112135-300x102.png\" alt=\"screenshot makefile text\" width=\"300\" height=\"102\" class=\"size-medium wp-image-6058\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-112135-300x102.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-112135-500x171.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-112135.png 662w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-6058\" class=\"wp-caption-text\">Figure 1. A sample <code>makefile<\/code> that builds my memory-file program. (Click to embiggen.)<\/p><\/div>\n<p>From Figure 1, the label <code>memfile:<\/code> lists dependencies. The <code>memfile<\/code> program file requires that object files <code>main.o<\/code> and <code>memfile.o<\/code> be up-to-date. If not, the command on the following line is executed; the files are linked and the <code>memfile<\/code> program is built.<\/p>\n<p>For the <code>main.o<\/code> file, files <code>main.c<\/code> and <code>memfile.h<\/code> must be up-to-date. if not, the command on the following line is executed, which creates the <code>main.o<\/code> object file. Ditto for the <code>memfile.o<\/code> file, which builds itself in the same manner.<\/p>\n<p>The result of typing <strong>make<\/strong> on the command prompt is that all the necessary files are built and linked to create the program file. If you modify the <code>memfile.h<\/code> header file and run <em>make<\/em>, the program is rebuilt.<\/p>\n<p>The <em>make<\/em> utility has many more options and tricks you can use. I have makefiles I&#8217;ve used that I&#8217;ve copied from other sources and I have no clue how they work. The commands are delightfully cryptic. Still, <em>make<\/em> is a useful utility.<\/p>\n<p>In an IDE, of course, the process of building a large file with multiple source code modules works differently. The steps required depend on the IDE, but things work the same: The program is built based on dependencies. I may explore this concept further in a future Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>make<\/em> utility can be cryptic and frustrating, which makes it a perfect companion for C programming. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6045\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6045","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6045"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6045\/revisions"}],"predecessor-version":[{"id":6059,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6045\/revisions\/6059"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}