文章目录
- 一、环境搭建脚本
- toolChain_jsonc.cmake
- toolChain_libubox.cmake
- toolChain_ubus.cmake
- install.sh
- 二、测试
- 出现问题:
- 三、测试uloop
- main.c 每5s打印信息
一、环境搭建脚本
准备四个文件
install.sh
,toolChain_jsonc.cmake
,toolChain_libubox.cmake
,toolChain_ubus.cmake
toolChain_jsonc.cmake
set(CMAKE_SYSTEM_NAME Linux)
SET(TOOLCHAIN_DIR "/usr")
set(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/g++)
set(CMAKE_FIND_ROOT_PATH "/home/yyh/ubus_libs/json-c")
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
toolChain_libubox.cmake
set(CMAKE_SYSTEM_NAME Linux)
SET(TOOLCHAIN_DIR "/usr")
set(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/g++)
set(INSTALL_PATH "/home/yyh/ubus_libs/install_build_ubuntu")
include_directories(${INSTALL_PATH}/include)
include_directories(${INSTALL_PATH}/include/json-c)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH "/home/yyh/ubus_libs/ubox") # or libubox
set(json "/home/yyh/ubus_libs/install_build_ubuntu/lib/libjson-c.so")
toolChain_ubus.cmake
set(CMAKE_SYSTEM_NAME Linux)
SET(TOOLCHAIN_DIR "/usr")
set(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/g++)
set(INSTALL_PATH "/home/yyh/ubus_libs/install_build_ubuntu")
include_directories(${INSTALL_PATH}/include)
set(CMAKE_FIND_ROOT_PATH "/home/yyh/ubus_libs/ubus" $(INSTALL_PATH))
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(blob_library ${INSTALL_PATH}/lib/libblobmsg_json.so)
set(json ${INSTALL_PATH}/lib/libjson-c.so)
set(ubox_include_dir ${INSTALL_PATH}/include/libubox)
set(ubox_library ${INSTALL_PATH}/lib/libubox.so)
install.sh
#!/bin/bash
# @Author: yyh
# @Date: 2023-03-08 13:32:27
# @Last Modified by: yyh
# @Last Modified time: 2023-03-08 17:09:22
LIBS_DIR=/home/yyh/ubus_libs
LIB_LIBUBOX_DIR=libubox
LIB_UBUS_DIR=ubus
LIB_JSONC_DIR=json-c
COMPILE_EV=install_build_ubuntu
if [[ $1 = "git_libs" ]]; then
mkdir $LIBS_DIR -p
cd $LIBS_DIR
git clone https://github.com/json-c/json-c.git
git clone http://git.openwrt.org/project/libubox.git
git clone https://git.openwrt.org/project/ubus.git
fi
if [[ $1 = "compile_lib" ]]; then
cp *.cmake $LIBS_DIR
cd $LIBS_DIR
mkdir ${LIBS_DIR}/${COMPILE_EV} -p
cd ${LIBS_DIR}/$LIB_JSONC_DIR
cmake -DCMAKE_INSTALL_PREFIX=${LIBS_DIR}/${COMPILE_EV} -DCMAKE_TOOLCHAIN_FILE=${LIBS_DIR}/toolChain_jsonc.cmake .
# cmake -DCMAKE_INSTALL_PREFIX=/home/yyh/ubus_libs/install_build_ubuntu -DCMAKE_TOOLCHAIN_FILE=/home/yyh/ubus_libs/toolChain_jsonc.cmake .
make
sudo make install
sudo ldconfig -v
cd ${LIBS_DIR}/$LIB_LIBUBOX_DIR
cmake -DBUILD_LUA=OFF -DCMAKE_INSTALL_PREFIX=${LIBS_DIR}/${COMPILE_EV} -DCMAKE_TOOLCHAIN_FILE=${LIBS_DIR}/toolChain_libubox.cmake .
# cmake -DCMAKE_INSTALL_PREFIX=/home/yyh/ubus_libs/install_build_ubuntu -DCMAKE_TOOLCHAIN_FILE=/home/yyh/ubus_libs/toolChain_libubox.cmake .
make
sudo make install
cd ${LIBS_DIR}/$LIB_UBUS_DIR
cmake -DBUILD_LUA=OFF -DCMAKE_INSTALL_PREFIX=${LIBS_DIR}/${COMPILE_EV} -DCMAKE_TOOLCHAIN_FILE=${LIBS_DIR}/toolChain_ubus.cmake .
#cmake -DCMAKE_INSTALL_PREFIX=/home/yyh/ubus_libs/install_build_ubuntu -DCMAKE_TOOLCHAIN_FILE=/home/yyh/ubus_libs/toolChain_ubus.cmake .
make
sudo make install
fi
二、测试
安装相关库
sudo apt-get install libjsoncpp-dev
sudo apt-get install zmap
sudo apt-get install autoconf automake libtool
sudo apt install lua5.1
chmod +x ./install.sh
./install.sh git_libs
./install compile_lib
出现问题:
/home/yyh/ubus_libs/libubox/blobmsg_json.c:23:11: fatal error: json/json.h: 没有那个文件或目录
23 | #include <json/json.h>
| ^~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/blobmsg_json.dir/build.make:63:CMakeFiles/blobmsg_json.dir/blobmsg_json.c.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:211:CMakeFiles/blobmsg_json.dir/all] 错误 2
make: *** [Makefile:130:all] 错误 2
解决:
sudo cp /home/yyh/ubus_libs/install_build_ubuntu/include/json-c /home/yyh/ubus_libs/install_build_ubuntu/include/json -rf
三、测试uloop
makefile
CC = gcc
CFLAGS = -I./include -L./lib -lubox -lubus#-lblobmsg_json -ljson_script -ljson-c
TARGET = main_ubuntu
all:
$(CC) -o $(TARGET) main.c $(CFLAGS)
.PHONY:clean
clean:
rm -rf *.o $(TARGET)
main.c 每5s打印信息
/*
* @Author: yyh
* @Date: 2023-03-08 17:17:35
* @Last Modified by: yyh
* @Last Modified time: 2023-03-08 17:40:41
*/
/*
typedef void (*uloop_timeout_handler)(struct uloop_timeout *t);
struct uloop_timeout
{
struct list_head list;
bool pending;
uloop_timeout_handler cb;
struct timeval time;
};
*/
#include <stdio.h>
#include "./libubox/uloop.h"
static void timeout_handler(struct uloop_timeout *timeout)
{
printf("(hello world)\n");
}
static struct uloop_timeout timer = {
.cb = timeout_handler,
};
int main(int args, char *argv[])
{
uloop_init();
uloop_timeout_set(&timer, 5000); // 5s
uloop_run();
return 0;
}