CMake make multiple errors when include boost/beast/http.hpp

CMake multiple errors when include boost/beast/http.hpp

我正在尝试通过 cmake 构建使用 boost beast 库的项目。 当我只使用 boost asio 库时,一切正常。但是当我添加 boost/beast/http.hpp header 时,cmake 会给出大量错误。

CMake 文件:


cmake_minimum_required(VERSION 3.10)

project(ConsoleChat.Server)

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

find_package(Boost 1.75.0 REQUIRED COMPONENTS system filesystem)

add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY)

include_directories(${Boost_INCLUDE_DIRS})

link_directories(${Boost_LIBRARY_DIRS})

add_executable(ConsoleChat.Server main.cpp)
target_link_libraries(ConsoleChat.Server ${Boost_LIBRARIES})

代码文件:

#include <cstdlib>
#include <memory>
#include <thread>
#include <iostream>
#include <string>

#include <boost/asio.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/version.hpp>
#include <boost/beast/http.hpp>

namespace net = boost::asio;
namespace beast = boost::beast;
namespace http = beast::http;
using tcp = boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
    try
    {
        if (argc != 3)
        {
            std::cout <<
                "Usage: websocket-chat-multi <address> <port>\n" <<
                "Example:\n" <<
                "    websocket-chat-server 0.0.0.0 8080\n";
            return EXIT_FAILURE;
        }

        auto const address = net::ip::address::from_string(argv[1]);//net::ip::make_address(argv[1]); 
        auto const port = static_cast<unsigned short>(std::atoi(argv[2]));

        net::io_service ios{1};

        tcp::acceptor acceptor{ios, {address, port}};
    
        for (;;)
        {
            tcp::socket socket{ios};

            acceptor.accept(socket);
        }
    }
    catch(const std::exception& e)
    {
        std::cout << e.what() << '\n';
    }
}

错误示例:

In file included from /usr/local/include/boost/beast/core/buffer_traits.hpp:14,
                 from /usr/local/include/boost/beast/http/basic_dynamic_body.hpp:14,
                 from /usr/local/include/boost/beast/http.hpp:15,
                 from /home/kudryavii/projects/ConsoleChat/ConsoleChat.Server/main.cpp:10:
/usr/local/include/boost/beast/core/detail/buffer_traits.hpp:66:18: error: ‘is_const_buffer_sequence’ is not a member of ‘boost::beast::net’
   66 |             net::is_const_buffer_sequence<B>::value>::type>
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/beast/core/detail/buffer_traits.hpp:66:18: error: ‘is_const_buffer_sequence’ is not a member of ‘boost::beast::net’

我尝试添加所有增强库来查找包部分,但这没有帮助。

是的。 preprocessed source 确认第一次提到 is_const_buffer_sequence 来自第 116485 行(eek),这归因于

# 49 "/usr/local/include/boost/beast/core/detail/buffer_traits.hpp" 3

来自第 116467 行。在我的系统中,这不是第一次提到,因为(显然?)boost_1_75_0/boost/asio/buffer.hpp 中的声明在:

之前
# 2422 "/home/sehe/custom/boost_1_75_0/boost/asio/buffer.hpp" 2 3 4

它的包含树检出为¹

/usr/include/boost/asio.hpp -->
  /usr/include/boost/asio/basic_datagram_socket.hpp -->
    /usr/include/boost/asio/datagram_socket_service.hpp -->
      /usr/include/boost/asio/detail/null_socket_service.hpp -->
        /usr/include/boost/asio/buffer.hpp

那么,让我们看看是否可以为您的预处理源追踪相似的路径。

减少所有行指令(通过剥离实际行号,并消除后续重复)我看到了预期的踪迹。但是,然后我发现了差异:

# 124 "/usr/local/include/boost/asio/buffer.hpp" 3
private:
  friend void* boost::asio::detail::buffer_cast_helper(
      const mutable_buffer& b);
  friend std::size_t boost::asio::detail::buffer_size_helper(
      const mutable_buffer& b);

  void* data_;
  std::size_t size_;

嗯?在 1.75.0 中,这些朋友不存在:https://github.com/boostorg/asio/blob/boost-1.75.0/include/boost/asio/buffer.hpp

你显然在那里安装了 Boost 1.75.0 以外的东西 - 这很有趣,因为 CMake 的 FindPackage 应该抱怨?无论如何,再探索一些:

git grep -l is_const_buffer_sequence boost-1.{60..75}.0 | grep buffer.hpp
boost-1.66.0:include/boost/asio/buffer.hpp
boost-1.67.0:include/boost/asio/buffer.hpp
boost-1.68.0:include/boost/asio/buffer.hpp
boost-1.69.0:include/boost/asio/buffer.hpp
boost-1.70.0:include/boost/asio/buffer.hpp
boost-1.71.0:include/boost/asio/buffer.hpp
boost-1.72.0:include/boost/asio/buffer.hpp
boost-1.73.0:include/boost/asio/buffer.hpp
boost-1.74.0:include/boost/asio/buffer.hpp
boost-1.75.0:include/boost/asio/buffer.hpp

也许还可以找出好友何时被删除:

git grep -l friend boost-1.{60..75}.0 | grep buffer.hpp
boost-1.60.0:include/boost/asio/buffer.hpp
boost-1.61.0:include/boost/asio/buffer.hpp
boost-1.62.0:include/boost/asio/buffer.hpp
boost-1.63.0:include/boost/asio/buffer.hpp
boost-1.64.0:include/boost/asio/buffer.hpp
boost-1.65.0:include/boost/asio/buffer.hpp

就在同一时刻。看起来我们可以将 upper-bound 精确定位到你的 boost asio header:

git log -Sfriend include/boost/asio/buffer.hpp
commit b60e92b13ef68dfbb9af180d76eae41d22e19356
Author: Christopher Kohlhoff <chris@kohlhoff.com>
Date:   Mon Oct 23 14:27:36 2017 +1100

    Initial merge of Networking TS compatibility.
    
    Merged from chriskohlhoff/asio master branch as of commit
    4a4d28b0d24c53236e229bd1b5f378c9964b1ebb.

这与引入 is_const_buffer_sequence 的提交相同。 Git 确认您的 Asio 版本肯定是 boost 1.65.0 或更早的版本²:

git tag --contains b60e92b13ef68dfbb9af180d76eae41d22e19356
boost-1.66.0
boost-1.67.0
boost-1.68.0
boost-1.69.0
boost-1.69.0-beta1
boost-1.70.0
boost-1.70.0.beta1
boost-1.71.0
boost-1.71.0.beta1
boost-1.72.0
boost-1.72.0.beta1
boost-1.73.0
boost-1.73.0.beta1
boost-1.74.0
boost-1.74.0.beta1
boost-1.75.0
boost-1.75.0.beta1

Now the wild thing is: Boost Beast didn't exist until Boost 1.66.0

简而言之,您的安装已损坏。首先,您有一个带有较新 Beast header 的旧 Asio。其次,它的安装方式使得 CMake 显然将其检测为满足 1.75.0 要求的有效 Boost 安装。


¹(这不是 100% 完全保真,因为我使用 vim 的 :check! 命令进行了检查,该命令不使用 libclang 或任何 options/preprocessor 感知)

² 当然,只查看 /usr/include/boost/version.hpp 或打印 BOOST_LIB_VERSION:

的值会容易得多
std::cout << BOOST_LIB_VERSION << "\n";