2016-04-14 85 views
-1

我試圖用gcc命令編譯並運行.cpp文件來測試google protobuf。通過protoc未找到谷歌協議緩衝區的gcc頭文件

#ifndef PROTOBUF_GameInfo_2eproto__INCLUDED 
#define PROTOBUF_GameInfo_2eproto__INCLUDED 

#include <string> 

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

產生

CPP_TEST.cpp

#include "GameInfo.pb.h" 

int main() { 
    // ... 
} 

GameInfo.pb.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編譯器標誌一個錯誤,但無法弄清楚爲什麼...

任何建議將讚賞,感謝: )

UPDATE:

後改變命令

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) 

UPDATE:

我試圖編譯並以相同的步驟鏈接的所有文件步:

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 

這真的很奇怪,因爲我可以用xcode和所有GameInfo.pb.h/cc GameEnum.pb.h/cc,libprotobuf.a和「google源代碼」成功編譯並運行它。

回答

1

gcc不知道google頭的包含目錄,直到您提到使用-I標誌的路徑不是-l。由於google目錄在當前工作目錄中,所以無需爲-I通過./google

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

要不然你可以用你的錯誤建議,說什麼(使用引號來代替角),因爲你的文件層次這一建議相匹配。

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

謝謝。我嘗試使用'-I'標誌,但仍然得到相同的錯誤... – supersuraccoon

+0

@supersuraccoon我應該使用當前目錄'-I。/'而不是'-I。/ google'。檢查更新的答案。 –

+0

感謝您的評論。它使用「-I./」標誌後編譯器運行良好...但現在我無法連接libprotobuf.a文件。你能檢查這個問題的更新嗎?謝謝:) – supersuraccoon