@@ -151,21 +151,55 @@ If building and installing Protobuf and gRPC manually with cmake the
151151 $
152152 ```
153153
154- ### Incorporating into an existing CMake Project
154+ ### Incorporating into an external CMake Project
155155
156- To use the library from a CMake project, you can locate it directly with
157- ` find_package ` and use the imported targets from generated package
158- configurations. As of now, this will import targets for both API and SDK. In
159- future, there may be separate packages for API and SDK which can be installed
160- and imported separately according to need.
156+ There are two approaches to incoporate ` opentelemetry-cpp ` into
157+ an external CMake project:
161158
162- ``` cmake
163- # CMakeLists.txt
164- find_package(opentelemetry-cpp CONFIG REQUIRED)
165- ...
166- target_include_directories(foo PRIVATE ${OPENTELEMETRY_CPP_INCLUDE_DIRS})
167- target_link_libraries(foo PRIVATE ${OPENTELEMETRY_CPP_LIBRARIES})
168- ```
159+ 1 . Build and install ` opentelemetry-cpp ` then use ` find_package `
160+ to import its targets
161+
162+ ``` cmake
163+ # Find all installed components and link all imported targets
164+ find_package(opentelemetry-cpp CONFIG REQUIRED)
165+ ...
166+ target_include_directories(foo PRIVATE ${OPENTELEMETRY_CPP_INCLUDE_DIRS})
167+ target_link_libraries(foo PRIVATE ${OPENTELEMETRY_CPP_LIBRARIES})
168+ ```
169+
170+ ``` cmake
171+ # Find a specific component and link its imported target(s)
172+ find_package(opentelemetry-cpp CONFIG REQUIRED COMPONENTS api)
173+ ...
174+ target_link_libraries(foo PRIVATE opentelemetry-cpp::api)
175+ ```
176+
177+ 2 . Use CMake's [ FetchContent] ( https://cmake.org/cmake/help/latest/module/FetchContent.html )
178+ module to fetch and build ` opentelemetry-cpp ` then make its targets available
179+
180+ ``` cmake
181+ # Fetch from an existing clone and build
182+ include(FetchContent)
183+ FetchContent_Declare(opentelemetry-cpp SOURCE_DIR "<path/to/opentelemetry-cpp>")
184+ FetchContent_MakeAvailable(opentelemetry-cpp)
185+ ...
186+ target_link_libraries(foo PRIVATE opentelemetry-cpp::api)
187+ ```
188+
189+ ``` cmake
190+ # Clone and build opentelemetry-cpp from a git tag
191+ include(FetchContent)
192+ FetchContent_Declare(
193+ opentelemetry-cpp
194+ GIT_REPOSITORY https://github.com/open-telemetry/opentelemetry-cpp.git
195+ GIT_TAG v1.20.0)
196+ FetchContent_MakeAvailable(opentelemetry-cpp)
197+ ...
198+ target_link_libraries(foo PRIVATE opentelemetry-cpp::api)
199+ ```
200+
201+ In both cases the project's built or imported CMake targets will be
202+ available in the ` opentelemetry-cpp ` namespace (ie: ` opentelemetry-cpp::api ` )
169203
170204#### Using opentelemetry-cpp package components
171205
@@ -187,13 +221,17 @@ target_link_libraries(foo_lib PRIVATE opentelemetry-cpp::api)
187221# foo_app/CMakeLists.txt
188222find_package(opentelemetry-cpp CONFIG REQUIRED COMPONENTS api sdk exporters_otlp_grpc)
189223add_executable(foo_app main.cpp)
190- target_link_libraries(foo_app PRIVATE foo_lib opentelemetry-cpp::api opentelemetry-cpp::sdk opentelemetry-cpp::otlp_grpc_exporter )
224+ target_link_libraries(foo_app PRIVATE foo_lib opentelemetry-cpp::api opentelemetry-cpp::trace opentelemetry-cpp::otlp_grpc_exporter )
191225```
192226
193227The following table provides the mapping between components and targets. Components
194228and targets available in the installation depends on the opentelemetry-cpp package
195229build configuration.
196230
231+ > ** Note:** components ` exporters_elasticsearch ` and ` exporters_etw `
232+ may be moved out of the core package and to ` opentelemetry-cpp-contrib `
233+ in a future release
234+
197235| Component | Targets |
198236| ----------------------------| --------------------------------------------------------------------------------------------------|
199237| ** api** | opentelemetry-cpp::api |
0 commit comments