【问题标题】:Cannot build boost asio with clang 12 on windows无法在 Windows 上使用 clang 12 构建 boost asio
【发布时间】:2021-09-10 21:51:01
【问题描述】:

我的项目设置使用最新的 vcpkg、boost 1.75 和 clang 12。这适用于 MSVC2019 编译器,但在使用最新的 clang 编译时会产生协程错误。这很容易通过这个简单的 cmake 设置重新创建。我测试了不同的手动编译器标志,但都不起作用。

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

project(cpp_20_compile_test_boost LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(cpp_20_compile_test_boost  main.cpp)

# This check only works for clang
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-fcoroutines-ts" support_coroutine)
if(NOT support_coroutine)
    message(FATAL_ERROR "The compiler doesn't support C++ Coroutines")
endif()

# Still not working when setting this:
# set(CMAKE_CXX_FLAGS "-fcoroutines-ts --stdlib=libc++ -Xclang -fconcepts-ts")

# No change when setting this flag either:
# target_compile_definitions(${PROJECT_NAME} PUBLIC _WINDOWS BOOST_ASIO_HAS_STD_COROUTINE)

# ######### BOOST ##########
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(Boost_USE_STATIC_LIBS ON)

find_package(    Boost    COMPONENTS filesystem thread regex date_time chrono    REQUIRED)

target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${Boost_LIBRARY_DIRS})
target_link_libraries(cpp_20_compile_test_boost PRIVATE ${Boost_LIBRARIES})

target_compile_definitions(${PROJECT_NAME} PUBLIC _WINDOWS BOOST_ASIO_HAS_CO_AWAIT  )

main.cpp

#include <SDKDDKVer.h>
#include <boost/asio.hpp>

int main(int argc, char *argv[]){}

错误:

[0/1 ?/sec] Re-running CMake...
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Backup/Code/Qt/build-cpp_20_compile_test_boost-CLANG_K3000_Qt_5_15_2_MSVC2019_64bit-Debug
[1/2 0.3/sec] Building CXX object CMakeFiles/cpp_20_compile_test_boost.dir/main.cpp.obj
FAILED: CMakeFiles/cpp_20_compile_test_boost.dir/main.cpp.obj 
C:\PROGRA~1\LLVM\bin\CLANG_~1.EXE -DBOOST_ASIO_HAS_CO_AWAIT -D_WINDOWS -I. -ID:/Backup/Code/Qt/cpp_20_compile_test_boost -ID:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include -DQT_QML_DEBUG -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -DQT_QML_DEBUG -std=gnu++20 -MD -MT CMakeFiles/cpp_20_compile_test_boost.dir/main.cpp.obj -MF CMakeFiles\cpp_20_compile_test_boost.dir\main.cpp.obj.d -o CMakeFiles/cpp_20_compile_test_boost.dir/main.cpp.obj -c D:/Backup/Code/Qt/cpp_20_compile_test_boost/main.cpp
In file included from D:/Backup/Code/Qt/cpp_20_compile_test_boost/main.cpp:2:
In file included from D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio.hpp:23:
D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio/awaitable.hpp:37:12: error: no member named 'coroutine_handle' in namespace 'std'
using std::coroutine_handle;
      ~~~~~^
D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio/awaitable.hpp:38:12: error: no member named 'suspend_always' in namespace 'std'
using std::suspend_always;
      ~~~~~^
D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio/awaitable.hpp:96:15: error: no template named 'coroutine_handle' in namespace 'boost::asio::detail'
      detail::coroutine_handle<detail::awaitable_frame<U, Executor>> h)
      ~~~~~~~~^
In file included from D:/Backup/Code/Qt/cpp_20_compile_test_boost/main.cpp:2:
In file included from D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio.hpp:23:
In file included from D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio/awaitable.hpp:131:
D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio/impl/awaitable.hpp:256:3: error: no template named 'coroutine_handle'
  coroutine_handle<void> coro_ = nullptr;
  ^
D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio/impl/awaitable.hpp:113:26: error: no template named 'coroutine_handle'
      void await_suspend(coroutine_handle<void>) noexcept
                         ^
D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio/impl/awaitable.hpp:169:26: error: no template named 'coroutine_handle'
      void await_suspend(coroutine_handle<void>) noexcept
                         ^
D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio/impl/awaitable.hpp:205:26: error: no template named 'coroutine_handle'
      void await_suspend(coroutine_handle<void>) noexcept
                         ^
D:/Backup/Code/Qt/MyApp-vcpkg/installed/x64-windows/include\boost/asio/impl/awaitable.hpp:414:8: error: explicit specialization of undeclared template struct 'coroutine_traits'
struct coroutine_traits<boost::asio::awaitable<T, Executor>, Args...>
       ^               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 errors generated.
ninja: build stopped: subcommand failed.
17:07:47: The process "C:\Program Files\CMake\bin\cmake.exe" exited with code 1.
Error while building/deploying project cpp_20_compile_test_boost (kit: CLANG K3000 Qt 5.15.2 MSVC2019 64bit)
When executing step "Build"

有什么想法吗?

【问题讨论】:

  • 我会说,该错误非常明显且非特定于 Boost。请提取minimal reproducible example,包括(!)用于构建它的命令行。此外,请特别检查 Boost bugtracker 是否存在类似问题。作为这里的新用户,也可以使用tour 并阅读How to Ask
  • 它是特定于 boost 的,因为它在没有 clang 的情况下支持 clang,甚至完全支持协程。我能够通过在没有协程的情况下构建 asio 来解决这个问题。 github.com/chriskohlhoff/asio/issues/859

标签: c++ windows boost clang coroutine


【解决方案1】:

Boost asio 启用了 COROUTINE 支持,但没有完全支持它,请参阅 https://clang.llvm.org/cxx_status.html。我可以通过禁用 COROUTINE 自己编译 asio 来解决此问题,请参阅 https://github.com/chriskohlhoff/asio/issues/859#issuecomment-870320308

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多