2016-04-14 5 views
-1

.cppファイルをコンパイルして実行して、Googleのprotobufをテストしています。gccコマンドを使用しようとしています。Googleプロトコルバッファでgccヘッダーファイルが見つかりません

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コンパイラフラグとエラーだと思いますが、なぜ把握することはできません...

何かアドバイスは、感謝を理解されるであろう。 )

UPDATE:

にコマンドを変更した後

gcc CPP_TEST.cpp -I./ -o OUT_CPP_TEST 

ファイルはコンパイラで正常に実行され、実行されます。

しかし、私はmain関数にコードを追加した場合:だから私は、gccのコマンドに

libprotobuf.aファイルを追加しようとしました

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) 

game::info::GameInfo gameInfoOut; 

とコンパイラそれはで失敗します

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 

GameInfo.pb.h/cc GameEnum.pb.h/cc、libprotobuf.aと「google source」のすべてをxcodeでコンパイルして正常に実行できるので、本当に奇妙です。

答えて

1

gccは、-Iフラグが-lではないパスを指定するまで、Googleヘッダーのインクルードディレクトリを認識しません。googleディレクトリが現在の作業ディレクトリにあるので、./google-Iに渡す必要はありません。

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

または他のあなたは自分のファイル階層が、この提案に一致するので、あなたのエラーの提案は(使用ではなく、角度の引用)言ったことを使用することができます。

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

ありがとうございました。私は '-I'フラグを使用しようとしましたが、同じエラーが発生しました... – supersuraccoon

+0

@supersuraccoon' -I。/ google'の代わりに '-I//'を使ってください。更新された回答を確認してください。 –

+0

コメントありがとうございます。 "-I./"フラグを使用してコンパイルして正常に動作します...しかし、今はlibprotobuf.aファイルのリンクに問題があります。この質問の更新を確認できますか?ありがとう:) – supersuraccoon

関連する問題