프로그래밍/C++

centos7 c++ 에서 http_client.h install 하는 방법 (rest sdk build)

소행성왕자 2021. 11. 2. 16:01

c++ 에서 아래와 같은 코드를 실행하려고 할때 rest sdk 있어야 함

 

 

사전준비

boost 1.54 이상 버전 필요

여기서는 1.61 사용

boost build - 오래걸린다.

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
Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2
Detecting Python version... 2.7
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... not found.
Generating Boost.Build configuration in project-config.jam...

Bootstrapping is done. To build, run:

    ./b2
    
To adjust configuration, edit 'project-config.jam'.
Further information:

   - Command line help:
     ./b2 --help
     
   - Getting started guide: 
     http://www.boost.org/more/getting_started/unix-variants.html
     
   - Boost.Build documentation:
     http://www.boost.org/build/doc/html/index.html
$ ./b2 install

rest sdk build

$ git clone https://github.com/microsoft/cpprestsdk
$ cd casablanca/Release

# 오류 방지하기 위해 
$ git submodule update --init

$ mkdir build.release

$ cd build.release

$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..

-- Setting gcc options
-- websocketpp not found, using the embedded version
-- Added test library httpclient_test
-- Added test library httplistener_test
-- Added test library json_test
-- Added test library pplx_test
-- Added test library streams_test
-- Added test library uri_test
-- Added test library utils_test
-- Added test library websocketsclient_test
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/cpprestsdk/build.release
[root@a82f5c258527 build.release]# ls -al
total 68
drwxr-xr-x  4 root root   131 Nov 30 05:35 .
drwxr-xr-x 10 root root  4096 Nov 30 05:03 ..
-rw-r--r--  1 root root 28834 Nov 30 05:35 CMakeCache.txt
drwxr-xr-x  5 root root  4096 Nov 30 05:35 CMakeFiles
-rw-r--r--  1 root root   296 Nov 30 05:35 CTestTestfile.cmake
-rw-r--r--  1 root root 17848 Nov 30 05:35 Makefile
drwxr-xr-x  7 root root   149 Nov 30 05:35 Release
-rw-r--r--  1 root root  1819 Nov 30 05:35 cmake_install.cmake

 

그런데 cmake 버전이 낮다고 나온다면

지우고 다시 설치 하자

CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
  CMake 3.9 or higher is required.  You are running version 2.8.12.2


-- Configuring incomplete, errors occurred!
# 기존 cmake 삭제
$ yum remove cmake

# 소스파일 받아서 컴파일 하자
$ wget https://cmake.org/files/v3.22/cmake-3.22.0.tar.gz
$ tar -zxvf cmake-3.22.0.tar.gz
$ cd cmake-3.22.0

# /usr/local 경로의 bin 폴더에 설치가 됩니다.
$ ./bootstrap --prefix=/usr/local
$ make
$ make install

# 설치확인
cmake --version

 

오류 발생

CMake Error at cmake/cpprest_find_websocketpp.cmake:14 (message):
  -- websocketpp not found and embedded version not present; try `git
  submodule update --init` and run CMake again
Call Stack (most recent call first):
  src/CMakeLists.txt:68 (cpprest_find_websocketpp)

시도

$ git submodule update --init
You need to run this command from the toplevel of the working tree

상위 경로에서 다시 시도

$ pwd 
/usr/local/src/cpprestsdk

$ cd /usr/local/src/cpprestsdk
$ git submodule update --init

$ cd Release/
$ cd build.release/

$ pwd 
/usr/local/src/cpprestsdk/Release/build.release
$ make 


[root@localhost build.release]# make
[  1%] Building CXX object src/CMakeFiles/cpprest.dir/http/client/http_client.cpp.o
[  1%] Building CXX object src/CMakeFiles/cpprest.dir/http/client/http_client_msg.cpp.o
[  2%] Building CXX object src/CMakeFiles/cpprest.dir/http/common/http_compression.cpp.o
/usr/local/src/cpprestsdk/Release/src/http/common/http_compression.cpp:149:24: error: missing initializer for member ‘z_stream_s::next_in’ [-Werror=missing-field-initializers]
     z_stream m_stream {};
...
...

cc1plus: all warnings being treated as errors
make[2]: *** [src/CMakeFiles/cpprest.dir/http/common/http_compression.cpp.o] 오류 1
make[1]: *** [src/CMakeFiles/cpprest.dir/all] 오류 2
make: *** [all] 오류 2

에러발생

시도

$ yum install zlib-devel libicu-devel openssl-devel

그래도 에러 발생
vi casablanca/Release/CMakeLists.txt 
옵션이 포함되어 있는지 확인
WERROR 있다면 -DWERROR=OFF 추가

모두 지우고 다시 컴파일
$ pwd 
build.release

$ rm -rf ./

$ cmake -DCMAKE_BUILD_TYPE=Release -DWERROR=OFF -DCMAKE_INSTALL_PREFIX=/usr/local ..

$ make 

$ make install

예제코드 

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>

using namespace utility;                    
using namespace web;                        
using namespace web::http;                  
using namespace web::http::client;          
using namespace concurrency::streams;       

int main(int argc, char* argv[])
{
    auto fileStream = std::make_shared<ostream>();

    
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
    {
        *fileStream = outFile;

    
        http_client client(U("http://www.bing.com/"));

    
        uri_builder builder(U("/search"));
        builder.append_query(U("q"), U("cpprestsdk github"));
        return client.request(methods::GET, builder.to_string());
    })

    
    .then([=](http_response response)
    {
        printf("Received response status code:%u\n", response.status_code());

    
        return response.body().read_to_end(fileStream->streambuf());
    })

    
    .then([=](size_t)
    {
        return fileStream->close();
    });

    
    try
    {
        requestTask.wait();
    }
    catch (const std::exception &e)
    {
        printf("Error exception:%s\n", e.what());
    }

    return 0;
}

컴파일 및 실행

$ g++ -std=c++11 hellorest.cpp -o hellorest -lboost_system -lcrypto -lssl -lcpprest
$ ./hellorest
Received response status code:200

실행시 오류 발생

error while loading shared libraries: libboost_system.so.1.61.0
./hellorest: error while loading shared libraries: libcpprest.so.2.10: cannot open shared object file: No such file or directory

해결

시도해 본것 

g++ -std=c++11 hellorest.cpp -o hellorest -L/usr/local/lib -lboost_system -lcrypto -lssl -L/usr/local/lib64 -lcpprest

g++ -std=c++11 hellorest.cpp -o hellorest -lboost_system -lcrypto -lssl -lcpprest


libboost_system.so.1.61.0 파일은 /usr/local/lib 에 위치
libcpprest.so.2.10 파일은 /usr/local/lib64 에 위치

/usr/local/lib64/libcpprest.so.2.10 파일을 /usr/local/lib 에 복사한후 다시 컴파일

g++ -std=c++11 hellorest.cpp -o hellorest -lboost_system -lcrypto -lssl -lcpprest

이슈 및 해결

[@localhost cpp]# ./ocrrest
./ocrrest: error while loading shared libraries: libboost_system.so.1.61.0: cannot open shared object file: No such file or directory

export LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH=/usr/local/lib

참고

https://yakolla.tistory.com/130

 

CentOS 7에서 rest sdk 빌드 하기

사전 준비 boost 1.54 이상 버전의 라이브러리가 필요하다. 여기서는 1.61 버전을 사용한다. boost 빌드 $ wget https://sourceforge.net/projects/boost/files/boost/1.61.0/boost_1_61_0.tar.bz2/download # do..

yakolla.tistory.com

https://gist.github.com/coin8086/488580e650a552bdfbd0f858c193a26e

 

Compile CppREST on CentOS 7

Compile CppREST on CentOS 7. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com