我已阅读其他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,因为尝试重新安装提升导致&#34; libboost-all-dev已经是最新版本。&#34;解决此问题的最佳方法是什么?
答案 0 :(得分:0)
boost_asio
没有图书馆。 boost::asio
命名空间中的所有内容都在标头中实现。您可能需要链接到其他Boost库,例如boost_system
(我看到您已经拥有)。
您应该从项目中删除boost_asio
的链接配置。
boost_asio
可能需要的库是here。您可能还想阅读Which boost libraries are header-only?。