Boost.Asio 仅作为标头
我想在我的项目中使用 Boost 中的 ASIO 库。它的文档说,如果不使用正则表达式并且不使用 SSL,它可以是仅标头的。但是,为 asio
运行 bcp
会拉出很多库,其中一些带有源代码,因此需要编译、bjam
等。
我可以以某种方式使用 ASIO在项目中仅作为标题,没有库/源?我只需要ASIO,不需要Boost的其他部分。
编辑:ASIO 想要 Boost.System
,它有一个要链接的库 - 这种依赖关系不能让我只能使用标头 ASIO 吗?
I want to use ASIO library from Boost in my project. Its doc say it can be header-only if regex is not used and SSL not used. However, running bcp
for asio
pulls a very many libraies some of which are with sources so need compiling, bjam
etc.
Can I somehow use ASIO in project as only headers, without libs/source? I only need ASIO, not other part of Boost.
EDIT: ASIO want Boost.System
which has a lib to link - can this dependency not be so that I can use header only ASIO?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK 您可以从 http://think-async.com/Asio/AsioAndBoostAsio 获取 asio 的非升压版本
“— Boost.Asio 使用 Boost.System 库来提供对错误代码的支持( boost::system::error_code 和 boost::system::system_error)。Asio 将这些包含在自己的命名空间下( asio:: error_code 和 asio::system_error)。这些类的 Boost.System 版本当前支持用户定义的错误代码的更好的可扩展性
- Asio 仅限头文件,并且对于大多数用途不需要链接任何 Boost 库。 Asio 始终要求您链接到 Boost.System 库,如果您想使用 boost::thread 启动线程,还需要链接到 Boost.Thread。”
AFAIK you can get the non-boost version of asio from http://think-async.com/Asio/AsioAndBoostAsio
"— Boost.Asio uses the Boost.System library to provide support for error codes ( boost::system::error_code and boost::system::system_error). Asio includes these under its own namespace ( asio::error_code and asio::system_error). The Boost.System version of these classes currently supports better extensibility for user-defined error codes.
— Asio is header-file-only and for most uses does not require linking against any Boost library. Boost.Asio always requires that you link against the Boost.System library, and also against Boost.Thread if you want to launch threads using boost::thread."
更新 - 07/25/2019:
正如 @OleThomsenBuus 在下面的评论中指出的(谢谢!),从 Boost 1.69 开始,Boost.System 现在仅包含标头,因此无需跳过所有这些环节来消除与其链接的需要。
原始答案:
接受的答案是 100% 有效并推荐的,但另一种选择 - 如果您确实想要/需要使用 Boost Asio - 是尝试使用 <代码>-DBOOST_ERROR_CODE_HEADER_ONLY。使用此宏(记录在此处)应该避免与 Boost.System 链接的需要。但是,值得阅读此答案中指出的警告。特别是,您可能需要创建一个“虚拟”CPP 文件,其中包含:
并仅该文件禁用优化。 (就我个人而言,我不需要这样做,但是 YMMV...)
UPDATE – 07/25/2019:
As noted in the comment below by @OleThomsenBuus (thank you!), from Boost 1.69 onward, Boost.System is now header-only, so there's no need to jump through all these hoops to eliminate the need to link with it.
ORIGINAL ANSWER:
The accepted answer is 100% effective and recommended, but another option—if you really want/need to use Boost Asio—is to try compiling your application with
-DBOOST_ERROR_CODE_HEADER_ONLY
. Use of this macro (documented here) should get around the need to link with Boost.System. However, it's worth reading the caveats pointed out in this answer. In particular, you may need to create a 'dummy' CPP file containing:and disable optimization for that file only. (Personally, I didn't need to do this, but YMMV...)
我认为 bcp 提取了正则表达式库,因为它可以使用(并且在 Windows 机器上默认使用它)。我希望您可以毫无问题地删除正则表达式库源文件。如果您是 Windows 上的编译器,请确保添加正确的编译器标志
(
-DBOOST_DATE_TIME_NO_LIB
和-DBOOST_REGEX_NO_LIB
)详细信息来自 这个页面(听上去你已经找到了)。
我不确定 bcp 有多智能 - 我认为您无法将上面给出的定义传递给它,以防止它遵循 mscv 路由。
I think bcp pulls the regex library because it can be used (and on Windows machines it is used by default). I expect that you can delete the regex library source files no problem. Make sure you add the correct compiler flags if you are compiler on windows
(
-DBOOST_DATE_TIME_NO_LIB
and-DBOOST_REGEX_NO_LIB
)The details are from this page (which by the sounds of it you have already found).
I'm not sure how smart bcp is - I'm don't think you can pass it the defines given above that prevent it following the mscv route.