我正在尝试使用gcc
命令编译并运行.cpp
文件来测试google protobuf。
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 / cc,libprotobuf.a和“google source”在xcode中成功编译并运行它。
答案 0 :(得分:1)
在您使用-I
标志而不是-l
提及路径之前,gcc不知道google标头的include目录。由于google
目录位于当前工作目录中,因此无需为./google
传递-I
。
g++ -I./ CPP_TEST.cpp -o OUT_CPP_TEST
或者您可以使用您的错误建议所说的内容(使用引号而不是角度),因为您的文件层次结构符合此建议。
#include "google/protobuf/stubs/common.h"