【问题标题】:Boost Asio not linking in Eclipse Cdt?Boost Asio 没有在 Eclipse Cdt 中链接?
【发布时间】:2014-06-11 01:31:42
【问题描述】:

我已阅读其他 Stackoverflow 线程,但似乎没有任何解决方案有帮助。

我无法在 Eclipse Cdt 中链接 Boost Asio 库。但是,我可以链接其他库,这让我认为 Eclipse 不是问题。

我在 Xubuntu 14.04 上。 Boost 安装时使用:sudo apt-get install libboost-all-dev。 这是/usr/include/

这是/usr/include/boost

这是 Eclipse Cdt 链接器:

这是我要编译的代码:

//============================================================================
// Name        : BoostMain.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include<vector>
#include <ostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

int do_get(std::string &host_, std::string &port_, std::string url_path,
        std::ostream &out_, std::vector<std::string> &headers,
        unsigned int timeout) {

    try {
        using namespace boost::asio::ip;
        tcp::iostream request_stream;
        if (timeout > 0) {
            request_stream.expires_from_now(
                    boost::posix_time::milliseconds(timeout));
        }
        request_stream.connect(host_, port_);
        if (!request_stream) {
            return -1;
        }
        request_stream << "GET " << url_path << " HTTP/1.0\r\n";
        request_stream << "Host: " << host_ << "\r\n";
        request_stream << "Accept: */*\r\n";
        request_stream << "Cache-Control: no-cache\r\n";
        request_stream << "Connection: close\r\n\r\n";
        request_stream.flush();
        std::string line1;
        std::getline(request_stream, line1);
        if (!request_stream) {
            return -2;
        }
        std::stringstream response_stream(line1);
        std::string http_version;
        response_stream >> http_version;
        unsigned int status_code;
        response_stream >> status_code;
        std::string status_message;
        std::getline(response_stream, status_message);
        if (!response_stream || http_version.substr(0, 5) != "HTTP/") {
            return -1;
        }
        if (status_code != 200) {
            return (int) status_code;
        }
        std::string header;
        while (std::getline(request_stream, header) && header != "\r")
            headers.push_back(header);
        out_ << request_stream.rdbuf();
        return status_code;
    } catch (std::exception &e) {
        std::cout << e.what() << std::endl;
        return -3;
    }

    return -1;

}

int main() {
    std::cout << "Hello World" << std::endl;
//  std::string host, port;
//  std::vector<std::string> headers;
//  std::string url = "https://www.google.com/";
//  int output = do_get(host, port, url, std::cout, headers, 1000);
//  std::cout << "Out:" << output << std::endl << std::endl;
//  std::cout << host << std::endl << port << std::endl << std::endl;
    //std::cout << stream;
    return 0;
}

这是错误信息:

   **** Build of configuration Debug for project BoostMain ****

    make all 
    Building file: ../src/BoostMain.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/BoostMain.d" -MT"src/BoostMain.d" -o "src/BoostMain.o" "../src/BoostMain.cpp"
    Finished building: ../src/BoostMain.cpp

    Building target: BoostMain
    Invoking: GCC C++ Linker
    g++ -L/usr/include -o "BoostMain"  ./src/BoostMain.o   -lboost_system -lboost_thread -lboost_asio
    /usr/bin/ld: cannot find -lboost_asio
    collect2: error: ld returned 1 exit status
    make: *** [BoostMain] Error 1

    **** Build Finished ****

Boost 肯定已安装,因为尝试重新安装 boost 会导致“libboost-all-dev 已经是最新版本”。解决此问题的最佳方法是什么?

【问题讨论】:

    标签: c++ boost boost-asio linker-errors


    【解决方案1】:

    boost_asio 没有库。 boost::asio 命名空间中的所有内容都在标头中实现。您可能需要链接到其他 Boost 库,例如 boost_system(我看到您已经拥有)。

    您应该从项目中删除boost_asio 的链接配置。

    boost_asio 可能需要的库是here。您可能还想阅读Which boost libraries are header-only?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-03
      • 2011-06-24
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 2013-04-13
      相关资源
      最近更新 更多