ZeroMQ Go 오픈소스를 활용하여 C코드에서 사용 할 수 있는 Library 테스트

 

(ZeroMQ callback function)

  • Make a library for using at C code.
  • Make a C code.
  • Build by linking a library to C code.

ZeroMq Opensource URL

 

go-zeromq/zmq4

[WIP] Pure-Go implementation of ZeroMQ-4. Contribute to go-zeromq/zmq4 development by creating an account on GitHub.

github.com

download go command

  • go get github.com/go-zeromq/zmq4
package main
/*
#include <stdio.h>
typedef int (*FuncPtr)(int, int);

static inline int Shoot(FuncPtr func,int a,int b)
{
        func(a,b);
}
*/
import "C"
import (
        "context"
        "github.com/go-zeromq/zmq4"
        "log"
)

//export Rcv
func Rcv(t C.FuncPtr) {   
        log.SetPrefix("psenvsub: ")
        sub := zmq4.NewSub(context.Background())
        defer sub.Close()
        err := sub.Dial("tcp://localhost:5563")
        if err != nil {
                log.Fatalf("could not dial: %v", err)
        }
        err = sub.SetOption(zmq4.OptionSubscribe, "test1") //Received only named "test1" key.
        if err != nil {
                log.Fatalf("could not subscribe: %v", err)
        }
        var a int =0
        var b int =0
        for {
                msg, err := sub.Recv()
                if err != nil {
                        log.Fatalf("could not receive message: %v", err)
                }
                log.Printf("[%s] %s\n", msg.Frames[0], msg.Frames[1])
                C.Shoot(t,C.int(a),C.int(b))

                a+=1
                b+=1
        }
}

func main() {} // 반드시 main function 작성.

library.go(수신용 라이브러리 Go파일)

go tool 명령어를 실행하여 라이브러리 빌드

  • command 실행-> go build -o library.so -buildmode=c-shared library.go
#include <stdio.h>
#include "library.h"

int plus (int first, int second)
{
    printf("first:%d second:%d!!!!!!!!!!!!!!!!!!!!!!!!!!!\n",first,second);
        return first + second;
}

int main()
{
        Rcv(plus);
        return 0;
}

rev.c(라이브러리 callback함수)

gcc를 이용하여 빌드

  • gcc -o rcv ./rcv.c ./library.so

 

 

'언어 > Golang' 카테고리의 다른 글

Pure zmq4 based on golang 오픈소스를 활용하여 PUB/SUB 테스트  (0) 2020.11.23
Golang  (0) 2017.05.22

jansson json library 컴파일 및 샘플테스트

 

 

설치환경: Ubuntu Server 18.04 LTS

설치방법

  •  해당 사이트에 접속하여 latest버전의 tar.gz파일을 다운로드.
  • 다운로드받은 tar.gz파일을 설치환경으로 put한다.

tar파일 압축 해제

  • tar파일을 압축해제.
    • # tar -xvf ./jansson-2.13.1.tar.gz

 

압축해제 한 jansson directory

  • jansson디렉토리내에서 아래 명령어를 순차적으로 실행한다.
  • 설치환경에는 gcc또는 make가 없다면 설치해야한다.
    • sudo apt-get install gcc
    • sudo apt-get install make
  • # ./configure
  • # make
  • # make install

 

  • 위 과정을 완료한 후 설치환경의 global영역에 .a 및 .so파일이 있는지 확인.

 

샘플테스트코드작성

#include <stdio.h>
#include <jansson.h>
int main() {     
        json_t* obj=json_object();    
        json_t* strVal1=json_string("a");   
        json_t* strVal2=json_string("b");  
        json_t* strVal3=json_string("c");  
        json_t* strVal4=json_string("d");  
        json_t* strVal5=json_string("e");
        json_t* strVal6=json_string("f");
        json_t* strVal7=json_string("g");
        json_t* strVal8=json_string("h");
        json_object_set_new(obj,"myKey1",strVal1);
        json_object_set_new(obj,"myKey2",strVal2);  
        json_object_set_new(obj,"myKey3",strVal3);
        json_object_set_new(obj,"myKey4",strVal4);
        json_object_set_new(obj,"myKey5",strVal5);        json_object_set_new(obj,"myKey6",strVal6);
        json_object_set_new(obj,"myKey7",strVal7);    
        json_object_set_new(obj,"myKey8",strVal8);        const char* key;
        json_t *value;   
        void* iter=json_object_iter(obj);
        while(iter)     {
                key=json_object_iter_key(iter);
                value=json_object_iter_value(iter);
                printf("%s=%s\n",key,json_string_value(value));
                iter = json_object_iter_next(obj, iter);
        }
        return 0;
}

sample.c

 

빌드

  • # gcc -o ./sample ./sample.c -ljansson

  • gcc명령어로 sample이라는 실행파일이 만들어졌는지 확인한다.
    • 만약 library를 찾지 못하는 error가 발생한다면 아래 명령어를 실행시켜 공유 라이브러리 캐시를 다시 설정한다.
      • # sudo ldconfig

 

실행

  • 소스코드 실행결과 확인

 

API reference

https://jansson.readthedocs.io/en/2.13/apiref.html

 

API Reference — Jansson 2.13.1 documentation

Library Version The Jansson version is of the form A.B.C, where A is the major version, B is the minor version and C is the micro version. If the micro version is zero, it’s omitted from the version string, i.e. the version string is just A.B. When a new

jansson.readthedocs.io

 

'기타 > etc' 카테고리의 다른 글

json-c 라이브러리  (0) 2020.11.23
mysql root password 설정방법  (0) 2020.06.12
윈도우10에서 환경변수 등록방법  (0) 2020.05.29

+ Recent posts