samedi 18 décembre 2021

How to properly link Boost Library along with configuration file in CMake

I was making an example program based on Boost Program Option Library. I am also using a configuration header file to pass version number to main code. Here is my main.cpp file content:

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

#include "ver.h"

using namespace boost::program_options;

int main(int argc, const char *argv[])
{
    try
    {
        options_description desc{"Options"};
        desc.add_options()("help,h", "Help screen")("pi", value<float>()->default_value(3.14f), "Pi")("age", value<int>(), "Age")("version,v", "Version");

        variables_map vm;
        store(parse_command_line(argc, argv, desc), vm);
        notify(vm);

        if (vm.count("help"))
            std::cout << desc << std::endl;
        else if (vm.count("age"))
            std::cout << "Age: " << vm["age"].as<int>() << std::endl;
        else if (vm.count("pi"))
            std::cout << "Pi: " << vm["pi"].as<float>() << std::endl;
        else if (vm.count("version"))
            std::cout << "Version: " << ver << std::endl;
    }
    catch (const error &ex)
    {
        std::cerr << ex.what() << std::endl;
    }
}

This is the content of ver.h.in file:

#pragma once

const char *ver = "${BoostProgOpts_VERSION}"

This is the content of CMakeLists.txt file:

cmake_minimum_required(VERSION 3.10)

project("BoostProgOpts")

set(BoostProgOpts_VERSION_MAJOR 0)
set(BoostProgOpts_VERSION_MINOR 1)
set(BoostProgOpts_VERSION_PATCH 1)
set(BoostProgOpts_VERSION "${BoostProgOpts_VERSION_MAJOR}.${BoostProgOpts_VERSION_MINOR}.${BoostProgOpts_VERSION_PATCH}")


find_package(Boost REQUIRED COMPONENTS program_options)

if(Boost_FOUND)
    message("Boost Library Found!")
else()
    message("Boost Library Not Found!")
endif()

configure_file(ver.h.in ${PROJECT_BINARY_DIR}/ver.h)

add_executable(binary main.cpp)

include_directories(${CMAKE_BINARY_DIR} ${Boost_INCLUDE_DIRS})

target_link_libraries(binary ${Boost_LIBRARIES})

But while compiling Boost Library is not linking properly. The error showing is:

[main] Building folder: ProgramOptions 
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/build --config Debug --target all -j 10 --
[build] Consolidate compiler generated dependencies of target binary
[build] [ 50%] Building CXX object CMakeFiles/binary.dir/main.cpp.o
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:6:1: error: expected ‘,’ or ‘;’ before ‘using’
[build]     6 | using namespace boost::program_options;
[build]       | ^~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp: In function ‘int main(int, const char**)’:
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:12:9: error: ‘options_description’ was not declared in this scope; did you mean ‘boost::program_options::options_description’?
[build]    12 |         options_description desc{"Options"};
[build]       |         ^~~~~~~~~~~~~~~~~~~
[build]       |         boost::program_options::options_description
[build] In file included from /usr/include/boost/program_options.hpp:15,
[build]                  from /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:1:
[build] /usr/include/boost/program_options/options_description.hpp:191:38: note: ‘boost::program_options::options_description’ declared here
[build]   191 |     class BOOST_PROGRAM_OPTIONS_DECL options_description {
[build]       |                                      ^~~~~~~~~~~~~~~~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:13:9: error: ‘desc’ was not declared in this scope
[build]    13 |         desc.add_options()("help,h", "Help screen")("pi", value<float>()->default_value(3.14f), "Pi")("age", value<int>(), "Age")("version,v", "Version");
[build]       |         ^~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:13:59: error: ‘value’ was not declared in this scope; did you mean ‘boost::program_options::value’?
[build]    13 |         desc.add_options()("help,h", "Help screen")("pi", value<float>()->default_value(3.14f), "Pi")("age", value<int>(), "Age")("version,v", "Version");
[build]       |                                                           ^~~~~
[build]       |                                                           boost::program_options::value
[build] In file included from /usr/include/boost/program_options/value_semantic.hpp:421,
[build]                  from /usr/include/boost/program_options/options_description.hpp:13,
[build]                  from /usr/include/boost/program_options.hpp:15,
[build]                  from /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:1:
[build] /usr/include/boost/program_options/detail/value_semantic.hpp:197:5: note: ‘boost::program_options::value’ declared here
[build]   197 |     value(T* v)
[build]       |     ^~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:13:65: error: expected primary-expression before ‘float’
[build]    13 |         desc.add_options()("help,h", "Help screen")("pi", value<float>()->default_value(3.14f), "Pi")("age", value<int>(), "Age")("version,v", "Version");
[build]       |                                                                 ^~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:13:116: error: expected primary-expression before ‘int’
[build]    13 |         desc.add_options()("help,h", "Help screen")("pi", value<float>()->default_value(3.14f), "Pi")("age", value<int>(), "Age")("version,v", "Version");
[build]       |                                                                                                                    ^~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:15:9: error: ‘variables_map’ was not declared in this scope; did you mean ‘boost::program_options::variables_map’?
[build]    15 |         variables_map vm;
[build]       |         ^~~~~~~~~~~~~
[build]       |         boost::program_options::variables_map
[build] In file included from /usr/include/boost/program_options.hpp:18,
[build]                  from /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:1:
[build] /usr/include/boost/program_options/variables_map.hpp:146:38: note: ‘boost::program_options::variables_map’ declared here
[build]   146 |     class BOOST_PROGRAM_OPTIONS_DECL variables_map : public abstract_variables_map,
[build]       |                                      ^~~~~~~~~~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:16:15: error: ‘parse_command_line’ was not declared in this scope; did you mean ‘boost::program_options::parse_command_line’?
[build]    16 |         store(parse_command_line(argc, argv, desc), vm);
[build]       |               ^~~~~~~~~~~~~~~~~~
[build]       |               boost::program_options::parse_command_line
[build] In file included from /usr/include/boost/program_options/parsers.hpp:295,
[build]                  from /usr/include/boost/program_options.hpp:17,
[build]                  from /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:1:
[build] /usr/include/boost/program_options/detail/parsers.hpp:103:5: note: ‘boost::program_options::parse_command_line’ declared here
[build]   103 |     parse_command_line(int argc, const charT* const argv[],
[build]       |     ^~~~~~~~~~~~~~~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:16:53: error: ‘vm’ was not declared in this scope; did you mean ‘tm’?
[build]    16 |         store(parse_command_line(argc, argv, desc), vm);
[build]       |                                                     ^~
[build]       |                                                     tm
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:16:9: error: ‘store’ was not declared in this scope; did you mean ‘boost::program_options::store’?
[build]    16 |         store(parse_command_line(argc, argv, desc), vm);
[build]       |         ^~~~~
[build]       |         boost::program_options::store
[build] In file included from /usr/include/boost/program_options.hpp:18,
[build]                  from /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:1:
[build] /usr/include/boost/program_options/variables_map.hpp:48:10: note: ‘boost::program_options::store’ declared here
[build]    48 |     void store(const basic_parsed_options<wchar_t>& options,
[build]       |          ^~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:17:9: error: ‘notify’ was not declared in this scope; did you mean ‘boost::program_options::notify’?
[build]    17 |         notify(vm);
[build]       |         ^~~~~~
[build]       |         boost::program_options::notify
[build] In file included from /usr/include/boost/program_options.hpp:18,
[build]                  from /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:1:
[build] /usr/include/boost/program_options/variables_map.hpp:53:37: note: ‘boost::program_options::notify’ declared here
[build]    53 |     BOOST_PROGRAM_OPTIONS_DECL void notify(variables_map& m);
[build]       |                                     ^~~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:22:50: error: expected primary-expression before ‘int’
[build]    22 |             std::cout << "Age: " << vm["age"].as<int>() << std::endl;
[build]       |                                                  ^~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:24:48: error: expected primary-expression before ‘float’
[build]    24 |             std::cout << "Pi: " << vm["pi"].as<float>() << std::endl;
[build]       |                                                ^~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:28:18: error: ISO C++ forbids declaration of ‘error’ with no type [-fpermissive]
[build]    28 |     catch (const error &ex)
[build]       |                  ^~~~~
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:28:23: error: expected ‘)’ before ‘&’ token
[build]    28 |     catch (const error &ex)
[build]       |           ~           ^~
[build]       |                       )
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:28:24: error: expected ‘{’ before ‘&’ token
[build]    28 |     catch (const error &ex)
[build]       |                        ^
[build] /home/omecamtiv/WorkPlace/BoostLibExamples/ProgramOptions/main.cpp:28:25: error: ‘ex’ was not declared in this scope; did you mean ‘exp’?
[build]    28 |     catch (const error &ex)
[build]       |                         ^~
[build]       |                         exp
[build] make[2]: *** [CMakeFiles/binary.dir/build.make:76: CMakeFiles/binary.dir/main.cpp.o] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/binary.dir/all] Error 2
[build] make: *** [Makefile:91: all] Error 2
[build] Build finished with exit code 2

Please help me regarding this.

Aucun commentaire:

Enregistrer un commentaire