Boost ASIO示例无法构建

3
我是一名有用的助手,可以为您翻译文本。
我正在尝试完成第一个asio tutorial。我使用的是Boost 1.60、CLion 1.2和Cygwin。
使用以下C++代码:
    //
    // timer.cpp
    // ~~~~~~~~~
    //
    // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
    //
    // Distributed under the Boost Software License, Version 1.0. (See accompanying
    // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    //

    #include <iostream>
    #include <boost/asio.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>

    int main()
    {
        boost::asio::io_service io;

        boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
        t.wait();

        std::cout << "Hello, world!" << std::endl;

        return 0;
    }

并且有这个 CMake 文件:

    cmake_minimum_required(VERSION 3.3)
    project(Test1)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    #add_compile_options(-D__USE_W32_SOCKETS -D_WIN32_WINNT=0x0501 -lboost_system -lws2_32)
    add_compile_options(-D__USE_W32_SOCKETS -D_WIN32_WINNT=0x0501)



    set(Boost_INCLUDE_DIR "D:\\Downloads\\boost_1_60_0\\boost_1_60_0")

    find_package(Boost)
    if (Boost_FOUND)
        include_directories(${Boost_INCLUDE_DIR})
    endif ()


    set(SOURCE_FILES main.cpp)
    add_executable(Test1 ${SOURCE_FILES})

我遇到了以下错误:

(强调)

error: '__MSABI_LONG' was not declared in this scope        BOOST_ASIO_NATIVE_ERROR(ERROR_BROKEN_PIPE)

这个错误在不同的宏中多次重复出现。
因此,使用这个answer,代码被改为: C++
    //
    // timer.cpp
    // ~~~~~~~~~
    //
    // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
    //
    // Distributed under the Boost Software License, Version 1.0. (See accompanying
    // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    //
    #define __MSABI_LONG(x)

    #include <iostream>
    #include <boost/asio.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>

    int main()
    {
        boost::asio::io_service io;

        boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
        t.wait();

        std::cout << "Hello, world!" << std::endl;

        return 0;
    }

CMake
    cmake_minimum_required(VERSION 3.3)
    project(Test1)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    add_compile_options(-D__USE_W32_SOCKETS -D_WIN32_WINNT=0x0501 -lboost_system -lws2_32)



    set(Boost_INCLUDE_DIR "D:\\Downloads\\boost_1_60_0\\boost_1_60_0")

    find_package(Boost)
    if (Boost_FOUND)
        include_directories(${Boost_INCLUDE_DIR})
    endif ()


    set(SOURCE_FILES main.cpp)
    add_executable(Test1 ${SOURCE_FILES})

我收到以下错误信息。
error: expected primary-expression before ',' token BOOST_ASIO_NATIVE_ERROR(EPIPE))

我也收到了很多这样的问题。

编辑1

如果我将代码更改为:

    //
    // timer.cpp
    // ~~~~~~~~~
    //
    // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
    //
    // Distributed under the Boost Software License, Version 1.0. (See accompanying
    // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    //
    #define __MSABI_LONG(x) x

    #include <iostream>
    #include <boost/asio.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>

    int main()
    {
        boost::asio::io_service io;

        boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
        t.wait();

        std::cout << "Hello, world!" << std::endl;

        return 0;
    }

错误输出样例:
    D:/Downloads/boost_1_60_0/boost_1_60_0/boost/asio/detail/impl/winsock_init.ipp:36:44: error: cannot convert 'long int*' to 'volatile int*' for argument '1' to 'int _InterlockedIncrement(volatile int*)'
       if (::InterlockedIncrement(&d.init_count_) == 1)
                                                ^
    D:/Downloads/boost_1_60_0/boost_1_60_0/boost/asio/detail/impl/winsock_init.ipp:40:45: error: cannot convert 'long int*' to 'volatile int*' for argument '1' to 'int _InterlockedExchange(volatile int*, int)'
         ::InterlockedExchange(&d.result_, result);
                                                 ^
    D:/Downloads/boost_1_60_0/boost_1_60_0/boost/asio/detail/impl/winsock_init.ipp: In static member function 'static void boost::asio::detail::winsock_init_base::manual_startup(boost::asio::detail::winsock_init_base::data&)':

       if (::InterlockedIncrement(&d.init_count_) == 1)
                                                ^
    D:/Downloads/boost_1_60_0/boost_1_60_0/boost/asio/detail/impl/winsock_init.ipp:48:40: error: cannot convert 'long int*' to 'volatile int*' for argument '1' to 'int _InterlockedExchange(volatile int*, int)'
         ::InterlockedExchange(&d.result_, 0);

编辑2

更改CMake代码以在编译器选项中指定正确的操作系统:

    cmake_minimum_required(VERSION 3.3)
    project(Test1)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    add_compile_options(-D__USE_W32_SOCKETS -D_WIN32_WINNT=0x0A00 -lboost_system -lws2_32)



    set(Boost_INCLUDE_DIR "D:\\AdrianMattocks\\Downloads\\boost_1_60_0\\boost_1_60_0")

    find_package(Boost)
    if (Boost_FOUND)
        include_directories(${Boost_INCLUDE_DIR})
    endif ()


    set(SOURCE_FILES main.cpp)
    add_executable(Test1 ${SOURCE_FILES})

我遇到了这个错误(由于SO大小限制,这只是一个示例,但这些错误会重复多次):

    D:/Malachi/Downloads/boost_1_60_0/boost_1_60_0/boost/asio/detail/impl/winsock_init.ipp: In static member function 'static void boost::asio::detail::winsock_init_base::startup(boost::asio::detail::winsock_init_base::data&, unsigned char, unsigned char)':
    D:/Malachi/Downloads/boost_1_60_0/boost_1_60_0/boost/asio/detail/impl/winsock_init.ipp:36:44: error: [Kno matching function for call to '_InterlockedIncrement(long int*)'
       if (::InterlockedIncrement(&d.init_count_) == 1)
    D:/Malachi/Downloads/boost_1_60_0/boost_1_60_0/boost/asio/detail/impl/winsock_init.ipp:36:44: error: invalid conversion from 'long int*' to 'volatile long unsigned int*' [-fpermissive]
       if (::InterlockedIncrement(&d.init_count_) == 1)

怎么修复这个问题?

1
再看一遍答案(它被错误地突出显示了)。__MSABI_LONG(x) x。我不知道这是否会使其正常工作。 - llonesmiz
谢谢指出。漏掉了“x”。:-| - Malachi
不定义任何东西,如__USE_W32_SOCKETS_WIN32_WINNT,直接使用默认值怎么样? - Ulrich Eckhardt
@UlrichEckhardt 我遇到了以下错误: 错误:#error 您必须将-D__USE_W32_SOCKETS添加到编译器选项中。 - Malachi
对于那些后来发现这个问题的人,我从未能够在CLion中构建它,所以我回到了Visual Studio 2015,它完美地工作了。也许是DLL编译的问题,也许是我的问题,我不知道,既然Visual Studio可以工作,那么就没有必要费力去解决它了。 - Malachi
在AppVeyor的二月更新中,我遇到了一个问题,涉及到msys2。不知何故,cmake现在认为它在cygwin上。我想知道以前是什么情况。我记得以前不必提供__USE_W32_SOCKETS,现在却要求提供。 - mlt
1个回答

2
我也遇到了同样的问题,当我尝试编译Boost ASIO时,成功消除了'__MSABI_LONG' was not declared in this scope和类似错误。我按照Boost库文档单独下载了Boost,并且修复错误的过程非常简单:
  • 删除编译器路径和包含选项中引用Boost库的任何内容。
  • 运行Cygwin安装程序并删除之前出现的Boost库。
  • 重新运行Cygwin安装程序并从安装程序中安装Boost库(您可以搜索Boost并选择性地安装您感兴趣的所有软件包)。
  • 重启IDE。
因此,Boost由Cygwin“处理”,再次编译时不会有任何错误。
希望这能帮到你!

1
你帮了我很多!:) 感谢启动了我的C++之旅。 - izzaki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接