{"id":401,"date":"2019-04-30T03:37:21","date_gmt":"2019-04-30T03:37:21","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=401"},"modified":"2019-07-21T13:47:54","modified_gmt":"2019-07-21T13:47:54","slug":"build-and-run-your-first-application-with-cmake","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/mycpptutorial\/environment-setup\/build-and-run-your-first-application-with-cmake\/","title":{"rendered":"Build and run your first application with cmake"},"content":{"rendered":"\n<p>This sample is showing you how you can use cmake and CMakeLists.txt file to compile your c++ application.<br><\/p>\n\n\n\n<p>Make sure <strong>g++<\/strong> installed. If not go to <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> for more help.<\/p>\n\n\n\n<p>Also make sure <strong>make<\/strong> command is installed. If not go to <a href=\"http:\/\/kabiliravi.com\/index.php\/software\/programming\/mycpptutorial\/environment-setup\/build-and-run-your-first-application-with-make\/\">Build and run your first application with make<\/a> for more help.<\/p>\n\n\n\n<p>If you face&nbsp;<strong>command not found: cmake<\/strong>&nbsp;that means it is not there.<\/p>\n\n\n\n<p>Run following command to see if <strong>cmake<\/strong> is installed:\n<\/p>\n<pre>$ cmake --version\nzsh: command not found: cmake\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Installing make<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>MacOS <pre>$ brew install cmake<\/pre><\/li><li>Linux<ul><li>Debian\/Ubuntu <pre>$ sudo apt install cmake<\/pre><\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Building Hello World application<\/h2>\n\n\n\n<p>The first step is to create a <strong>CMakeLists.txt<\/strong> file to define the build configuration steps for cmake. Add following content:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cmake_minimum_required (VERSION 2.6)<br>project (Hello)<br>set (HELLO_SRCS hello.h hello.cpp main.cpp)<br>add_executable(greet ${HELLO_SRCS})<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>The first tag <strong>cmake_minimum_required<\/strong> is to indicate the minimum version of <strong>conan.<\/strong> In this sample I set it to 2.8.12<\/li><li>The second tag <strong>project<\/strong> is to set the project name which I set it to <strong>Hello<\/strong>. <\/li><li>The 3rd tag <strong>add_definitions<\/strong> is to set <\/li><li>Next for compiling your non-executable files as the shared library objects files you can use <strong>ADD_LIBRARY<\/strong>. The first parameter is the target and the next parameters are the file names  (.h and .cpp files) you are going to compile them into that <strong>non-executable<\/strong> target object file.<\/li><li>To compile your executable file, you can use <strong>ADD_EXECUTABLE<\/strong>. The first parameter is the target and the next parameters are the source code file names (.h and .cpp files) you are going to compile them into that <strong>executable<\/strong> target object file.<\/li><li>At the end, in order to link all of your compiled object files you can use <strong>TARGET_LINK_LIBRARIES<\/strong>. The first parameter is the target <strong>executable<\/strong> file that you eventually run in command line and the next parameters are the <strong>object file<\/strong> names you are going to link them into that <strong>executable<\/strong> file.<\/li><\/ul>\n\n\n\n<p>Create a folder called <strong>build<\/strong> and then use <strong>cmake<\/strong> command to build the application. It uses <strong>CMakeLists.txt<\/strong> file to compile the sources to object files.<\/p>\n\n\n\n<p>Create a folder called <em>build<\/em> and then use <em>conan install<\/em> command to build the application.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mkdir build<br>$ cd build<br>$ conan install ..<br><\/pre>\n\n\n\n<p>For Linux and Mac<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cmake ..<br>$ cmake --build .<br><\/pre>\n\n\n\n<p>For Windows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cmake .. -G \"Visual Studio  Win64\"<br>$ cmake --build . --config Release<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Running the app<\/h2>\n\n\n\n<p>Run <em><strong>greet<\/strong><\/em> command like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ .\/greet<br> Hello World!<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This sample is showing you how you can use cmake and CMakeLists.txt file to compile your c++ application. Make sure g++ installed. If not go to Build and run your first application with g++ for more help. Also make sure make command is installed. If not go to Build and run your first application with [&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-401","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/401","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=401"}],"version-history":[{"count":13,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/401\/revisions"}],"predecessor-version":[{"id":497,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/401\/revisions\/497"}],"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=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}