c++open-telemetry

How to make vcpkg build a specific library version of opentelemetry-cpp?


I would like to use opentelemetry-cpp ABI version 2 in a Windows VS2022 project. To do this, i am trying to use vcpkg. By default the vcpkg build of opentlemetry-cpp creates ABI version 1 libraries. So, I created a triplets file x64-windows-otel2.cmake following some posts i've seen on the internet:

set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)

# Enable OpenTelemetry ABI v2
set(OPENTELEMETRY_ABI_VERSION 2)
set(OPENTELEMETRY_ABI_VERSION_NO 2)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOPENTELEMETRY_ABI_VERSION_NO=2")

and then tried to build it with

vcpkg install --classic opentelemetry-cpp[otlp-grpc] --triplet x64-windows-otel2

It seems this is not correct, as the build logs all still show -DOPENTELEMETRY_ABI_VERSION_NO=1

Does anyone know what is the correct triplet to make this work?


Solution

  • To build opentelemetry-cpp with ABI version 2 you should:

    1. Set WITH_ABI_VERSION_2 CMake option to ON
    2. Set WITH_ABI_VERSION_1 CMake option to OFF

    Note that you should set these options in the triplet file and not in your project. Also note that the correct way to pass CMake options is via VCPKG_CMAKE_CONFIGURE_OPTIONS:

    set(VCPKG_TARGET_ARCHITECTURE x64)
    set(VCPKG_CRT_LINKAGE dynamic)
    set(VCPKG_LIBRARY_LINKAGE dynamic)
    
    # Enable OpenTelemetry ABI v2
    if("${PORT}" STREQUAL "opentelemetry-cpp")
        list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DWITH_ABI_VERSION_1=OFF -DWITH_ABI_VERSION_2=ON)
    endif()
    

    PS: when using custom triplets, you may save some build time by setting host triplet equal to your custom triplet (provided that they are compatible).