1

I'm trying to create an HTTP client for a rest API using the cpprest-sdk library: https://github.com/Microsoft/cpprestsdk

I'm using a MacBook Pro with the latest version of MacOs (10.14.2).

I use brew as a package manager and have installed boost with:
brew install boost

Versions of dependencies:
clang: Apple LLVM version 10.0.0 (clang-1000.11.45.5)
cmake: 3.13.2
boost: stable 1.68.0 (bottled), HEAD

First I tried to follow this:
https://github.com/Microsoft/cpprestsdk/wiki/How-to-build-for-Mac-OS-X

So I also installed cpprestsdk with boost:
boost install cpprestsdk

Then I used the code from the tutorial in one main.cpp:

#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;
}

And I created a CMake build file CMakeLists.txt as they describe in the README of cpprestsdk:

cmake_minimum_required(VERSION 3.13)
project(cpprest-example)

set(CMAKE_CXX_STANDARD 14)

find_package(cpprestsdk REQUIRED)

add_executable(cpprest-example main.cpp)
target_link_libraries(cpprest-example PRIVATE cpprestsdk::cpprest)

First I had a problem with CMake not fining openssl, so I had to add two env parameters like described here:
CMake not able to find OpenSSL library

Then it compiles but then fails during linking, with errors:

Scanning dependencies of target cpprest-example
[ 50%] Building CXX object CMakeFiles/cpprest-example.dir/main.cpp.o
[100%] Linking CXX executable cpprest-example
Undefined symbols for architecture x86_64:
  "boost::this_thread::interruption_point()", referenced from:
      boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in main.cpp.o
      boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, boost::detail::real_platform_timepoint const&) in main.cpp.o
  "boost::chrono::steady_clock::now()", referenced from:
      bool boost::condition_variable::wait_for<long long, boost::ratio<1l, 1000l>, pplx::details::event_impl::wait(unsigned int)::'lambda0'()>(boost::unique_lock<boost::mutex>&, boost::chrono::duration<long long, boost::ratio<1l, 1000l> > const&, pplx::details::event_impl::wait(unsigned int)::'lambda0'()) in main.cpp.o
      bool boost::condition_variable::wait_until<boost::chrono::steady_clock, boost::chrono::duration<long long, boost::ratio<1l, 1000000000l> >, pplx::details::event_impl::wait(unsigned int)::'lambda0'()>(boost::unique_lock<boost::mutex>&, boost::chrono::time_point<boost::chrono::steady_clock, boost::chrono::duration<long long, boost::ratio<1l, 1000000000l> > > const&, pplx::details::event_impl::wait(unsigned int)::'lambda0'()) in main.cpp.o
  "boost::detail::get_current_thread_data()", referenced from:
      boost::detail::interruption_checker::interruption_checker(_opaque_pthread_mutex_t*, _opaque_pthread_cond_t*) in main.cpp.o
  "boost::system::detail::system_category_instance", referenced from:
      boost::system::system_category() in main.cpp.o
  "boost::system::detail::generic_category_instance", referenced from:
      boost::system::generic_category() in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cpprest-example] Error 1
make[1]: *** [CMakeFiles/cpprest-example.dir/all] Error 2
make: *** [all] Error 2

Strange thing is that if I build the cpprestsdk codebase with CMake it works fine and one of the samples is the same code: https://github.com/Microsoft/cpprestsdk/tree/master/Release/samples/BingRequest

That sample project has then been built successfully and runs fine.

I also tried uninstalling cpprestsdk from brew and did a CMake build of the cloned repository and then "make -j4 && make install". That installs cpprestsdk headers and libraries locally but in the end, results in the same errors.

It looks like it's not finding the boost libraries and I have tried using find_package(BOOST 1.68.0) and similar things but it fails with the same errors.

Can anyone see what I'm doing wrong?

Robert Sjödahl
  • 734
  • 5
  • 19
  • 3
    For the dependencies of CPPRestSDK have a look at `cpprestsdk-config.in.cmake` in the cmake folder of the package. All dependencies are listed in this file. It looks like you need to add `find_package(Boost COMPONENTS random system thread filesystem chrono atomic date_time regex)` before the `add_executable` call and `${Boost_LIBRARIES}` to your `target_link_libraries` call. – vre Dec 17 '18 at 19:27
  • Thanks, that fixed the problem. – Robert Sjödahl Dec 17 '18 at 20:41

0 Answers0