caiorss / conan-cpp-recipes

Lots of conan recipes and CMake/Conan project-templates in a single repository.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conan Recipes for many C++ Packages

Install all conan recipes

Enter in this directory and run the command:

$ make install 

Create a new recipe

Create recipe project skeleton:

$ mkdir -p recipes/<RECIPE_NAME> && cd recipes/<RECIPE_NAME> 
$ conan new <RECIPE_NAME>/<VERSION> -t 

Modify the files:

  • recipes/<RECIPE_NAME>/conanfile.py
  • recipes/<RECIPE_NAME>/test_package/example.cpp
  • recipes/<RECIPE_NAME>/test_package/CMakeLists.txt

Test the package with:

$ conan create recipes/<RECIPE_NAME> <RECIPE_NAME>/testing

It will attempt to install the package and run the file example.cpp in the directory recipes/<RECIPE_NAME>/test_package/example.cpp. If the package code correct, the file example.cpp will be compiled and executed.

Example:

$ mkdir -p recipes/mylibrary && cd recipes/mylibrary 
$ conan new mylibrary/0.1 -t 

It generates the following directory structure within the directory recpies/mylibrary

$ cd recipes/mylibrary 

$ tree 
.
├── conanfile.py
└── test_package
    ├── CMakeLists.txt
    ├── conanfile.py
    └── example.cpp

1 directory, 4 files

Now modify the following files:

  • recipes/mylibrary/conanfile.py
  • recipes/mylibrary/test_package/example.cpp

Recipes

Package cpp-httplib - header-only http server

Overview

The library cpp-httplib is a single-file header-only cross-platform library for building HTTP/HTTPS standalone servers.

Testing package

Enter package directory

$ cd cpp-httplib && conan create . recipes/httplibtesting

This command downloads the library source from the remote repository; installs the library in the local cache package directory and then runs the test executable recipes/httplib/test_package/example.cpp,

File: example.cpp

#include <iostream> 
#include <httplib.h>

int main(void)
{
    using namespace httplib;

    std::puts("\n\n ===================================");
    std::puts(" [INFO] Starting Web server. OK");
  
    Server svr;

    svr.Get("/hi", [](const Request& req, Response& res) {
        std::puts(" [INFO] Server route /hi");
        res.set_content("Hello World!", "text/plain");
    });

    svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
        std::puts(" [INFO] Server route /numbers");			  
        auto numbers = req.matches[1];
        res.set_content(numbers, "text/plain");
    });

    svr.Get("/stop", [&](const Request& req, Response& res) {
        std::puts(" [INFO] Server route /stop");			  
        svr.stop();
    });

    std::puts(" [INFO] Running server - listening port 1234");			  
    svr.listen("localhost", 1234);
}

If the command runs successfuly, the executable ‘example’ shows the following message:

Scanning dependencies of target example
[ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o
[100%] Linking CXX executable bin/example
[100%] Built target example
recipes/httplib0.1@cpp-httplib/testing (test package): Running test()


 ===================================
 [INFO] Starting Web server. OK
 [INFO] Running server - listening port 1234

Testing with curl in another terminal:

  • Route: /hi => URL http://localhost:1234/hi
$ curl -v localhost:1234/hi

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 1234 (#0)
> GET /hi HTTP/1.1
> Host: localhost:1234
> User-Agent: curl/7.59.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Length: 12
< Content-Type: text/plain
< 
* Connection #0 to host localhost left intact
Hello World!
  • Route: /numbers/<NUMBER> => url: http://localhost:1234/numbers/2431
$ curl -v localhost:1234/numbers/2431
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 1234 (#0)
> GET /numbers/2431 HTTP/1.1
> Host: localhost:1234
> User-Agent: curl/7.59.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Length: 4
< Content-Type: text/plain
< 
* Connection #0 to host localhost left intact
2431

Installing Locally

Conan reference for package:

  • cpp-httplib/0.1@local/testing

Run the command:

$ make install-httplib

Output:

$ make install
conan export ./recipes/httplib local/testing 
Exporting package recipe
Linter warnings
    WARN: Linter. Line 1: Unused import os
    WARN: Linter. Line 2: Unused CMake imported from conans
    WARN: Linter. Line 2: Unused tools imported from conans
cpp-httplib/0.1@local/testing: A new conanfile.py version was exported
cpp-httplib/0.1@local/testing: Folder: /home/archbox/.conan/data/cpp-httplib/0.1/local/testing/export

Checking Local Installation:

$ conan info cpp-httplib/0.1@local/testing
cpp-httplib/0.1@local/testing
    ID: 5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9
    BuildID: None
    Remote: None
    URL: <Package recipe repository url here, for issues about the package>
    License: Public Domain Package Recipe
    Author: Caio Rodrigues
    Topics: http, web, server
    Recipe: Cache
    Binary: Missing
    Binary remote: None
    Creation date: 2019-08-19 14:06:24

Building sample project

Note: It assumes that the package has already been installed with:

$ make install-httplib 

File: CMakeLists.txt

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(cmake-experiment)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)

# ============= Conan Bootstrap =============================#

# Download automatically, you can also just copy the conan.cmake file
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
   message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
   file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.13/conan.cmake"
                 "${CMAKE_BINARY_DIR}/conan.cmake")
endif()

include(${CMAKE_BINARY_DIR}/conan.cmake)

conan_cmake_run( REQUIRES
                # Libraries to be installed locally
                cpp-httplib/0.1@local/testing

                BASIC_SETUP
                BUILD missing
                )

#  ========= Target Configuration ==================== #

add_executable(server server.cpp)
target_link_libraries(server pthread)

File: server.cpp

#include <iostream> 
#include <httplib.h>

int main(void)
{
    using namespace httplib;

    std::puts("\n\n ===================================");
    std::puts(" [INFO] Starting Web server. OK");
  
    Server svr;

    svr.Get("/hi", [](const Request& req, Response& res) {
        std::puts(" [INFO] Server route /hi");
        res.set_content("Hello World!", "text/plain");
    });

    svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
        std::puts(" [INFO] Server route /numbers");			  
        auto numbers = req.matches[1];
        res.set_content(numbers, "text/plain");
    });

    svr.Get("/stop", [&](const Request& req, Response& res) {
        std::puts(" [INFO] Server route /stop");			  
        svr.stop();
    });

    std::puts(" [INFO] Running server - listening port 1234");			  
    svr.listen("localhost", 1234);
}

Build project:

$ cd sample-projects/httplib 
$ cmake -H. -B_build -DCMAKE_BUILD_TYPE=Debug
$ cmake --build _build --target  

Run Server:

$ _build/bin/server 

 ===================================
 [INFO] Starting Web server. OK
 [INFO] Running server - listening port 1234

Package cpptoml - header-only TOML configuration parser

CPPtoml is header-only library for the TOML configuration format which a format similar to Windows INI, however it is platform-agnostic and standardized. This advantage of this format is that is more human-readable and more lightweight than XML or Json and less error prone than YAML.

Library Repository:

Recipe:

Install Cpptoml Recipe

$ make install-cpptoml 

Test Cpptoml Recipe

It run the file recipes/cpptoml/test_package/example.cpp

$ make test-cpptoml

If the test is successful, it produces the following output:

--- TOML Configuration data read from input stream ------
=> loglevel = 10
=> userName = Somebody else
=> file     = C:\Users\somebody\storage\data.log
=> port     = 9090

CMakeLists.txt - Usage Example:

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(cmake-experiment)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)

# ============= Conan Bootstrap =============================#

# Download automatically, you can also just copy the conan.cmake file
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
   message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
   file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.13/conan.cmake"
                 "${CMAKE_BINARY_DIR}/conan.cmake")
endif()

include(${CMAKE_BINARY_DIR}/conan.cmake)

conan_cmake_run( REQUIRES
                # Libraries to be installed locally
                cpptoml/0.1@local/testing

                BASIC_SETUP
                BUILD missing
                )

#  ========= Target Configuration ==================== #
add_executable(cpptoml1 example.cpp)

Package served - http rest web server library

  • “Served is a C++ library for building high performance RESTful web servers. Served builds upon Boost.ASIO to provide a simple API for developers to create HTTP services in C++. Features: HTTP 1.1 compatible request parser; Middleware / plug-ins; Flexible handler API; Cross-platform compatible”

Repository:

Recipe:

Install Served Recipe

$ make install-served

Test Served Recipe

$ make test-served 

CMakeLists.txt - Usage Example:

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(cmake-experiment)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)

# ============= Conan Bootstrap =============================#

# Download automatically, you can also just copy the conan.cmake file
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
   message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
   file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.13/conan.cmake"
                 "${CMAKE_BINARY_DIR}/conan.cmake")
endif()

include(${CMAKE_BINARY_DIR}/conan.cmake)

conan_cmake_run( REQUIRES
                # Libraries to be installed locally
                served/0.1@local/testing

                BASIC_SETUP
                BUILD missing
                )

#  ========= Target Configuration ==================== #
add_executable(server1 example.cpp)
target_link_libraries(server1 pthread served)

File: example.cpp

#include <served/served.hpp>

int main(int argc, char const* argv[]) {
        // Create a multiplexer for handling requests
        served::multiplexer mux;

        // GET /hello
        mux.handle("/hello")
                .get([](served::response & res, const served::request & req) {
                        res << "Hello world!";
                });

        // Create the server and run with 10 handler threads.
        served::net::server server("127.0.0.1", "9080", mux);
        server.run(10);
	
        return (EXIT_SUCCESS);
}

Test the server with:

$ curl -v localhost:9080/hello 

About

Lots of conan recipes and CMake/Conan project-templates in a single repository.


Languages

Language:Python 53.9%Language:C++ 24.0%Language:CMake 15.8%Language:Makefile 6.3%