2022年5月27日 星期五

[Linux] Makefile Function

define funcName
@echo "pram1 = $(0)"
@echo "pram2 = $(1)"
@echo "pram3 = $(2)"
endef

all:
$(call funcName,hello 1,hello 2)


2022年5月2日 星期一

[C] JSON Library

安裝JSON函式庫(操作環境 ubuntu)
sudo apt install libjson-c-dev


範例1
int main()
{
	json_object *retObj_1 = NULL, *retObj_2 = NULL, *json_obj = NULL;
	int val_1, val_2;

	json_obj = json_object_new_object();

	/*Add object*/
	json_object_object_add(json_obj, "item1", json_object_new_int(123));
	json_object_object_add(json_obj, "item2", json_object_new_int(456));

	/*讀出json_obj內的所有key and value*/
	printf("%s\n", json_object_to_json_string(json_obj)); 
	//-> { "item1": 123, "item2": 456 }

	/*Get object, 下列兩種取到的object是一樣的*/
	retObj_1 = json_object_object_get(json_obj, "item1"); 	
	json_object_object_get_ex(json_obj, "item1", &retObj_2);
	//if retObj_1 = 0, item1 isn't existing

	val_1 = json_object_get_int (retObj_1);
	val_2 = json_object_get_int (retObj_2);
	printf(%d\n", json_object_get_int(val_1)); //-> 123
	printf("%d\n", json_object_get_int(val_2)); //-> 123
}