Casablancaのインストール
皆さんこんにちは
お元気ですか。私は燃え尽きました。
今日はCasablancaのインストール記事を記載します。
Casablancaとは
マイクロソフトのC++ REST SDKで、クラウドベースのクライアント・サーバーの
APIデザインだそうな。
C++ REST SDK (codename "Casablanca") - Home
内容にはHTTP Client/Server, JSON, URL ,Asynchronus Stream,WebSockets,OAuthが搭載されているそうな。
因みにマイクロソフトだからといって、WindowsだけでなくLinuxやMacでも使えます。
因みに僕はMacOSです。
Install
必要なもの
brew install cmake git openssl boost
必要なものがインストールされているのを確認してから
git clone https://git01.codeplex.com/casablanca cd casablanca mkdir build.release cd build.release cmake ../Release -DCMAKE_BUILD_TYPE=Release make -j 4
以下のサンプルコードを動かすにはどうすればよいか考えてみましょう。
ぶっちゃけmake installと呼ばれる素晴らしいコマンドはないので自力で。
#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("Casablanca CodePlex")); 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; }
問題点が2つあります。
①cpprestのheaderを使うこと
うん、これは適当に放り込みました。入れる場所の参考にしてください。
要はincludeできればなんでもいいのですが・・・・
cp -r $(PATH)/casablanca/Release/include/ /usr/local/include/
②リンク、コンパイルです。
以下の$(PATH)はcasablancaに至るまでのパスです。その中にある.dylibを全てリンクさせます。
また -lを使い、中で利用されているboost_systemを動作させます。
時折、オブジェクトが足りませんエラーが出るので辛抱強く頑張りましょう
g++ test.o $(PATH)/casablanca/build.release/Binaries/*.dylib -o main -I /usr/local/include/ /usr/local/lib/libboost_thread-mt.a -lboost_system /usr/local/lib/libboost_chrono-mt.a
コンパイルしてうまくいくと以下の様なメッセージが出てきます。
Received response status code:200
細かい使い方は次回以降に回します。
何かいい設定はないですか・・・・・・
make installの実装を希望したいところ・・・・