Skip to content

Instantly share code, notes, and snippets.

@BewareMyPower
Last active December 19, 2023 13:28
Show Gist options
  • Select an option

  • Save BewareMyPower/fa81e6f8badb494ba195ef5273a4e4db to your computer and use it in GitHub Desktop.

Select an option

Save BewareMyPower/fa81e6f8badb494ba195ef5273a4e4db to your computer and use it in GitHub Desktop.
Verify pulsar-client-cpp vcpkg port.md

Example code and vcpkg.json

vcpkg.json:

{
  "name": "vcpkg-pulsar-demo",
  "version-string": "0.1.0",
  "builtin-baseline": "<commit-id>",
  "dependencies": [
    "pulsar-client-cpp"
  ]
}

helloworld.cpp:

#include <pulsar/Client.h>
using namespace pulsar;

int main(int argc, char *argv[]) {
  Client client{"pulsar://localhost:6650"};
  client.close();
  return 0;
}

Windows

x64-windows

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

set(VCPKG_TARGET_TRIPLET x64-windows-static)
set(CMAKE_TOOLCHAIN_FILE "<path-to-my-vcpkg-repo>\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake")
project(HelloWorld CXX)

set(CMAKE_CXX_STANDARD 11)

find_package(unofficial-pulsar CONFIG REQUIRED)

add_executable(HelloWorld helloworld.cpp)
target_link_libraries(HelloWorld PRIVATE unofficial::pulsar::pulsar)

Commands and outputs:

> cmake -B build -DCMAKE_BUILD_TYPE=Release
> cmake --build build
> .\build\Debug\HelloWorld.exe
2023-12-19 21:20:21.684 INFO  [6792] D:\github.com\BewareMyPower\vcpkg\buildtrees\pulsar-client-cpp\src\v3.4.2-5aee99fc11.clean\lib\ClientImpl:612 | Closing Pulsar client with 0 producers and 0 consumers
> cmake --build build --config Release
> .\build\Release\HelloWorld.exe
2023-12-19 21:21:47.906 INFO  [5320] D:\github.com\BewareMyPower\vcpkg\buildtrees\pulsar-client-cpp\src\v3.4.2-5aee99fc11.clean\lib\ClientImpl:612 | Closing Pulsar client with 0 producers and 0 consumers
> ls .\build\Debug\
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2023/12/11     21:26          61440 dl.dll
-a----        2023/12/19     21:20          70144 HelloWorld.exe
-a----        2023/12/19     21:20        1306624 HelloWorld.pdb
-a----        2023/12/11     21:20        7636992 libcrypto-3-x64.dll
-a----        2023/12/16     20:59        1682944 libcurl-d.dll
-a----        2023/12/11     21:27        7442944 libprotobufd.dll
-a----        2023/12/11     21:20        1093632 libssl-3-x64.dll
-a----        2023/12/19     21:16        8876544 pulsar.dll
-a----        2023/12/11     21:31         220160 snappy.dll
-a----        2023/12/11     21:23         208896 zlibd1.dll
-a----        2023/12/11     21:32        1718784 zstd.dll
> ls .\build\Release\
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2023/12/11     21:26          16384 dl.dll
-a----        2023/12/19     21:20          13312 HelloWorld.exe
-a----        2023/12/11     21:22        4484608 libcrypto-3-x64.dll
-a----        2023/12/16     20:59         574464 libcurl.dll
-a----        2023/12/11     21:29        2523136 libprotobuf.dll
-a----        2023/12/11     21:22         548352 libssl-3-x64.dll
-a----        2023/12/19     21:18        2467328 pulsar.dll
-a----        2023/12/11     21:31          76288 snappy.dll
-a----        2023/12/11     21:23          89088 zlib1.dll
-a----        2023/12/11     21:32         651776 zstd.dll

x64-windows-dynamic

> cmake -B build-release -DVCPKG_TARGET_TRIPLET=x64-windows-release
> cmake --build build --config Debug
> .\build\Debug\HelloWorld.exe
2023-12-19 21:27:05.972 INFO  [7904] D:\github.com\BewareMyPower\vcpkg\buildtrees\pulsar-client-cpp\src\v3.4.2-5aee99fc11.clean\lib\ClientImpl:612 | Closing Pulsar client with 0 producers and 0 consumers
> cmake --build build --config Release
> .\build\Release\HelloWorld.exe
2023-12-19 21:27:38.144 INFO  [11560] D:\github.com\BewareMyPower\vcpkg\buildtrees\pulsar-client-cpp\src\v3.4.2-5aee99fc11.clean\lib\ClientImpl:612 | Closing Pulsar client with 0 producers and 0 consumers

x64-windows-static

Add the following lines to CMakeLists.txt before find_package:

string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})

(I tried cmake_policy(SET CMP0091 NEW) and set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") but it does not work)

Commands and outputs:

> cmake -B build-static
> cmake --build build-static
> .\build-static\Debug\HelloWorld.exe
2023-12-17 01:14:24.933 INFO  [8528] D:\github.com\BewareMyPower\vcpkg\buildtrees\pulsar-client-cpp\src\v3.4.1-1f36e88ede.clean\lib\ClientImpl:612 | Closing Pulsar client with 0 producers and 0 consumers
> rm .\build-static\CMakeCache.txt
> cmake -B build-static -DCMAKE_BUILD_TYPE=Release
> cmake --build build-static --config Release
> cmake --build build-static --config Release
> .\build-static\Release\HelloWorld.exe
2023-12-17 01:15:58.589 INFO  [388] D:\github.com\BewareMyPower\vcpkg\buildtrees\pulsar-client-cpp\src\v3.4.1-1f36e88ede.clean\lib\ClientImpl:612 | Closing Pulsar client with 0 producers and 0 consumers

Linux or macOS

Default (statically linked):

cmake -B build
cmake --build build
./build/HelloWorld

Dynamically linked:

cmake -B build-dynamic -DVCPKG_TARGET_TRIPLET=<triplet>
cmake --build build-dynamic
./build/HelloWorld

The triplet could be one of the following according to the architecture and OS:

  • arm64-osx-dynamic
  • x64-osx-dynamic
  • x64-linux-dynamic

See the supported triplet in https://github.com/microsoft/vcpkg/tree/master/triplets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment