google 协议缓冲区未找到 gcc 头文件

gcc header file not found with google protocol buffer

我正在尝试使用 gcc 命令编译和 运行 一个 .cpp 文件来测试 google protobuf。

CPP_TEST.cpp

#include "GameInfo.pb.h"

int main() {
   // ...
}

GameInfo.pb.h 由 protoc

生成
#ifndef PROTOBUF_GameInfo_2eproto__INCLUDED
#define PROTOBUF_GameInfo_2eproto__INCLUDED

#include <string>

#include <google/protobuf/stubs/common.h>
// ...

我文件夹中的文件如下所示

- test
    CPP_TEST.cpp
    GameInfo.pb.h
    GameInfo.pb.cc

    // the lib file for protobuf
    libprotobuf.a

    // the srouce code of protobuf
    - google
        - protobuf
            // ...
            - stubs
                common.h
                // ...

然后我尝试编译 .cpp 文件

gcc CPP_TEST.cpp -l ./google -o OUT_CPP_TEST

但是得到错误:

#include <google/protobuf/stubs/common.h>
'google/protobuf/stubs/common.h' file not found with <angled> include; use "quotes" instead

我认为这是 gcc 编译器标志的错误,但无法弄清楚为什么...

如有任何建议,我们将不胜感激:)

更新:

将命令更改为

gcc CPP_TEST.cpp -I./ -o OUT_CPP_TEST

该文件对编译器是正确的,运行。

但是如果我在 main 函数中添加代码:

game::info::GameInfo gameInfoOut;

编译器会失败:

Undefined symbols for architecture x86_64:
  "game::info::GameInfo::GameInfo()", referenced from:
      _main in CPP_TEST-9245ef.o
  "game::info::GameInfo::~GameInfo()", referenced from:
      _main in CPP_TEST-9245ef.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

所以我尝试将 libprotobuf.a 文件添加到 gcc 命令

gcc CPP_TEST.cpp -L./ -lprotobuf -I./ -o CPP_TEST_OUT

但我仍然遇到同样的错误:

Undefined symbols for architecture x86_64:
  "game::info::GameInfo::GameInfo()", referenced from:
      _main in CPP_TEST-0e3576.o
  "game::info::GameInfo::~GameInfo()", referenced from:
      _main in CPP_TEST-0e3576.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

更新:

我尝试编译并link所有文件一步一步像:

gcc -c -I./ GameInfo.pb.cc 
gcc -c -I./ GameEnum.pb.cc 
gcc -c -I./ CPP_TEST.cpp

和link

gcc CPP_TEST.o GameInfo.pb.o GameEnum.pb.o -L./ -lprotobuf -o main

现在我收到一堆错误,例如:

Undefined symbols for architecture x86_64:
  "___dynamic_cast", referenced from:
      game::info::RoleInfo const* google::protobuf::internal::dynamic_cast_if_available<game::info::RoleInfo const*, google::protobuf::Message const*>(google::protobuf::Message const*) in GameInfo.pb.o
      game::info::ItemInfo const* google::protobuf::internal::dynamic_cast_if_available<game::info::ItemInfo const*, google::protobuf::Message const*>(google::protobuf::Message const*) in GameInfo.pb.o
      game::info::GameInfo const* google::protobuf::internal::dynamic_cast_if_available<game::info::GameInfo const*, google::protobuf::Message const*>(google::protobuf::Message const*) in GameInfo.pb.o
      google::protobuf::FileDescriptorSet const* google::protobuf::internal::dynamic_cast_if_available<google::protobuf::FileDescriptorSet const*, google::protobuf::Message const*>(google::protobuf::Message const*) in libprotobuf.a(descriptor.pb.o)
      google::protobuf::FileDescriptorProto const* google::protobuf::internal::dynamic_cast_if_available<google::protobuf::FileDescriptorProto const*, google::protobuf::Message const*>(google::protobuf::Message const*) in libprotobuf.a(descriptor.pb.o)
      google::protobuf::DescriptorProto_ExtensionRange const* google::protobuf::internal::dynamic_cast_if_available<google::protobuf::DescriptorProto_ExtensionRange const*, google::protobuf::Message const*>(google::protobuf::Message const*) in libprotobuf.a(descriptor.pb.o)
      google::protobuf::DescriptorProto const* google::protobuf::internal::dynamic_cast_if_available<google::protobuf::DescriptorProto const*, google::protobuf::Message const*>(google::protobuf::Message const*) in libprotobuf.a(descriptor.pb.o)
      ...
  "___gxx_personality_v0", referenced from:
      game::enumeration::protobuf_AssignDesc_GameEnum_2eproto() in GameEnum.pb.o
      google::protobuf::GoogleOnceInit(long*, void (*)()) in GameEnum.pb.o
      Dwarf Exception Unwind Info (__eh_frame) in GameEnum.pb.o
      game::info::protobuf_AssignDesc_GameInfo_2eproto() in GameInfo.pb.o
      game::info::protobuf_AddDesc_GameInfo_2eproto() in GameInfo.pb.o
      game::info::RoleInfo::RoleInfo() in GameInfo.pb.o
      game::info::RoleInfo::RoleInfo(game::info::RoleInfo const&) in GameInfo.pb.o
      // more here

这真的很奇怪,因为我可以编译器并且 运行 它在 xcode 中与所有 GameInfo.pb.h/cc GameEnum.pb.h/cc、libprotobuf.a 和 "google source".

gcc 不知道 google headers 的包含目录,直到您使用 -I 标志而不是 -l 提及路径。由于 google 目录位于当前工作目录中,因此无需为 -I.

传递 ./google

g++ -I./ CPP_TEST.cpp -o OUT_CPP_TEST

否则你可以只使用你的错误建议所说的(使用引号而不是角度),因为你的文件层次结构符合这个建议。

#include "google/protobuf/stubs/common.h"