{"id":405,"date":"2019-04-30T03:38:01","date_gmt":"2019-04-30T03:38:01","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=405"},"modified":"2019-07-21T13:47:54","modified_gmt":"2019-07-21T13:47:54","slug":"build-and-run-your-first-application-with-conan","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/mycpptutorial\/environment-setup\/build-and-run-your-first-application-with-conan\/","title":{"rendered":"Build and run your first application with conan"},"content":{"rendered":"\n<p>This sample is showing you how you can use <strong>conan<\/strong> to compile your c++ application. I found <a href=\"https:\/\/docs.conan.io\/en\/latest\/index.html\"><strong>conan<\/strong> documentation<\/a> great and you can find your answers there. I also used <strong>docker<\/strong>\u00a0to run this hello world app via a docker container.<\/p>\n\n\n\n<p>Here is the sample git repo for this sample:<br><a href=\"https:\/\/github.com\/mycpptutorial\/helloworld-conan.git\">https:\/\/github.com\/mycpptutorial\/helloworld-conan.git<\/a><br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/mycpptutorial\/helloworld-conan#environment-setup\"><\/a>Environment Setup<\/h2>\n\n\n\n<p>Make sure&nbsp;<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>&nbsp;for more help.<\/p>\n\n\n\n<p>Also make sure&nbsp;<strong>make<\/strong>&nbsp;command is installed. If not go to&nbsp;<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>&nbsp;for more help.<\/p>\n\n\n\n<p>And make sure&nbsp;<strong>cmake<\/strong>&nbsp;command is installed. If not go to&nbsp;<a href=\"http:\/\/kabiliravi.com\/index.php\/software\/programming\/mycpptutorial\/environment-setup\/build-and-run-your-first-application-with-cmake\/\">Build and run your first application with cmake<\/a>&nbsp;for more help.<\/p>\n\n\n\n<p>The easiest way to install&nbsp;<strong>conan<\/strong>&nbsp;is using&nbsp;<strong>python<\/strong>&nbsp;package manager&nbsp;<strong>pip<\/strong>&nbsp;and since you eventually need python to write parts of your build scripts, first we install&nbsp;<strong>python3<\/strong>&nbsp;and&nbsp;<strong>pip3<\/strong>&nbsp;and then we install conan using pip3.<\/p>\n\n\n\n<p>In order to install python3 and pip, for:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>MacOS<pre>$ brew install python3\n$ sudo curl https:\/\/bootstrap.pypa.io\/get-pip.py -o get-pip.py\n$ sudo python3 get-pip.py\n<\/pre><\/li><li><pre>$ sudo apt install python3\n$ sudo curl https:\/\/bootstrap.pypa.io\/get-pip.py -o get-pip.py\n$ sudo python3 get-pip.py\n<\/pre><\/li><\/ul>\n\n\n\n<p>For MacOS you can install conan using&nbsp;<strong>brew<\/strong>&nbsp;command also:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ brew update\n$ brew install conan\n<\/pre>\n\n\n\n<p>And for Arch linux, if you have yay package manager installed:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ yay -S conan\n<\/pre>\n\n\n\n<p>After you installed python you can easily install conan with pip and the instructions is same for all operating systems.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ pip3 install conan<\/pre>\n\n\n\n<p>In order to use&nbsp;<strong>conan<\/strong> for compilation, the first step is create a file called <strong>conanfile.txt<\/strong> in the root of your project with following content:<\/p>\n\n\n\n<pre>[requires]\n\n[generators]\ncmake\n<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>In <strong>[requires]<\/strong> tag you can define your dependencies<\/li><li>In <strong>[generators]<\/strong> you specify the build system such as cmake. Read more about <a href=\"https:\/\/docs.conan.io\/en\/latest\/reference\/generators.html#generators-reference\">Generators<\/a>.<\/li><\/ul>\n\n\n\n<p>The next step is to create <strong>CMakeLists.txt<\/strong> in the root project with following content:<\/p>\n\n\n\n<pre>cmake_minimum_required(VERSION 2.8.12)\nproject(Hello)\n\nadd_definitions(\"-std=c++11\")\n\ninclude(${CMAKE_BINARY_DIR}\/conanbuildinfo.cmake)\nconan_basic_setup()\n\nADD_LIBRARY(hello hello.cpp)\nADD_EXECUTABLE(greet main.cpp)\nTARGET_LINK_LIBRARIES(greet hello)\n<\/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>In the 4th step, you will include<strong> conanbuildinfo.cmake<\/strong> path which contains the auto-generated <strong>conanbuildinfo.cmake<\/strong> by <strong>conan_basic_setup() <\/strong>helper micro.<\/li><li>To know what is <strong>conan_basic_setup()<\/strong> in the next step, you can refer to <a href=\"https:\/\/docs.conan.io\/en\/latest\/reference\/generators\/cmake.html#conan-basic-setup\">here<\/a><\/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<h2 class=\"wp-block-heading\">Building Hello World application<\/h2>\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 .. -G \"Unix Makefiles\" -DCMAKE_BUILD_TYPE=Release<br>$ cmake --build .<\/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\">$ .\/bin\/greet<br> Hello World!<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This sample is showing you how you can use conan to compile your c++ application. I found conan documentation great and you can find your answers there. I also used docker\u00a0to run this hello world app via a docker container. Here is the sample git repo for this sample:https:\/\/github.com\/mycpptutorial\/helloworld-conan.git Environment Setup Make sure&nbsp;g++ installed. If [&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-405","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/405","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=405"}],"version-history":[{"count":11,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/405\/revisions"}],"predecessor-version":[{"id":494,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/405\/revisions\/494"}],"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=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}