c++windowscmakevisual-studio-2022cpprest-sdk

Can't get a minimal cpprest example to compile under windows


I'm trying to get cpprestsdk to work under Windows. I have installed cpprestsdk with vcpkg:

cpprestsdk               2.10.19#3        C++11 JSON, REST, and OAuth library
cpprestsdk[brotli]                        Brotli compression support
cpprestsdk[compression]                   HTTP Compression support

I have the following CMakeLists.txt

cmake_minimum_required(VERSION 3.25)
project(weather-data-app LANGUAGES CXX)

# Require C++20
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Define minimum Windows version for WinHTTP & WebSocket support
add_compile_definitions(_WIN32_WINNT=0x0601)

# Point to vcpkg’s toolchain file on the command line:
find_package(cpprestsdk CONFIG REQUIRED)

add_executable(${PROJECT_NAME} src/main.cpp)

# Link the cpprestsdk imported target (brings HTTP, JSON, and WebSocket client libs)
target_link_libraries(${PROJECT_NAME}
    PRIVATE cpprestsdk::cpprest
)

and the following minimal example

// src/main.cpp

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

int main()
{
    // Instantiate types to verify headers are present
    web::http::client::http_client httpClient(U("http://localhost"));
    web::websockets::client::websocket_client wsClient;
    (void)httpClient;
    (void)wsClient;
    return 0;
}

I build the project with the following cmake command

cmake -S . -B build ^
  -DCMAKE_TOOLCHAIN_FILE="C:/vcpkg/scripts/buildsystems/vcpkg.cmake" ^
  -DVCPKG_TARGET_TRIPLET="x64-windows" ^
  -DCMAKE_BUILD_TYPE=Debug

and use the following command to compile:

cmake --build build --config Debug

And I get the following error:

MSBuild version 17.14.23+b0019275e for .NET Framework

  1>Checking Build System
  Building Custom Rule C:/Users/me/dev/weather-data-app/CMakeLists.txt
  main.cpp
C:\Users\me\dev\weather-data-app\src\main.cpp(9,22): error C3083: 'websockets': the symbol to the left of a '::' must be a type [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj
]
C:\Users\me\dev\weather-data-app\src\main.cpp(9,34): error C2039: 'client': is not a member of 'web' [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
      C:\vcpkg\installed\x64-windows\include\cpprest\http_client.h(75,11):
      see declaration of 'web'

C:\Users\me\dev\weather-data-app\src\main.cpp(9,34): error C2871: 'client': a namespace with this name does not exist [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(17,44): error C2653: 'methods': is not a class or namespace name [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(17,53): error C2065: 'GET': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(18,47): error C3536: 'response': cannot be used before it is initialized [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(21,9): error C2065: 'websocket_client': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(21,26): error C2146: syntax error: missing ';' before identifier 'wsClient' [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(21,26): error C2065: 'wsClient': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(22,9): error C2065: 'wsClient': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(26,9): error C2065: 'websocket_outgoing_message': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(26,36): error C2146: syntax error: missing ';' before identifier 'outMsg' [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(26,36): error C2065: 'outMsg': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(27,9): error C2065: 'outMsg': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(28,9): error C2065: 'wsClient': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(28,23): error C2065: 'outMsg': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(30,22): error C2065: 'wsClient': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(32,22): error C3536: 'inMsg': cannot be used before it is initialized [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]
C:\Users\me\dev\weather-data-app\src\main.cpp(34,9): error C2065: 'wsClient': undeclared identifier [C:\Users\me\dev\weather-data-app\build\weather-app-app.vcxproj]

This seems like the headers are not found? I'm new to developing under Windows, not sure what else might be going wrong here. The CMAKE_TOOLCHAIN_FILE business seems fine. Don't know.


Solution

  • Apparently, websockets functionality has been removed without any notice or documentation update. This cost me a week trying to build a websocket client, and failing miserably! Only when I went back to baseline `efb1e7436979a30c4d3e5ab2375fd8e2e461d541` in my vcpkg config, I managed to pull a version of `cpprest` that has a websocket functionality.

    It is hard to understand how did this happen!? The following vcpkg config, solved the issue. Have no idea how to use websockets from the latest baseline.

    {
      "name": "restaurant-data-app",
      "version": "0.0.1",
      "dependencies": [
        { "name": "cpprestsdk", "features": ["websockets"] }
      ],
      "builtin-baseline": "efb1e7436979a30c4d3e5ab2375fd8e2e461d541"
    }