[Boost-users] Command Line Options Questions

678 views
Skip to first unread message

Jon Lederman

unread,
Mar 6, 2016, 11:43:46 AM3/6/16
to boost...@lists.boost.org
Hi,

I have two questions re: the boost command line options library:

1) I am compiling a dynamic library that links to the boost command line options library as a shared library.  When I try to unload the dynamic library, I find that it isn’t properly unloaded and the boost library seems to be causing the issue.  I did find that things were helped somewhat when I linked to the boost static libraries.  However, it still feels unstable.  Are there any particular things I should know about in this context in terms of how my makefile should be linking to boost?  Or are there other things I should look at.

2)  When using program options, I am getting the following error.  For some reason this seemed to work with the dynamic libraries (or it may be a coincidence).  Am I required to implement the << and >> operators?  It had been compiling previously and stopped.  After googling this issue there seems to be a lot of confusion.  Any help would be greatly appreciated.



in file included from /usr/include/boost/any.hpp:27:0,
                 from /usr/include/boost/program_options/value_semantic.hpp:12,
                 from /usr/include/boost/program_options/options_description.hpp:13,
                 from /usr/include/boost/program_options.hpp:15,
                 from /usr/local/src/freeswitch/src/mod/applications/mod_sc_dsp/mod_sc_dsp.cpp:23:
/usr/include/boost/lexical_cast.hpp: In instantiation of 'struct boost::detail::deduce_target_char_impl<boost::detail::deduce_character_type_later<dsp_type> >':
/usr/include/boost/lexical_cast.hpp:415:89:   required from 'struct boost::detail::deduce_target_char<dsp_type>'
/usr/include/boost/lexical_cast.hpp:674:92:   required from 'struct boost::detail::lexical_cast_stream_traits<std::basic_string<char>, dsp_type>'
/usr/include/boost/lexical_cast.hpp:2363:19:   required from 'static Target boost::detail::lexical_cast_do_cast<Target, Source>::lexical_cast_impl(const Source&) [with Target = dsp_type; Source = std::basic_string<char>]'
/usr/include/boost/lexical_cast.hpp:2543:50:   required from 'Target boost::lexical_cast(const Source&) [with Target = dsp_type; Source = std::basic_string<char>]'
/usr/include/boost/program_options/detail/value_semantic.hpp:89:38:   required from 'void boost::program_options::validate(boost::any&, const std::vector<std::basic_string<charT> >&, T*, long int) [with T = dsp_type; charT = char]'
/usr/include/boost/program_options/detail/value_semantic.hpp:170:55:   required from 'void boost::program_options::typed_value<T, charT>::xparse(boost::any&, const std::vector<std::basic_string<charT> >&) const [with T = dsp_type; charT = char]'
/usr/local/src/freeswitch/src/mod/applications/mod_sc_dsp/mod_sc_dsp.cpp:484:1:   required from here
/usr/include/boost/lexical_cast.hpp:388:13: error: static assertion failed: Target type is neither std::istream`able nor std::wistream`able
             BOOST_STATIC_ASSERT_MSG((result_t::value || boost::has_right_shift<std::basic_istream<wchar_t>, T >::value),
           

desc.add_options()
("help", "Generates help message")
("dsp_type", po::value<dsp_type>(&params.dsp_type)->required(), “")

where dsp_type is this:

struct dsp_type {
dsp_type() {}
dsp_type(std::string const& val):
value(val)
{ }

std::string value;
};

and I have a validator defined as this:

void validate(boost::any& v,
              std::vector<std::string> const& values,
              dsp_type* /* target_type */,
              int)
{
  using namespace boost::program_options;
  //namespace po = boost::program_options;

  // Make sure no previous assignment to 'v' was made.
  validators::check_first_occurrence(v);

  // Extract the first string from 'values'. If there is more than
  // one string, it's an error, and exception will be thrown.
  std::string const& s = validators::get_single_string(values);

  if (s == "ladspa" || s == "audioweaver" || s == "gstreamer" || s == "lv2")  {
    v = boost::any(dsp_type(s));
  } else {
    throw validation_error(validation_error::invalid_option_value);
  }
}





/usr/local/src/freeswitch/build/modmake.rules:214: recipe for target 'mod_sc_dsp.lo' failed
make[1]: *** [mod_sc_dsp.lo] Error 1

Daniel Hofmann

unread,
Mar 6, 2016, 3:50:34 PM3/6/16
to boost...@lists.boost.org, Jon Lederman
You have to implement operator<< otherwise the compiler does not know how to print (pipe it into an ostream) dsp_type.

You can use Boost.Fusion's I/O feature and BOOST_ADAPT_STRUCT like this
> https://github.com/daniel-j-h/argparse-generic/blob/master/example.cc
to make your life easier.

>------------------------------------------------------------------------
>
>_______________________________________________
>Boost-users mailing list
>Boost...@lists.boost.org
>http://lists.boost.org/mailman/listinfo.cgi/boost-users

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Jon Lederman

unread,
Mar 7, 2016, 8:32:58 AM3/7/16
to Daniel Hofmann, boost...@lists.boost.org
Hi,

I tried like this:

std::ostream& operator<<(std::ostream& os, const dsp_type& obj)
{
// write obj to stream
os << obj.value;
return os;
}
std::istream& operator>>(std::istream& is, dsp_type& obj)
{
// read obj from stream
if ( 0 /* T could not be constructed */ )
is.setstate(std::ios::failbit);
is >> obj.value;
return is;

}

However now it let’s everything through and doesn’t validate. What is the proper way to do this?

-Jon

Daniel Hofmann

unread,
Mar 7, 2016, 11:28:53 AM3/7/16
to Jon Lederman, boost...@lists.boost.org
Take a look at the following docs for the validate function:
>
http://www.boost.org/doc/libs/1_60_0/doc/html/program_options/howto.html#idm45555218221088

Vladimir Prus

unread,
Mar 9, 2016, 10:35:55 AM3/9/16
to boost...@lists.boost.org
On 06-Mar-16 7:43 PM, Jon Lederman wrote:
> 1) I am compiling a dynamic library that links to the boost command line options library as a shared library. When I
> try to unload the dynamic library, I find that it isn’t properly unloaded and the boost library seems to be causing the
> issue.

Jon,

could you clarify how you try to unload the dynamic library, for what purpose, and how you determine it's not properly
unloaded?

If you link to program_options normally, just adding it on linker command line, it will be loaded automatically by
the dynamic linker and will remain in memory until your program exits - although if you don't use the library
after initial parsing, the unused code pages may be discarded by the operating system. I doubt explicitly
unloading, with dlclose, is possible in this case.

You possibly can use dlopen to load the library, but I can't see how that would work either, given it's a C++
library with a somewhat wide interface.

Thanks,

--
Vladimir Prus
http://vladimirprus.com
Reply all
Reply to author
Forward
0 new messages