{"id":398,"date":"2019-04-30T03:36:34","date_gmt":"2019-04-30T03:36:34","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=398"},"modified":"2019-07-21T13:47:54","modified_gmt":"2019-07-21T13:47:54","slug":"build-and-run-your-first-application-with-make","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/mycpptutorial\/environment-setup\/build-and-run-your-first-application-with-make\/","title":{"rendered":"Build and run your first application with make"},"content":{"rendered":"\n<p>In this example, we have the same code that we have in <a href=\"http:\/\/kabiliravi.com\/index.php\/software\/programming\/mycpptutorial\/environment-setup\/build-and-run-your-first-application-with-gcc\/\">Build and run your first application with g++<\/a> but here we use <strong>make<\/strong> and <strong>Makefile<\/strong> to compile and link the source code.<\/p>\n\n\n\n<p>You can find the source code of this sample <a href=\"https:\/\/github.com\/mycpptutorial\/helloworld-make.git\">here in Github<\/a>.<\/p>\n\n\n\n<p>The same as previous example, the application contains three files: <strong>hello.h<\/strong>, <strong>hello.cpp<\/strong> and <strong>main.cpp<\/strong>.<\/p>\n\n\n\n<p>In order to use <strong>make<\/strong> for compilation, the first step is create a file in the root of your project called <strong>Makefile<\/strong><\/p>\n\n\n\n<p>In short, a Makefile has a general syntax called Rule Syntax:<\/p>\n\n\n\n<pre>targets : prerequisites\n        recipe\n...\n<\/pre>\n\n\n\n<p><strong>target(s)<\/strong> is\/are the generated or resulted files that the <strong>recipe<\/strong> section generates and the <strong>recipe<\/strong> can have <strong>prerequisites<\/strong> <\/p>\n\n\n\n<p>Here is a simple example is you want to compile and link <strong>hello.h<\/strong>, <strong>hello.cpp<\/strong> and <strong>main.cpp<\/strong><\/p>\n\n\n\n<b>Makefile<\/b>\n<pre>\nCC=g++\nDEPS = hello.h\nOBJ = hello.o main.o\n\n%.o: %.cpp $(DEPS)\n\t$(CC) -c -o $@ $<\n\ngreet: $(OBJ)\n\t$(CC) -o $@ $^\n<\/pre>\n\n\n\n<p>Save above content is a file called <strong>Makefile<\/strong> in the root folder of your project. I go to the Makefile line by line. <\/p>\n\n\n\n<p>The first line is <strong>CC=g++<\/strong>. I defined <strong>CC<\/strong> variable to indicate the compiler which <strong>g++<\/strong> in here  to compile C++ files.  <\/p>\n\n\n\n<p>In the second line, I defined <strong>DEPS<\/strong> variable to list all header (.h) files which are needed to compile implementation (.cpp) files. Since we have only one header file <strong>DEPS = hello.h<\/strong><\/p>\n\n\n\n<p>In the third line, I listed the object compiled file which are result of the compilation of each .cpp file as a variable named <strong>OBJ<\/strong>. We have two .cpp files, one is hello.cpp that has the implementation of hello() function and main.cpp that is the consumer of hell() function in the main() method which is the starting point of the application. <br>So <strong>OBJ = hello.o main.o<\/strong><\/p>\n\n\n\n<p>Next is a two line paragraph, we call it <strong>Rule<\/strong> in Makefile. The target is <strong>%.o<\/strong> which means any file name with .o extension. In the <strong>prerequisites<\/strong> section I defined <strong>%.cpp $(DEPS)<\/strong> which means all .cpp files (hello.cpp and main.cpp) and DEPS variable has hello.h in it. In the <strong>recipe<\/strong> section <strong>$(CC)<\/strong> points to the value of CC variable which is g++ and <strong>-c<\/strong> means \"<em>Only run preprocess, compile, and assemble steps\".<\/em>  <strong>-o<\/strong> is a switch to indicate the name of the output object file which refers to <strong>$@<\/strong> and $@ means the target which is <strong>%.o<\/strong>. And finally $&lt; means just the first prerequisite that equals to <strong>%.cpp<\/strong> that we defined in prerequisite section.<\/p>\n\n\n\n<p>The last rule creates executable file that I named it <strong>greet<\/strong>. The target name is <strong>greet:<\/strong>. Prerequisite is <strong>$(OBJ)<\/strong> that has hello.o main.o values and in the recipe section we link all object files into one executable file called <em>greet<\/em>. $(CC) is the compiler, -o indicates output file, greet in this case that using <strong>$@<\/strong> you refer to target name in the rule you define and $^ list all prerequisite items<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>The value of <strong>'$^<\/strong>' is a list of all the prerequisites of the rule, including the names of the directories in which they were found, and the value of <strong>'$@'<\/strong> is the target<\/p><\/blockquote>\n\n\n\n<p>Now in a terminal run <strong>make<\/strong> to execute Makefile that you created.<\/p>\n\n\n\n<pre>$ make\ng++ -c -o hello.o hello.cpp\ng++ -c -o main.o main.cpp\ng++ -o greet hello.o main.o\n<\/pre>\n\n\n\n<p>And finally run your app:<\/p>\n\n\n\n<pre>$ .\/greet\nHello World!\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we have the same code that we have in Build and run your first application with g++ but here we use make and Makefile to compile and link the source code. You can find the source code of this sample here in Github. The same as previous example, the application contains three [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":274,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-398","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/comments?post=398"}],"version-history":[{"count":10,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/398\/revisions"}],"predecessor-version":[{"id":451,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/398\/revisions\/451"}],"up":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/274"}],"wp:attachment":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/media?parent=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}