2016-10-19 2 views
0

後世のために私の仕事を投稿しています。 C++で私の最後の例を完成させた後で、私が実際にC言語でそれを行う必要があったことを実感しました。どちらの反復もJavaプログラマとしてかなりの努力を払ってくれました。サンプルコードの多くは、あまりにも多くの穴を残していると思います。-特にかなり慣れ親しんだ人のためにコマンドラインからかなり難しいビルディングになるとEclipseを使用して、プロジェクトを構築し、依存関係を処理します。醸造とOSX用の依存関係をインストールする方法ZeroMQ上の単純なフラットバッファCの例 - zmq上のstructをflatbufferにコピーし、再び構造体に戻します

brew install flatcc
brew install zeromq

あなたが同様にインストールされているすべての標準的なビルダーのバイナリが必要になります。私はコンパイルにはgccを使用:

gcc publisher.c -o bin/zmq_pub -lzmq -lflatcc
gcc subscriber.c -o bin/zmq_sub -lzmq

これは、あなたが醸造は、それらのインストールが終了した後、/あなたは/ usr/localが含まへのシンボリックリンクを取得する必要がありzmqとflatccライブラリをインストールしている前提としています。あなたはライブラリが正しくリンク/インストールされていない場合は Undefined symbols for architecture x86_64::次のように:

zmq_cpub $ls -la /usr/local/include lrwxr-xr-x 1 user group 37 Oct 18 18:43 flatcc -> ../Cellar/flatcc/0.3.4/include/flatcc

次のようなコンパイルエラーを取得します。コンパイラ/リンカは関数の名前を変更し、それらの前に_を付けて、潜在的にあなたを混乱させます。 _flatcc_builder_initではないとは限りませんが、Undefined symbols for architecture x86_64 _flatcc_builder_initのようになります。

これは、C/C++でのライブラリのリンクがJavaとは基本的に異なるためです。 JARを追加する特定のプロジェクト構築パスの代わりに、外部C/C++ライブラリをインストールできる既知の場所があります。 /usr/local/include,/usr/local/lib,/usr/libおよび/usr/include

そして、あなたのパスにflatccバイナリをインストールした後、あなたの地元のプロジェクトで使用するヘッダファイルを生成することを忘れないでください:私は私の旅に直面したほとんどすべての障害物でなければなりません

flatcc -a Car.fbs

ダウンCレーン。誰かを助けてくれることを願っています。

答えて

0

Car.fbs

namespace Test; 

table Car { 
    name: string; 
    model: string; 
    year: int; 
} 
root_type Car; 

Subscriber.c(着信構造体をリッスン)

// Hello World client 
#include "flatbuffers/Car_builder.h" // Generated by `flatcc`. 
#include "flatbuffers/flatbuffers_common_builder.h" 
#include <zmq.h> 

#undef ns 
#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(Test, x) // Specified in the schema 

struct Car { 
    char* name; 
    char* model; 
    int year; 
}; 

int main (void) 
{ 
    printf ("Connecting to car world server...\n"); 
    void *context = zmq_ctx_new(); 
    void *requester = zmq_socket (context, ZMQ_REQ); 
    zmq_connect (requester, "tcp://localhost:5555"); 

    int request_nbr; 
    for (request_nbr = 0; request_nbr != 10; request_nbr++) { 
     char buffer [1024]; 
     printf ("Sending ready signal %d...\n", request_nbr); 
     zmq_send (requester, "Hello", 5, 0); 
     zmq_recv (requester, buffer, 1024, 0); 
     printf ("Received car %d\n", request_nbr); 
     ns(Car_table_t) car = ns(Car_as_root(buffer)); 
     int year = ns(Car_year(car)); 
     flatbuffers_string_t model = ns(Car_model(car)); 
     flatbuffers_string_t name = ns(Car_name(car)); 

     struct Car nextCar; 
     // no need to double up on memory!! 
     // strcpy(nextCar.model, model); 
     // strcpy(nextCar.name, name); 
     nextCar.model = model; 
     nextCar.name = name; 
     nextCar.year = year; 

     printf("Year: %d\n", nextCar.year); 
     printf("Name: %s\n", nextCar.name); 
     printf("Model: %s\n", nextCar.model); 
    } 
    zmq_close (requester); 
    zmq_ctx_destroy (context); 
    return 0; 
} 

Publisher.c(zmqソケット上に構造体を送る):

// Hello World server 

#include "flatbuffers/Car_builder.h" // Generated by `flatcc`. 
#include "flatbuffers/flatbuffers_common_builder.h" 
#include <zmq.h> 
#include <unistd.h> 
#include <time.h> 

#undef ns 
#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(Test, x) // specified in the schema 

struct Car { 
    char name[10]; 
    char model[10]; 
    int year; 
}; 

struct Car getRandomCar() { 
    struct Car randomCar; 
    int a = rand(); 
    if ((a % 2) == 0) { 
     strcpy(randomCar.name, "Ford"); 
     strcpy(randomCar.model, "Focus"); 
    } else { 
     strcpy(randomCar.name, "Dodge"); 
     strcpy(randomCar.model, "Charger"); 
    } 
    randomCar.year = rand(); 
    return randomCar; 
} 

int main (void) 
{ 
    srand(time(NULL)); 

    // Socket to talk to clients 
    void *context = zmq_ctx_new(); 
    void *responder = zmq_socket (context, ZMQ_REP); 
    int rc = zmq_bind (responder, "tcp://*:5555"); 
    assert (rc == 0); 
    int counter = 0; 

    while (1) { 
     struct Car c = getRandomCar(); 

     flatcc_builder_t builder, *B; 
     B = &builder; 
     // Initialize the builder object. 
     flatcc_builder_init(B); 
     uint8_t *buf; // raw buffer used by flatbuffer 
     size_t size; // the size of the flatbuffer 
     // Convert the char arrays to strings 
     flatbuffers_string_ref_t name = flatbuffers_string_create_str(B, c.name); 
     flatbuffers_string_ref_t model = flatbuffers_string_create_str(B, c.model); 

     ns(Car_start_as_root(B)); 
     ns(Car_name_add(B, name)); 
     ns(Car_model_add(B, model)); 
     ns(Car_year_add(B, c.year)); 
     ns(Car_end_as_root(B)); 
     buf = flatcc_builder_finalize_buffer(B, &size); 

     char receiveBuffer [10]; 
     zmq_recv (responder, receiveBuffer, 10, 0); 
     printf ("Received ready signal. Sending car %d.\n", counter); 
     sleep (1);   // Do some 'work' 
     zmq_send (responder, buf, size, 0); 
     counter++; 

     free(buf); 
    } 
    return 0; 
} 
関連する問題