Build and run your first application with conan

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 to 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 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 make for more help.

And make sure cmake command is installed. If not go to Build and run your first application with cmake for more help.

The easiest way to install conan is using python package manager pip and since you eventually need python to write parts of your build scripts, first we install python3 and pip3 and then we install conan using pip3.

In order to install python3 and pip, for:

  • MacOS
    $ brew install python3
    $ sudo curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    $ sudo python3 get-pip.py
    
  • $ sudo apt install python3
    $ sudo curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    $ sudo python3 get-pip.py
    

For MacOS you can install conan using brew command also:

$ brew update
$ brew install conan

And for Arch linux, if you have yay package manager installed:

$ yay -S conan

After you installed python you can easily install conan with pip and the instructions is same for all operating systems.

$ pip3 install conan

In order to use conan for compilation, the first step is create a file called conanfile.txt in the root of your project with following content:

[requires]

[generators]
cmake
  • In [requires] tag you can define your dependencies
  • In [generators] you specify the build system such as cmake. Read more about Generators.

The next step is to create CMakeLists.txt in the root project with following content:

cmake_minimum_required(VERSION 2.8.12)
project(Hello)

add_definitions("-std=c++11")

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

ADD_LIBRARY(hello hello.cpp)
ADD_EXECUTABLE(greet main.cpp)
TARGET_LINK_LIBRARIES(greet hello)
  • The first tag cmake_minimum_required is to indicate the minimum version of conan. In this sample I set it to 2.8.12
  • The second tag project is to set the project name which I set it to Hello.
  • The 3rd tag add_definitions is to set
  • In the 4th step, you will include conanbuildinfo.cmake path which contains the auto-generated conanbuildinfo.cmake by conan_basic_setup() helper micro.
  • To know what is conan_basic_setup() in the next step, you can refer to here
  • Next for compiling your non-executable files as the shared library objects files you can use ADD_LIBRARY. 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 non-executable target object file.
  • To compile your executable file, you can use ADD_EXECUTABLE. 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 executable target object file.
  • At the end, in order to link all of your compiled object files you can use TARGET_LINK_LIBRARIES. The first parameter is the target executable file that you eventually run in command line and the next parameters are the object file names you are going to link them into that executable file.

Building Hello World application

Create a folder called build and then use conan install command to build the application.

$ mkdir build
$ cd build
$ conan install ..

For Linux and Mac

$ cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
$ cmake --build .

For Windows:

$ cmake .. -G "Visual Studio  Win64"
$ cmake --build . --config Release

Running the app

Run greet command like this:

$ ./bin/greet
Hello World!

Leave a Reply