The development system is:
MacBook Pro Intel i9
MacOS  Big Sur 11.4
gcc version 11.1.0 (Homebrew GCC 11.1.0_1) 
clang version 12.0.0

I'm having trouble compiling some examples from the book 'Boost C ++ 
Application Development Cookbook’.
The problem occurs only with g ++, with clang there is no error.
Mistakes only occur when linking to -lboost_program_options, -lboost_system and 
-lboost_filesystem, with any other library (eg -lboost_chrono) everything is 
fine.

Furthermore, it should be noted that by linking with  -lboost_system and 
-lboost_filesystem no compilation / linking errors are obtained but we get a 
SEGMENTATION FAULT  (ONLY when compiling with g++, compiling with Clang the 
program run fine), instead linking with -lboost_program_options results in 
linking errors.

This is the example:

#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;
}

and these are the errors:

Undefined symbols for architecture x86_64:
  
"__ZN5boost15program_options11to_internalERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE",
 referenced from:
      
__ZN5boost15program_options11to_internalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESt6vectorIS7_SaIS7_EERKS8_IT_SaISB_EE
 in ccmlvHtI.o
  
"__ZN5boost15program_options16validation_error12get_templateB5cxx11ENS1_6kind_tE",
 referenced from:
      
__ZN5boost15program_options16validation_errorC1ENS1_6kind_tERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_i
 in ccmlvHtI.o
  
"__ZN5boost15program_options19options_descriptionC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjj",
 referenced from:
      _main in ccmlvHtI.o
  
"__ZN5boost15program_options20invalid_option_valueC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE",
 referenced from:
      
__ZN5boost15program_options8validateIicEEvRNS_3anyERKSt6vectorINSt7__cxx1112basic_stringIT0_St11char_traitsIS7_ESaIS7_EEESaISB_EEPT_l
 in ccmlvHtI.o
  
"__ZN5boost15program_options22error_with_option_nameC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES9_S9_i",
 referenced from:
      
__ZN5boost15program_options16validation_errorC1ENS1_6kind_tERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_i
 in ccmlvHtI.o
  "__ZN5boost15program_options3argB5cxx11E", referenced from:
      __ZNK5boost15program_options11typed_valueIicE4nameB5cxx11Ev in ccmlvHtI.o
  
"__ZN5boost15program_options6detail7cmdline21set_additional_parserENS_9function1ISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_ERKSA_EE",
 referenced from:
      
__ZN5boost15program_options25basic_command_line_parserIcE12extra_parserENS_9function1ISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_ERKSA_EE
 in ccmlvHtI.o
  
"__ZN5boost15program_options6detail7cmdlineC2ERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS9_EE",
 referenced from:
      __ZN5boost15program_options25basic_command_line_parserIcEC1EiPKPKc in 
ccmlvHtI.o
  "__ZN5boost15program_optionslsERSoRKNS0_19options_descriptionE", referenced 
from:
      _main in ccmlvHtI.o
  
"__ZNK5boost15program_options22abstract_variables_mapixERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE",
 referenced from:
      
__ZNK5boost15program_options13variables_mapixERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
 in ccmlvHtI.o
  
"__ZNK5boost15program_options22error_with_option_name23substitute_placeholdersERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE",
 referenced from:
      __ZTVN5boost15program_options20invalid_option_valueE in ccmlvHtI.o
      __ZTVN5boost15program_options16validation_errorE in ccmlvHtI.o
      __ZTVN5boost10wrapexceptINS_15program_options16validation_errorEEE in 
ccmlvHtI.o
      __ZTVN5boost10wrapexceptINS_15program_options20invalid_option_valueEEE in 
ccmlvHtI.o
  
"__ZNK5boost15program_options29value_semantic_codecvt_helperIcE5parseERNS_3anyERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EEb",
 referenced from:
      __ZTVN5boost15program_options11typed_valueIicEE in ccmlvHtI.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
The terminal process "zsh '-c', 'g++-11 -g 
-I/usr/local/Cellar/boost/1.76.0/include/ -I/usr/local/include/ 
-L/usr/local/Cellar/boost/1.76.0/lib/ -lboost_program_options -std=gnu++20 
Chapter01/01_A_program_options_base/main.cpp -o 
Chapter01/01_A_program_options_base/main'" terminated with exit code: 1.

The compile options are (Visual Studio Code):

"label": "Build with g++ 11",
        "type": "shell",
        "command": "g++-11", 
        "args": [
          "-g",
          "-I/usr/local/Cellar/boost/1.76.0/include/",
          "-I/usr/local/include/",
          "-L/usr/local/Cellar/boost/1.76.0/lib/",
          "-lboost_program_options",
          // "-lboost_system",
          // "-lboost_filesystem",
          // "-lboost_chrono",
          "-std=gnu++20",
          "Chapter01/01_A_program_options_base/main.cpp",
        //  "-L/usr/local/Cellar/gcc/11.1.0_1/lib/gcc/11/",
          "-o",
          "Chapter01/01_A_program_options_base/main"
        ]
Thanks
Stefano Gragnani
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
https://lists.boost.org/mailman/listinfo.cgi/boost-users

Reply via email to