如何将使用 boost::asio 的本机 C++ 静态库导入到 CLI/C++ 混合模式应用程序中?

How can I import a native C++ static lib which uses boost::asio into a CLI/C++ mixed mode application?

我有一个 C++/CLI 命令行客户端,我正在尝试导入一个本机 C++ 库,该库又具有 #include <boost/asio.hpp>

当我尝试导入它时出现以下错误:

2>C:\boost_1_54_0\boost/asio/generic/detail/endpoint.hpp(27): error C2059: syntax error : 'generic'
2>C:\boost_1_54_0\boost/asio/generic/detail/endpoint.hpp(27): error C2143: syntax error : missing ';' before '{'
2>C:\boost_1_54_0\boost/asio/generic/detail/endpoint.hpp(27): error C2447: '{' : missing function header (old-style formal list?)
2>C:\boost_1_54_0\boost/asio/generic/detail/impl/endpoint.ipp(32): error C2059: syntax error : 'generic'

包含必须在静态库的 header 中,因为它们是成员变量。

那么从 static/native 库中导入和使用这些 类 的最简单选项是什么?

这个问题已经出现在boost trac上,你可以找到它here。目前的解决方案(由 Michael Rasmussen 提供)是这样做的

#ifdef __cplusplus_cli
#define generic __identifier(generic)
#endif>
#include <boost/filesystem.hpp>
#ifdef __cplusplus_cli
#undef generic
#endif

并包装你的 boost 包含,这些 ifdef 中的通用符号有问题。

编辑:我错过了关于你的库是静态库的一点,你可能想使用动态提升库,它可以避免定义多个符号的问题。使用 BOOST_ALL_DYN_LINK 预处理器定义来代替使用 boost dll。