我有一个C ++程序,可以在Linux上编译好。但是当我尝试在Windows上编译它时,我得到了:fatal error: boost/asio.hpp: No such file or directory
我不明白这是怎么可能的,因为我有更多的提升包括(<boost/thread.hpp>
和<boost/locale.hpp>
),他们工作正常。我还检查了我的提升库,thread.hpp
,locale.hpp
和asio.hpp
都在那里。
如果这很重要,在Linux上我使用以下命令安装了boost(它可以在那里工作):
sudo apt-get install libboost-all-dev
sudo apt-get install aptitude
aptitude search boost
这是我的代码:
Client.cpp:
#include "../include/ConnectionHandler.h"
#include <stdlib.h>
#include <boost/thread.hpp>
#include <boost/locale.hpp>
//A method used to read messages from the console (stdin) and send them to the server using a ConnectionHandler
void sendMessage(ConnectionHandler *connectionHandler){ // handles stdin
if (connectionHandler->isConnected()){
while (1){
const short bufsize = 1024;
char buf[bufsize];
std::cin.getline(buf, bufsize);
std::string line(buf);
if (!connectionHandler->sendLine(line)) { break; }
if (line=="QUIT"){
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
if (!connectionHandler->isConnected()) { break; }
}
}
}
}
//A method used to read messages from the socket (using the ConnectionHandler)
void recieveMessage(ConnectionHandler *connectionHandler){ // handles socket
while (1){
std::string answer;
if (!connectionHandler->getLine(answer)) {
std::cout << "Disconnected.\nExiting...\n" << std::endl;
break;
}
int len=answer.length();
answer.resize(len-1);
std::cout << answer << std::endl << std::endl; // double line drops so it will be easier to notice between messages
if (answer == "SYSMSG QUIT ACCEPTED") {
std::cout << "Disconnected.\nExiting...\n" << std::endl;
break;
}
}
(*connectionHandler).close();
}
int main (int argc, char *argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " host port" << std::endl << std::endl;
return -1;
}
std::string host = argv[1];
unsigned short port_ = atoi(argv[2]);
ConnectionHandler *connectionHandler_= new ConnectionHandler(host,port_);
if (!(connectionHandler_)->connect()) {
std::cerr << "Cannot connect to " << host << ":" << port_ << std::endl;
delete connectionHandler_;
return 1;
}
boost::thread sender(&sendMessage,connectionHandler_); // a different thread to handle stdin
boost::thread reciever(&recieveMessage,connectionHandler_); // a different thread to handle the socket
reciever.join();
sender.join();
delete connectionHandler_;
}
ConnectionHandler.h:
#ifndef CONNECTION_HANDLER__
#define CONNECTION_HANDLER__
#include <string>
#include <iostream>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
class ConnectionHandler {
private:
const std::string host_;
const short port_;
boost::asio::io_service io_service_; // Provides core I/O functionality
tcp::socket socket_;
bool isConnected_;
public:
ConnectionHandler(std::string host, short port);
virtual ~ConnectionHandler();
bool connect();
bool getBytes(char bytes[], unsigned int bytesToRead);
bool sendBytes(const char bytes[], int bytesToWrite);
bool getLine(std::string& line);
bool sendLine(std::string& line);
bool getFrameAscii(std::string& frame, char delimiter);
bool sendFrameAscii(const std::string& frame, char delimiter);
void close();
bool isConnected();
};
#endif
ConnectionHandler.cpp:
#include "../include/ConnectionHandler.h"
using boost::asio::ip::tcp;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::string;
ConnectionHandler::ConnectionHandler(string host, short port): host_(host), port_(port), io_service_(), socket_(io_service_), isConnected_(false){}
ConnectionHandler::~ConnectionHandler() { close(); }
bool ConnectionHandler::connect() {
std::cout << "Starting connect to " << host_ << ":" << port_ << std::endl;
try {
tcp::endpoint endpoint(boost::asio::ip::address::from_string(host_), port_); // the server endpoint
boost::system::error_code error;
socket_.connect(endpoint, error);
if (error) { throw boost::system::system_error(error); }
else{
std::cout << "Connection to " << host_ << ":" << port_ << " Successfully made!" << std::endl;
isConnected_ = true;
}
}
catch (std::exception& e) {
std::cerr << "Connection failed (Error: " << e.what() << ')' << std::endl;
return false;
}
return true;
}
bool ConnectionHandler::getBytes(char bytes[], unsigned int bytesToRead) {
size_t tmp = 0;
boost::system::error_code error;
try {
while (!error && bytesToRead > tmp ) {
tmp += socket_.read_some(boost::asio::buffer(bytes+tmp, bytesToRead-tmp), error);
}
if(error) { throw boost::system::system_error(error); }
}
catch (std::exception& e) {
std::cerr << "recv failed (Error: " << e.what() << ')' << std::endl;
return false;
}
return true;
}
bool ConnectionHandler::sendBytes(const char bytes[], int bytesToWrite) {
int tmp = 0;
boost::system::error_code error;
try {
while (!error && bytesToWrite > tmp ) {
tmp += socket_.write_some(boost::asio::buffer(bytes + tmp, bytesToWrite - tmp), error);
}
if(error) { throw boost::system::system_error(error); }
}
catch (std::exception& e) {
std::cerr << "recv failed (Error: " << e.what() << ')' << std::endl;
return false;
}
return true;
}
bool ConnectionHandler::getLine(std::string& line) { return getFrameAscii(line, '\n'); }
bool ConnectionHandler::sendLine(std::string& line) { return sendFrameAscii(line, '\n'); }
bool ConnectionHandler::getFrameAscii(std::string& frame, char delimiter) {
char ch;
do{
if(!getBytes(&ch, 1)) { return false; }
frame.append(1, ch);
}while (delimiter != ch);
return true;
}
bool ConnectionHandler::sendFrameAscii(const std::string& frame, char delimiter) {
bool result=sendBytes(frame.c_str(),frame.length());
if(!result) return false;
return sendBytes(&delimiter,1);
}
// Close down the connection properly.
void ConnectionHandler::close() {
try{
socket_.close();
isConnected_ = false;
}
catch (...) { std::cout << "closing failed: connection already closed" << std::endl; }
}
bool ConnectionHandler::isConnected(){ return isConnected_; }
makefile:
# All Targets
all: client
# Tool invocations
# Executable "client" depends on the files Client.o, ConnectionHandler.o.
client: bin/Client.o bin/ConnectionHandler.o
@echo 'Building target: client'
@echo 'Invoking: C++ Linker'
g++ -o bin/client bin/Client.o bin/ConnectionHandler.o -lboost_system -lboost_locale -lboost_thread
@echo 'Finished building target: client'
@echo ' '
# Depends on the source and header files
bin/Client.o: src/Client.cpp
g++ -g -Wall -c -Linclude -I/usr/local/boost/1.57.0/include/boost -o bin/Client.o src/Client.cpp
# Depends on the source and header files
bin/ConnectionHandler.o: src/ConnectionHandler.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/ConnectionHandler.o src/ConnectionHandler.cpp
.PHONY: clean
#Clean the build directory
clean:
rm -f bin/*
如何解决此错误?