Skip to content

The pkg-config files should define PROTOBUF_USE_DLLS if the libraries are shared #12699

@coryan

Description

@coryan

What version of protobuf and what language are you using?

Version: 22.4
Language: C++

What operating system (Linux, Windows, ...) and version?

Linux

What runtime / compiler are you using (e.g., python version or gcc version)

GCC 12

What did you do?

Compile protobuf as a shared library and then try to link it using pkg-config to find the necessary compiler and link flags.

What did you expect to see

A successful build.

What did you see instead?

make: Entering directory '/workspace/google/cloud/profiler/quickstart'
g++  -DGOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY -DHAVE_ABSEIL -pthread -DNOMINMAX -I/tmp/tmp.eh1b3XZLXZ/lib64/pkgconfig/../../include -I/usr/local/include -L/tmp/tmp.eh1b3XZLXZ/lib64/pkgconfig/../../lib64 -L/usr/local/lib64 -L/usr/local/lib -L/usr/lib64 -o /workspace/cmake-out/quickstart/makefile-profiler/quickstart quickstart.cc -lgoogle_cloud_cpp_profiler -lgoogle_cloud_cpp_grpc_utils -lgoogle_cloud_cpp_iam_protos -lgoogle_cloud_cpp_iam_v1_iam_policy_protos -lgoogle_cloud_cpp_iam_v1_options_protos -lgoogle_cloud_cpp_iam_v1_policy_protos -lgoogle_cloud_cpp_type_expr_protos -lgoogle_cloud_cpp_longrunning_operations_protos -lgoogle_cloud_cpp_rpc_status_protos -lgoogle_cloud_cpp_rpc_error_details_protos -lgoogle_cloud_cpp_common -lgoogle_cloud_cpp_profiler_protos -lgoogle_cloud_cpp_api_annotations_protos -lgoogle_cloud_cpp_api_client_protos -lgoogle_cloud_cpp_api_field_behavior_protos -lgoogle_cloud_cpp_api_http_protos -lgoogle_cloud_cpp_api_launch_stage_protos -lgoogle_cloud_cpp_api_resource_protos -lgrpc++ -lgrpc -lgpr -labsl_random_distributions -labsl_random_seed_sequences -labsl_random_internal_pool_urbg -labsl_random_internal_randen -labsl_random_internal_randen_hwaes -labsl_random_internal_randen_hwaes_impl -labsl_random_internal_randen_slow -labsl_random_internal_platform -labsl_random_internal_seed_material -labsl_random_seed_gen_exception -lssl -lcrypto -lprotobuf -labsl_log_internal_check_op -labsl_leak_check -labsl_die_if_null -labsl_log_internal_conditions -labsl_log_internal_message -labsl_examine_stack -labsl_log_internal_format -labsl_log_internal_proto -labsl_log_internal_nullguard -labsl_log_internal_log_sink_set -labsl_log_sink -labsl_log_entry -labsl_flags -labsl_flags_internal -labsl_flags_marshalling -labsl_flags_reflection -labsl_flags_private_handle_accessor -labsl_flags_commandlineflag -labsl_flags_commandlineflag_internal -labsl_flags_config -labsl_flags_program_name -labsl_log_initialize -labsl_log_globals -labsl_log_internal_globals -labsl_hash -labsl_city -labsl_low_level_hash -labsl_raw_hash_set -labsl_hashtablez_sampler -labsl_statusor -labsl_status -labsl_cord -labsl_cordz_info -labsl_cord_internal -labsl_cordz_functions -labsl_exponential_biased -labsl_cordz_handle -labsl_crc_cord_state -labsl_crc32c -labsl_crc_internal -labsl_crc_cpu_detect -labsl_bad_optional_access -labsl_str_format_internal -labsl_strerror -labsl_synchronization -labsl_graphcycles_internal -labsl_stacktrace -labsl_symbolize -labsl_debugging_internal -labsl_demangle_internal -labsl_malloc_internal -labsl_time -labsl_civil_time -labsl_time_zone -labsl_bad_variant_access -lutf8_validity -lutf8_range -labsl_strings -labsl_strings_internal -lrt -labsl_base -labsl_spinlock_wait -labsl_int128 -labsl_throw_delegate -labsl_raw_logging_internal -labsl_log_severity -lz -lcares
make: Leaving directory '/workspace/google/cloud/profiler/quickstart'

-- stderr output is:
/usr/bin/ld: /tmp/ccZ3DEOv.o: in function `google::protobuf::internal::ThreadSafeArena::thread_cache()':
quickstart.cc:(.text._ZN6google8protobuf8internal15ThreadSafeArena12thread_cacheEv[_ZN6google8protobuf8internal15ThreadSafeArena12thread_cacheEv]+0x10): undefined reference to `google::protobuf::internal::ThreadSafeArena::thread_cache_'
collect2: error: ld returned 1 exit status
make: *** [Makefile:35: /workspace/cmake-out/quickstart/makefile-profiler/quickstart] Error 1

Anything else we should know about your project / environment

The error compiles from this file:

https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/profiler/quickstart/Makefile

The google_cloud_cpp_profiler pkg-config module depends on protobuf. protobuf.pc does not add -DPROTOBUF_USE_DLLS:

Cflags: -I${includedir}

But the ABI requires this to be defined if the library was built as a .so:

#elif defined(PROTOBUF_USE_DLLS)
// Thread local variables cannot be exposed through DLL interface but we can
// wrap them in static functions.
static ThreadCache& thread_cache();
#else
PROTOBUF_CONSTINIT static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_;
static ThreadCache& thread_cache() { return thread_cache_; }
#endif

I will send a PR to add the necessary flag to the .pc file. But maybe a better fix would be to remove the #ifdef PROTOBUF_USE_DLLS from this header.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions