Boost ASIO示例不构建

时间:2016-01-19 18:39:15

标签: c++ boost boost-asio

我正试图做拳头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 个答案:

答案 0 :(得分:1)

我也遇到了同样的问题,并且在尝试编译 Boost ASIO 时设法摆脱了'__MSABI_LONG' was not declared in this scope和类似的错误。我按照Boost库文档单独下载了Boost。修复错误的过程非常简单:

  • 删除编译器路径中对Boost库的任何引用,并包含选项。
  • 运行Cygwin installer并删除之前出现的任何Boost库。
  • 重新运行Cygwin安装程序并从安装程序安装Boost库(您可以搜索Boost并有选择地安装您感兴趣的所有软件包)。
  • 重新启动IDE。

因此,Boost由Cygwin“处理”,再次编译时我没有任何错误。

希望这有帮助!