Reputation: 1691
I'm trying to use the gcc
command to compile and run a .cpp
file to test google protobuf.
CPP_TEST.cpp
#include "GameInfo.pb.h"
int main() {
// ...
}
GameInfo.pb.h generated by protoc
#ifndef PROTOBUF_GameInfo_2eproto__INCLUDED
#define PROTOBUF_GameInfo_2eproto__INCLUDED
#include <string>
#include <google/protobuf/stubs/common.h>
// ...
and my files in folder looks like this
- 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
// ...
and then I tried to compile the .cpp
file
gcc CPP_TEST.cpp -l ./google -o OUT_CPP_TEST
But get the error:
#include <google/protobuf/stubs/common.h>
'google/protobuf/stubs/common.h' file not found with <angled> include; use "quotes" instead
I think it is an error with the gcc compiler flag but can not figure out why...
Any advice will be appreciated, thanks :)
UPDATE:
After changing the command to
gcc CPP_TEST.cpp -I./ -o OUT_CPP_TEST
The file is ok to compiler and run.
But if I add a code to the main
function :
game::info::GameInfo gameInfoOut;
And compiler it will fail with:
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)
So I tried to add the libprotobuf.a
file to gcc command
gcc CPP_TEST.cpp -L./ -lprotobuf -I./ -o CPP_TEST_OUT
But I am still getting the same error:
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)
UPDATE:
I tried to complier and link all the files step by step like:
gcc -c -I./ GameInfo.pb.cc
gcc -c -I./ GameEnum.pb.cc
gcc -c -I./ CPP_TEST.cpp
and link
gcc CPP_TEST.o GameInfo.pb.o GameEnum.pb.o -L./ -lprotobuf -o main
And now I am getting bunch of errors like:
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
It is really weird since the I can complier and run it successfully in the xcode with all the GameInfo.pb.h/cc GameEnum.pb.h/cc, libprotobuf.a and "google source".
Upvotes: 0
Views: 2714
Reputation: 1395
gcc doesn't know the include directory of google headers until you mention the path using -I
flag not -l
. As google
directory is in the current working directory so no need to pass ./google
for -I
.
g++ -I./ CPP_TEST.cpp -o OUT_CPP_TEST
or else you can just use what your error suggestion said (use quotes instead of angles) because your file hierarchy matches this suggestion.
#include "google/protobuf/stubs/common.h"
Upvotes: 1