open-telemetry / open-telemetry/opentelemetry-cpp

Undefined reference error while building simple example

Open
#2,188 33 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug Stale
Dominant language
C++
Stars
1.4k
Forks
632
Avg merge
1d 13h
Merged PRs (30d)
75

Description

Describe your environment Describe any aspect of your environment relevant to the problem, including your platform, build system, version numbers of installed dependencies, etc. If you're reporting a problem with a specific version of a library in this repo, please check whether the problem has been fixed on main branch.
OS - SUSE Linux Enterprise Server 15
Installed opentelemetry-cpp[otlp-grpc] through vcpkg.

Steps to reproduce
Describe exactly how to reproduce the error. Include a code sample if applicable.
I ran a slightly modified version of the simple example given in the repository.

g++ trial.cpp -std=c++14 -I vcpkg/packages/opentelemetry-cpp_x64-linux/include/ && ./a.out
#include "opentelemetry/exporters/ostream/span_exporter_factory.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/provider.h"



namespace trace_api      = opentelemetry::trace;
namespace trace_sdk      = opentelemetry::sdk::trace;
namespace trace_exporter = opentelemetry::exporter::trace;

namespace
{
void InitTracer()
{
  auto exporter  = trace_exporter::OStreamSpanExporterFactory::Create();
  auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
  std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
      trace_sdk::TracerProviderFactory::Create(std::move(processor));

  // Set the global trace provider
  trace_api::Provider::SetTracerProvider(provider);
}

void CleanupTracer()
{
  std::shared_ptr<opentelemetry::trace::TracerProvider> none;
  trace_api::Provider::SetTracerProvider(none);
}
}  // namespace

int main()
{
  // Removing this line will leave the default noop TracerProvider in place.
  InitTracer();

    auto provider = trace_api::Provider::GetTracerProvider();
    auto tracer = provider->GetTracer("foo_library");

  //create a span
    auto span = tracer->StartSpan("first-span");
    auto scope = tracer->WithActiveSpan(span);
    //inner ops
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    span->End();

    std::cout<<"Heloworld!"<<std::endl;
    CleanupTracer();
    return 0;
} 

What is the expected behavior?
What did you expect to see?
The program should compile without errors and execute.

What is the actual behavior?
What did you see instead?
The program passes syntax and lexical analysis , and fails during machine code generation, with the error

 /tmp/ccdjnFU7.o: In function `(anonymous namespace)::InitTracer()':
trial.cpp:(.text+0x1c5): undefined reference to `opentelemetry::v1::exporter::trace::OStreamSpanExporterFactory::Create()'
trial.cpp:(.text+0x1e3): undefined reference to `opentelemetry::v1::sdk::trace::SimpleSpanProcessorFactory::Create(std::unique_ptr<opentelemetry::v1::sdk::trace::SpanExporter, std::default_delete<opentelemetry::v1::sdk::trace::SpanExporter> >&&)'
trial.cpp:(.text+0x214): undefined reference to `opentelemetry::v1::sdk::trace::TracerProviderFactory::Create(std::unique_ptr<opentelemetry::v1::sdk::trace::SpanProcessor, std::default_delete<opentelemetry::v1::sdk::trace::SpanProcessor> >)'
collect2: error: ld returned 1 exit status 

Additional context
Add any other context about the problem here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the modified simple example in trial.cpp and the reported g++ command. Check the example's build or linking requirements for the vcpkg-installed opentelemetry-cpp libraries, then verify that the same program compiles and executes without the three undefined references.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
build-system
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.