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

gcc header file not found with google protocol buffer

本文关键字:缓冲区 gcc 文件 协议 google      更新时间:2023-10-16

我正试图使用gcc命令编译并运行一个.cpp文件来测试googleprotobuf。

CPP_TEST.CPP

#include "GameInfo.pb.h"
int main() {
   // ...
}

protoc 生成的GameInfo.pb.h

#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)

更新:

我试图一步一步地编译和链接所有文件,比如:

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

和链接

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

这真的很奇怪,因为我可以用所有的GameInfo.pb.h/cc GameEnum.pb.h/c、libprotobuf.a和"谷歌源代码"编译并在xcode中成功运行它。

gcc不知道google头的include目录,直到您提到使用-I标志而不是-l的路径。由于google目录在当前工作目录中,因此不需要为-I传递./google

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

或者,你可以使用你的错误建议(使用引号而不是角度),因为你的文件层次结构与这个建议相匹配。

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