Repository navigation
Link Error when use variables_map::contains method #117
Description
Activity
This is quite an old issue, but you're getting this error because your Boost libraries aren't targeting C++20.
As you correctly noted
variables_mapinherits fromstd::map, so while your compiler is happy to use C++20 methods likestd::map::contains, if Boost isn't being built for C++20 then the symbols won't be available at link time. I would look into howvcpkgbuilds Boost - it's almost certainly targeting an older standard.In the meantime,
vm.find("--help") != vm.end()will work just fine and is exactly the same asvm.contains("--help"), albeit less convenient.Reacted by Vladimir Prus and Xuntao ChiWould dropping BOOST_PROGRAM_OPTIONS_DECL on types help? Currently, BOOST_PROGRAM_OPTIONS_DECL is used on classes like variables_map and variable_value, which would make the types part of the DLL interface, causing linking problems like this.
I removed it from the class definition and friend class declaration, and it links. It would allow MSVC to instantiate templates in the code that uses the types, rather than in DLL. Also, I didn't find other boost components that uses DECL macros on types.Here are my modified version of the
variables_map.hppfor reference--- a/boost/program_options/variables_map.hpp 2024-04-08 08:17:53.000000000 +0800 +++ b/boost/program_options/variables_map.hpp 2025-01-17 12:42:55.994258200 +0800 @@ -55,7 +55,7 @@ /** Class holding value of option. Contains details about how the value is set and allows to conveniently obtain the value. */ - class BOOST_PROGRAM_OPTIONS_DECL variable_value { + class variable_value { public: variable_value() : m_defaulted(false) {} variable_value(const boost::any& xv, bool xdefaulted) @@ -98,12 +98,12 @@ void store(const basic_parsed_options<char>& options, variables_map& m, bool); - friend class BOOST_PROGRAM_OPTIONS_DECL variables_map; + friend class variables_map; }; /** Implements string->string mapping with convenient value casting facilities. */ - class BOOST_PROGRAM_OPTIONS_DECL abstract_variables_map { + class abstract_variables_map { public: abstract_variables_map(); abstract_variables_map(const abstract_variables_map* next); @@ -143,7 +143,7 @@ This class is derived from std::map<std::string, variable_value>, so you can use all map operators to examine its content. */ - class BOOST_PROGRAM_OPTIONS_DECL variables_map : public abstract_variables_map, + class variables_map : public abstract_variables_map, public std::map<std::string, variable_value> { public:
Env:
Windows 10 19044.1826Library Config:
vcpkg, all ports are updated to date.IDE:
Visual Studio 2022 17.2.6Boost Version:
1.79.0C++ Version:
C++20When i use
program_optionslibrary in my program, i got a link error as follows.Other boost libraries are work well with my vcpkg and VS env.
The reproduce code:
I noticed that
variables_mapare inherited fromstd::map, is something wrong with here?