apolukhin / Boost-Cookbook

Online examples from "Boost C++ Application Development Cookbook":
http://apolukhin.github.io/Boost-Cookbook/
Boost Software License 1.0
401 stars 108 forks source link

start up on Raspberry pi [SOLVED] #13

Closed zoldaten closed 7 months ago

zoldaten commented 11 months ago

hi ! thanks for the Book. but it took me a time to just start up with even "Hello world" example ) i work on Raspberry pi Raspbian bullseyes. so for those whos suffring as i was i wrote short intro how to start with boost on raspberry pi:

1. sudo apt install libboost-all-dev

2. nano main.cpp

#include <boost/program_options.hpp>
#include <iostream>

namespace opt = boost::program_options;

int main(int argc, char *argv[])
{
    // Constructing an options describing variable and giving it a
    // textual description "All options".
    opt::options_description desc("All options");

    // When we are adding options, first parameter is a name
    // to be used in command line. Second parameter is a type
    // of that option, wrapped in value<> class. Third parameter
    // must be a short description of that option.
    desc.add_options()
        ("apples", opt::value<int>(), "how many apples do you have")
        ("oranges", opt::value<int>(), "how many oranges do you have")
        ("help", "produce help message")
    ;

    // Variable to store our command line arguments.
    opt::variables_map vm;

    // Parsing and storing arguments.
    opt::store(opt::parse_command_line(argc, argv, desc), vm);

    // Must be called after all the parsing and storing.
    opt::notify(vm);

    if (vm.count("help")) {
        std::cout << desc << "\n";
        return 1;
    }

    std::cout << "Fruits count: "
        << vm["apples"].as<int>() + vm["oranges"].as<int>()
        << std::endl;

} // end of `main`

3. nano CMakeLists.txt

cmake_minimum_required(VERSION 3.3)
project(BoostTest)

FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
#set(CMAKE_CXX_FLAGS "-lboost_program_options")

set(SOURCE_FILES main.cpp)
add_executable(BoostTest ${SOURCE_FILES})
target_link_libraries(BoostTest ${Boost_LIBRARIES} -lpthread)
target_link_libraries(BoostTest ${Boost_LIBRARIES} -lboost_program_options)

4.

cmake . 
make

./BoostTest --apples 20 --oranges 30 Screenshot_1

apolukhin commented 7 months ago

Many thanks for a good instruction!