CentOS 7에서 rest sdk 빌드 하기
설치 및 셋팅 2016. 9. 21. 10:40
사전 준비
boost 1.54 이상 버전의 라이브러리가 필요하다.
여기서는 1.61 버전을 사용한다.
boost 빌드
1 2 3 4 5 6 7 | $ wget https: //sourceforge .net /projects/boost/files/boost/1 .61.0 /boost_1_61_0 . tar .bz2 /download # downlaod 이름의 파일로 받아진다. 그러니 이름을 변경해 두자. $ mv download boost_1_61_0. tar .bz2 $ tar -xvf boost_1_61_0. tar .bz2 $ cd boost_1_61_0 $ . /bootstrap .sh $ . /b2 install |
본론
rest sdk 빌드
1 2 3 4 5 6 | $ git clone https: //git .codeplex.com /casablanca $ cd casablanca /Release $ mkdir build.release $ cd build.release $ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX= /usr/local .. $ make & make install |
예제 코드 실행해 보기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cpprest/http_client.h> | |
#include <cpprest/filestream.h> | |
using namespace utility; // Common utilities like string conversions | |
using namespace web; // Common features like URIs. | |
using namespace web::http; // Common HTTP functionality | |
using namespace web::http::client; // HTTP client features | |
using namespace concurrency::streams; // Asynchronous streams | |
int main(int argc, char* argv[]) | |
{ | |
auto fileStream = std::make_shared<ostream>(); | |
// Open stream to output file. | |
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile) | |
{ | |
*fileStream = outFile; | |
// Create http_client to send the request. | |
http_client client(U("http://www.bing.com/")); | |
// Build request URI and start the request. | |
uri_builder builder(U("/search")); | |
builder.append_query(U("q"), U("cpprestsdk github")); | |
return client.request(methods::GET, builder.to_string()); | |
}) | |
// Handle response headers arriving. | |
.then([=](http_response response) | |
{ | |
printf("Received response status code:%u\n", response.status_code()); | |
// Write response body into the file. | |
return response.body().read_to_end(fileStream->streambuf()); | |
}) | |
// Close the file stream. | |
.then([=](size_t) | |
{ | |
return fileStream->close(); | |
}); | |
// Wait for all the outstanding I/O to complete and handle any exceptions | |
try | |
{ | |
requestTask.wait(); | |
} | |
catch (const std::exception &e) | |
{ | |
printf("Error exception:%s\n", e.what()); | |
} | |
return 0; | |
} |
1 2 3 | $ g++ -std=c++11 hellorest.cpp -o hellorest -lboost_system -lcrypto -lssl -lcpprest $ . /hellorest Received response status code:200 |