1

I want to use Google Protocol Buffers for C++ in XCode.
This is my directory where I have the library: /Developer/Protobuf.

What I did inside this directory, is compiled the .proto and produced the .pb.h & .pb.cc files. After this produced the object file:

clang++ -arch x86_64 -I./src -I./  -c file.pb.cc

Then:

ar -r file.pb.a file.pb.o

In XCode, in Build Phases -> Link Binary With Libraries I have added file.pb.a static library. In Build Settings -> Header Search Paths I have added /Developer/Protobuf/src. In Build Settings -> Librabry Search Paths I have added /Developer/Protobuf. In Build Settings -> User Header Search Paths I have added also /Developer/Protobuf/src.

But when I compiled the project I always get this kind of errors:

Undefined symbols for architecture x86_64:
  "google::protobuf::DescriptorPool::generated_pool()", referenced from:
      musicbrainz::protobuf_AssignDesc_musicbrainz_2eproto() in musicbrainz.pb.o
  "google::protobuf::DescriptorPool::InternalAddGeneratedFile(void const*, int)", referenced from:
      musicbrainz::protobuf_AddDesc_musicbrainz_2eproto() in musicbrainz.pb.o
  "google::protobuf::MessageFactory::generated_factory()", referenced from:
      musicbrainz::protobuf_AssignDesc_musicbrainz_2eproto() in musicbrainz.pb.o
  "google::protobuf::MessageFactory::InternalRegisterGeneratedFile(char const*, void (*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&))", referenced from:
      musicbrainz::protobuf_AddDesc_musicbrainz_2eproto() in musicbrainz.pb.o
.................................................................................

Maybe I am not creating the static library correct ?

4

1 回答 1

3

首先,您需要使用它们的 makefile 编译 Protocol Buffers 静态库,然后将静态库链接到您的项目中。您不应该将他们的源代码拉入您的 Xcode 项目。

将库链接到我的项目时,我遇到了与您相同的“未定义符号”错误。根据此讨论中的评论 #19,在构建协议缓冲区库时运行以下命令将使它们消失。

$ ./configure CC=clang CXX="clang++ -std=c++11 -stdlib=libc++" CXXFLAGS="-O3" --disable-shared
$ make
于 2013-12-10T00:38:12.027 回答