Qt_20 - 8BitsCoding/RobotMentor GitHub Wiki
이제 두 가지 버전으로 프로젝트를 만든다.
Visual Studio 2017, Qt Creator 버전으로...
Widget Application으로 생성하고, base class 역시 QWidget으로 생성한다.
#include <boost\asio.hpp>
를 써야하기에 프로젝트를 수정해야한다.비교적 qt에 비해서 수월하다.
- 추가 포함 디렉터리
C:\boost_#_##\
추가- 추가 라이브러리 디렉터리
C:\boost_#_##\stage\lib\
추가
// AsioClient.h
#pragma once
#include "boost/asio.hpp"
#include <QObject>
#include <qstring.h>
#include <memory>
#include <thread>
class AsioClient
{
public:
AsioClient();
~AsioClient();
void Get(const QString& url, const QString& path);
private:
boost::asio::io_service ioservice;
std::shared_ptr<boost::asio::io_service::work> work;
std::thread worker;
};
// AsioClient.cpp
#include "AsioClient.h"
AsioClient::AsioClient() :
work(new boost::asio::io_service::work(ioservice))
{
worker = std::thread([&]() {
ioservice.run();
});
}
AsioClient::~AsioClient()
{
ioservice.stop();
worker.join(); // thread의 종료를 기다린다.
work.reset(); // shared_ptr 메모리 해제
}
#include <boost\asio.hpp>
를 해서 asio.hpp를 써야하는데 ... pro파일을 조금 수정해야 한다.
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += C:\boost_1_71_0\
SOURCES += \
asioclient.cpp \
main.cpp \
widget.cpp
HEADERS += \
asioclient.h \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
줄이 긴데 추가된 부분은
INCLUDEPATH += C:\boost_1_71_0\
여기까지만 추가하면 인클루드는 되는데 빌드가 안된다.
빌드를 위해서 라이브러리를 추가한다.(프로젝트 우클릭 -> Add Library)
외부 라이브러리기에 External library 추가
system lib를 static으로 추가한다.
이렇게 추가하면 .pro파일이 자동으로 수정된다.!
// asioclient.h
#ifndef ASIOCLIENT_H
#define ASIOCLIENT_H
#include <boost\asio.hpp>
#include <QObject>
#include <QString>
#include <memory>
#include <thread>
class AsioClient
{
public:
AsioClient();
virtual ~AsioClient();
void Get(const QString& url, const QString& path);
private:
boost::asio::io_service ioservice;
std::shared_ptr<boost::asio::io_service::work> work;
std::thread worker;
};
#endif // ASIOCLIENT_H
// asioclient.cpp
#include "asioclient.h"
AsioClient::AsioClient() :
work(new boost::asio::io_service::work(ioservice))
{
worker = std::thread([&](){
ioservice.run();
});
}
AsioClient::~AsioClient(){
ioservice.stop();
worker.join();
work.reset();
}