为CPP构建protobuf

问题描述 投票:0回答:1

我正在尝试在我的应用程序中使用 protobuf。 我想链接它来运行我的程序

我所做的步骤

  1. 通过自制软件安装
    brew install protobuf
  2. 检查是否包含绳索(因此出现错误)
    brew list --versions abseil
  3. 像这样更新了我的链接器标志
/usr/bin/g++ -std=c++17 -fdiagnostics-color=always -I/Users/me/dev/customDev/cpp/tiny_db/generated -g /Users/me/dev/customDev/cpp/tiny_db/include/tiny_db.pb.cc /Users/me/dev/customDev/cpp/tiny_db/main.cpp -o /Users/me/dev/customDev/cpp/tiny_db/main -lprotobuf
  1. 构建我的原型文件
    protoc --cpp_out=./include tiny_db.proto
  2. 将其包含在我的 main.cpp 中
#include <iostream>
#include <unordered_map>
#include <vector>
#include <stdexcept>
#include <fstream>
#include "include/tiny_db.pb.h"

我收到以下错误

Undefined symbols for architecture x86_64:
  "void absl::lts_20240722::log_internal::LogMessage::CopyToEncodedBuffer<(absl::lts_20240722::log_internal::LogMessage::StringType)0>(std::__1::basic_string_view<char, std::__1::char_traits<char>>)", referenced from:
      absl::lts_20240722::log_internal::LogMessage& absl::lts_20240722::log_internal::LogMessage::operator<<<19>(char const (&) [19]) in tiny_db-9b57b9.o
  "absl::lts_20240722::log_internal::LogMessage& absl::lts_20240722::log_internal::LogMessage::operator<<<unsigned long, 0>(unsigned long const&)", referenced from:
      absl::lts_20240722::log_internal::LogMessage::operator<<(unsigned long) in tiny_db-9b57b9.o
  "absl::lts_20240722::log_internal::LogMessageFatal::LogMessageFatal(char const*, int, std::__1::basic_string_view<char, std::__1::char_traits<char>>)", referenced from:
      Value::SharedDtor() in tiny_db-9b57b9.o
      Value::MergeImpl(google::protobuf::MessageLite&, google::protobuf::MessageLite const&) in tiny_db-9b57b9.o
      google::protobuf::MessageLite::ClassData::full() const in tiny_db-9b57b9.o
      Key::SharedDtor() in tiny_db-9b57b9.o
      Key::MergeImpl(google::protobuf::MessageLite&, google::protobuf::MessageLite const&) in tiny_db-9b57b9.o
      KeyValue::SharedDtor() in tiny_db-9b57b9.o
      KeyValue::MergeImpl(google::protobuf::MessageLite&, google::protobuf::MessageLite const&) in tiny_db-9b57b9.o
      KeyValue::MergeImpl(google::protobuf::MessageLite&, google::protobuf::MessageLite const&) in tiny_db-9b57b9.o
      KeyValue::MergeImpl(google::protobuf::MessageLite&, google::protobuf::MessageLite const&) in tiny_db-9b57b9.o
      ...
  "absl::lts_20240722::log_internal::LogMessageFatal::~LogMessageFatal()", referenced from:
      Value::SharedDtor() in tiny_db-9b57b9.o
      Value::SharedDtor() in tiny_db-9b57b9.o
      Value::MergeImpl(google::protobuf::MessageLite&, google::protobuf::MessageLite const&) in tiny_db-9b57b9.o
      Value::MergeImpl(google::protobuf::MessageLite&, google::protobuf::MessageLite const&) in tiny_db-9b57b9.o
      google::protobuf::MessageLite::ClassData::full() const in tiny_db-9b57b9.o
      google::protobuf::MessageLite::ClassData::full() const in tiny_db-9b57b9.o
      Key::SharedDtor() in tiny_db-9b57b9.o
      Key::SharedDtor() in tiny_db-9b57b9.o
      ...
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::MakeCheckOpString<void const*, void const*>(void const*, void const*, char const*)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::Check_NEImpl<Value const*, Value*>(Value const* const&, Value* const&, char const*) in tiny_db-9b57b9.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::Check_NEImpl<Key const*, Key*>(Key const* const&, Key* const&, char const*) in tiny_db-9b57b9.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::Check_NEImpl<KeyValue const*, KeyValue*>(KeyValue const* const&, KeyValue* const&, char const*) in tiny_db-9b57b9.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::Check_NEImpl<KeyValueStore const*, KeyValueStore*>(KeyValueStore const* const&, KeyValueStore* const&, char const*) in tiny_db-9b57b9.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::MakeCheckOpString<long long, long long>(long long, long long, char const*)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::Check_GEImpl<int, int>(int const&, int const&, char const*) in tiny_db-9b57b9.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::Check_GTImpl<int, int>(int const&, int const&, char const*) in tiny_db-9b57b9.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::Check_LTImpl<int, int>(int const&, int const&, char const*) in tiny_db-9b57b9.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::Check_EQImpl<int, int>(int const&, int const&, char const*) in tiny_db-9b57b9.o
  "absl::lts_20240722::log_internal::CheckOpMessageBuilder::ForVar2()", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::MakeCheckOpString<unsigned long long, long long>(unsigned long long, long long, char const*) in tiny_db-9b57b9.o
  "absl::lts_20240722::log_internal::CheckOpMessageBuilder::NewString()", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::MakeCheckOpString<unsigned long long, long long>(unsigned long long, long long, char const*) in tiny_db-9b57b9.o
  "absl::lts_20240722::log_internal::CheckOpMessageBuilder::CheckOpMessageBuilder(char const*)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20240722::log_internal::MakeCheckOpString<unsigned long long, long long>(unsigned long long, long long, char const*) in tiny_db-9b57b9.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

现在链接下降(-labsl_base -labsl_log_internal -labsl_strings -labsl_log_severity)给我 ld:找不到库“absl_log_internal”

检查已安装的 Abseil(与 protobuf Abseil 20240722.0 一起自动安装)

如何构建我的 cpp 文件?

编辑我看到了关于如何在编译时将 Google Protocol Buffers 库与我的应用程序正确链接?

无论如何,我不想从源代码构建protobuf,而是希望让它以更方便的方式工作

c++ protocol-buffers protobuf-c
1个回答
0
投票

对于任何遇到这个问题的人 - 我的解决方案是利用 pkg-config --cflags --libs protobuf 的输出并使用它来构建它。 由于我使用的是 VSC,所以我的任务 json 看起来像这样

{
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-std=c++17",
            "-fdiagnostics-color=always",

            "-I/usr/local/Cellar/protobuf/28.3/include",
            "-I/usr/local/Cellar/abseil/20240722.0/include",
            "-I/usr/local/Cellar/protobuf/28.3/include",
            "-I/usr/local/Cellar/abseil/20240722.0/include",
            "-I${fileDirname}/include",
            "-L/usr/local/Cellar/protobuf/28.3/lib",
            "-L/usr/local/Cellar/abseil/20240722.0/lib",
            "-L/usr/local/Cellar/protobuf/28.3/lib",
            "-L/usr/local/Cellar/abseil/20240722.0/lib",
            "-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_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_internal_globals",
            "-labsl_log_globals",
            "-labsl_vlog_config_internal",
            "-labsl_log_internal_fnmatch",
            "-labsl_raw_hash_set",
            "-labsl_hash",
            "-labsl_city",
            "-labsl_low_level_hash",
            "-labsl_hashtablez_sampler",
            "-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",
            "-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_strerror",
            "-labsl_str_format_internal",
            "-labsl_synchronization",
            "-labsl_graphcycles_internal",
            "-labsl_kernel_timeout_internal",
            "-labsl_stacktrace",
            "-labsl_symbolize",
            "-labsl_debugging_internal",
            "-labsl_demangle_internal",
            "-labsl_demangle_rust",
            "-labsl_decode_rust_punycode",
            "-labsl_utf8_for_code_point",
            "-labsl_malloc_internal",
            "-labsl_time",
            "-labsl_civil_time",
            "-labsl_time_zone",
            "-labsl_bad_variant_access",
            "-lutf8_validity",
            "-lutf8_range",
            "-labsl_strings",
            "-labsl_strings_internal",
            "-labsl_string_view",
            "-labsl_base",
            "-labsl_spinlock_wait",
            "-labsl_int128",
            "-labsl_throw_delegate",
            "-labsl_raw_logging_internal",
            "-labsl_log_severity",

            "-g",
            "${file}",
            "${fileDirname}/include/tiny_db.pb.cc",

            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
],
"version": "2.0.0"

}

© www.soinside.com 2019 - 2024. All rights reserved.