Opened 6 years ago

Last modified 5 years ago

#7910 new Bugs

MSVC-11 can't find some symbols in Boost::System static library

Reported by: Gene Panov Owned by: Beman Dawes
Milestone: To Be Determined Component: system
Version: Boost 1.52.0 Severity: Showstopper
Keywords: undefined symbol boost system generic_category system_category Cc: bdawes@…

Description

#ifndef BOOST_SYSTEM_ERROR_CATEGORY_DIRTY_WORKAROUND
#define BOOST_SYSTEM_ERROR_CATEGORY_DIRTY_WORKAROUND

// I have not had a chance to investigate the root cause of this problem, 
// but it happens when I use boost::asio on MSVC-11 (Visual Studio 2012).
// The problem seems to be that boost::system defines generic_error_category and system_error_category, 
// but then somehow doesn't have these symbols in the libboost_system-vc110-mt-1_52.lib library when you build boost 
// with just "/b2" with and toolset=msvc-11.0

// For me the problem happened when I was trying to use boost::asio from something I compiled with MSVC-11.0.
// I got linker errors, so it's a showstopper.
// Feel free to contact me at MyName.MyLastName@Gmail.com for more detail.

// Now, I found that if you build boost on your machine with /b2 and then specifically do these two steps:
// > b2 --with-system threading=multi link=static runtime-link=shared clean
// > b2 --with-system threading=multi link=static runtime-link=shared
// then magicaly the problem goes away and you don't need this workaround below it seems.

// But since the root cause is not clear to me, the problem may not go away for you after the steps above.
// So, if it didn't go away for you indeed, feel free to add this piece of source code into your application.
// Two ways to do it:
//  1. if you are adding this piece of code as an include file, uncomment these two lines:
// #undef BOOST_SYSTEM_ERROR_CATEGORY_DIRTY_WORKAROUND 
// #define BOOST_SYSTEM_ERROR_CATEGORY_DIRTY_WORKAROUND inline
//  2. but if you are adding it as a separate compilation unit (source file), then don't uncomment anything

#if defined(_MSC_VER) && _MSC_VER >= 1700

#include <system_error> // looks like implementations of three error categories are defined in MSVC 11 include files in 'std' namespace
#include <boost/system/error_code.hpp> // interfaces for those error categories required by boost in 'boost' namespace, but somehow implementations cannot be found in the library files

// glue that hooks up the implementations from MSVC11 to the interfaces required by boost
// (this glue code is **unsafe** in case MSVC or Boost change interfaces for these error categories, because reinterpret_cast is used,
// so a safer way to go about it would be to wrap std::error_category& as a field of some wrapper class that inherits from boost::system::error_category,
// but it works with these versions, because boost and std interfaces are the same ("binary compatible"!) now and I don't yet have time to do it properly)
namespace boost { namespace system {
	error_category const& system_category() { 
		return reinterpret_cast<const error_category&>(std::system_category()); 
	}
	error_category const& generic_category() { 
		return reinterpret_cast<const error_category&>(std::generic_category()); 
	}
	error_category const& iostream_category() {
		return reinterpret_cast<const error_category&>(std::iostream_category());
	}
}}

#endif // defined(_MSC_VER) && _MSC_VER >= 1700

#endif // BOOST_SYSTEM_ERROR_CATEGORY_DIRTY_WORKAROUND

Change History (3)

comment:1 Changed 6 years ago by Gene Panov

Actually the code should be this:

namespace boost { namespace system {
	BOOST_SYSTEM_ERROR_CATEGORY_DIRTY_WORKAROUND error_category const& system_category() { 
		return reinterpret_cast<const error_category&>(std::system_category()); 
	}
	BOOST_SYSTEM_ERROR_CATEGORY_DIRTY_WORKAROUND error_category const& generic_category() { 
		return reinterpret_cast<const error_category&>(std::generic_category()); 
	}
	BOOST_SYSTEM_ERROR_CATEGORY_DIRTY_WORKAROUND error_category const& iostream_category() {
		return reinterpret_cast<const error_category&>(std::iostream_category());
	}
}}

comment:2 Changed 5 years ago by viboes

Please could you check if [81808] fixes the issue?

comment:3 Changed 5 years ago by viboes

PING !!!

Note: See TracTickets for help on using tickets.