简体   繁体   中英

Boost Asio not linking in Eclipse Cdt?

I have read other Stackoverflow threads and none of the solutions seem to help.

I cannot link the Boost Asio library in Eclipse Cdt. However, I can link other libraries which makes me think that Eclipse is not the problem.

I am on Xubuntu 14.04. Boost was installed with: sudo apt-get install libboost-all-dev . This is /usr/include/ : usr包含目录

This is /usr/include/boost : 提高包括目录

This is Eclipse Cdt linker: Eclipse GCC链接器

This is the code I am trying to compile:

//============================================================================
// 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;
}

This is the error message:

   **** 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 is surely installed because trying to reinstall boost leads to "libboost-all-dev is already the newest version." What is the best way to resolve this problem?

There is no library for boost_asio . Everything in the boost::asio namespace is implemented in the headers. You may need to link to other Boost libraries, such as boost_system (which I see you already have).

You should remove the link configuration for boost_asio from your project.

The libraries that boost_asio may need are here . You may also want to read Which boost libraries are header-only? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM