Home > Blockchain >  How to resolve linker error for protobuf on macos?
How to resolve linker error for protobuf on macos?

Time:11-21

I'm trying to link x86_64 compiled libprotobuf to my x86_64 executable. I'm doing this on M1 arm64 Macbook Pro which should not matter (cross-compiling). But I'm getting an error.

One other closed-source dependency is x86_64 so I cannot do arm64 throught the project

I've changed terminal arch to i386 with arm -x86_64.
I've built protobuf x86_64 using:

cmake -DCMAKE_CXX_STANDARD=17 -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE .. -DCMAKE_INSTALL_PREFIX=<path>/libs/protobuf/mac64 -DCMAKE_OSX_ARCHITECTURES=x86_64
make -j
make install

The library I get libprotobuf.a seems to be x86_64:

objdump -T <path>/libs/protobuf/mac64/lib/libprotobuf.a

....
<path>/lib/libprotobuf.a(common.cc.o):  file format mach-o 64-bit x86-64

DYNAMIC SYMBOL TABLE:
<path>/objdump: warning: 'common.cc.o': this operation is not currently supported for this file format
....

so the libprotobuf.a looks like x86_64. However protobuf does not work when linked to an application. This process worked with success for the spdlog library.

#include <iostream>

#include "message.pb.h"

int main() {
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    return 0;
}

g   -std=c  17 -I<path>/protobuf/mac64/include main.cpp -o main -L<path>/libs/protobuf/mac64/lib -lprotobuf

And the linker throws an error:

Undefined symbols for architecture x86_64:
  "absl::lts_20220623::numbers_internal::FastIntToBuffer(int, char*)", referenced from:
      absl::lts_20220623::AlphaNum::AlphaNum(int) in libprotobuf.a(common.cc.o)
  "absl::lts_20220623::numbers_internal::FastIntToBuffer(unsigned int, char*)", referenced from:
      absl::lts_20220623::AlphaNum::AlphaNum(unsigned int) in libprotobuf.a(common.cc.o)
  "absl::lts_20220623::numbers_internal::FastIntToBuffer(long long, char*)", referenced from:
      char* absl::lts_20220623::numbers_internal::FastIntToBuffer<long>(long, char*) in libprotobuf.a(common.cc.o)
      absl::lts_20220623::AlphaNum::AlphaNum(long long) in libprotobuf.a(common.cc.o)
  "absl::lts_20220623::numbers_internal::FastIntToBuffer(unsigned long long, char*)", referenced from:
      char* absl::lts_20220623::numbers_internal::FastIntToBuffer<unsigned long>(unsigned long, char*) in libprotobuf.a(common.cc.o)
      absl::lts_20220623::AlphaNum::AlphaNum(unsigned long long) in libprotobuf.a(common.cc.o)
  "absl::lts_20220623::numbers_internal::SixDigitsToBuffer(double, char*)", referenced from:
      absl::lts_20220623::AlphaNum::AlphaNum(double) in libprotobuf.a(common.cc.o)
  "absl::lts_20220623::AlphaNum::AlphaNum(absl::lts_20220623::Hex)", referenced from:
      google::protobuf::internal::LogMessage::operator<<(void*) in libprotobuf.a(common.cc.o)
  "absl::lts_20220623::StrAppend(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, absl::lts_20220623::AlphaNum const&)", referenced from:
      google::protobuf::internal::LogMessage::operator<<(absl::lts_20220623::string_view) in libprotobuf.a(common.cc.o)
      google::protobuf::internal::LogMessage::operator<<(void*) in libprotobuf.a(common.cc.o)
      google::protobuf::internal::LogMessage::operator<<(int) in libprotobuf.a(common.cc.o)
      google::protobuf::internal::LogMessage::operator<<(unsigned int) in libprotobuf.a(common.cc.o)
      google::protobuf::internal::LogMessage::operator<<(long) in libprotobuf.a(common.cc.o)
      google::protobuf::internal::LogMessage::operator<<(unsigned long) in libprotobuf.a(common.cc.o)
      google::protobuf::internal::LogMessage::operator<<(double) in libprotobuf.a(common.cc.o)
      ...
  "absl::lts_20220623::Status::ToStringSlow(absl::lts_20220623::StatusToStringMode) const", referenced from:
      absl::lts_20220623::Status::ToString(absl::lts_20220623::StatusToStringMode) const in libprotobuf.a(common.cc.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Do I need to link some other libraries? Or are there any examples I can take a look at?

CodePudding user response:

Need to use the release version.

I used a git clone version of the protobuf library and faced this issue. After downloading the actual release version it worked well.

  • Related