700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 如何将C++代码封装成C模块 适配Python Lua和C++调用。多种语言(C++ python和lua)

如何将C++代码封装成C模块 适配Python Lua和C++调用。多种语言(C++ python和lua)

时间:2024-02-10 23:55:21

相关推荐

如何将C++代码封装成C模块 适配Python Lua和C++调用。多种语言(C++ python和lua)

概述

示例程序仓库路径:/liudegui/test_dogservice

示例程序是对看门狗模块的封装,已在windows平台测试,代码理论上可直接支持Linux平台。C++可直接调用C模块库;Python通过cffi调用C模块库;Lua通过ffi调用C模块库;关于cffi和ffi资料请自行搜索。另外,Linux平台下cffi和ffi加载的是.so库;

封装对外的C库头文件dogservice_api.h

#pragma once#if (!defined(ROCK_LIB) && !defined(ROCK_LINUX_PLATFORM))// ROCK is used as DLL#define ROCK_DLLEXPORT __declspec(dllexport) #define ROCK_DLLIMPORT __declspec(dllimport) #else #define ROCK_DLLEXPORT #define ROCK_DLLIMPORT#endif#ifdef ROCK_DOG_EXPORTS#define ROCK_DOG_API ROCK_DLLEXPORT #else #define ROCK_DOG_API ROCK_DLLIMPORT #endif#ifdef __cplusplus extern "C" {#endifint ROCK_DOG_API dogStart();void ROCK_DOG_API dogStop();int ROCK_DOG_API getDogMemoryInfo(char* buf, int bufsize);void ROCK_DOG_API checkDogExpireDate(void(*cb)(int));int ROCK_DOG_API dogExpireDateStatus();void ROCK_DOG_API setDogTimerInterval(int interval, int expDateinterval);int ROCK_DOG_API dogStatus();void ROCK_DOG_API checkDogStatus(void(*cb)(int));int ROCK_DOG_API dogCheckExpDateFlag();#ifdef __cplusplus}#endif

C++调用C库dogservice_api.h示例

#include <iostream>#include "dogservice_api.h"#include "loghelper.h"using namespace RockLog;static void updateExpireDateStatusCallBack(int status){LOG(kDebug) << "The superdog expire date status: " << status << std::endl;if (status != 0){LOG(kErr) << "The superdog have arrived expire date! Status: " << status << std::endl;}}static void updateSessionStatusCallBack(int status){LOG(kDebug) << "dog status: " << status << std::endl;if (status != 0){LOG(kErr) << "dog status error! status: " << status << std::endl;}}int main(int argc, char *argv[]){setDogTimerInterval(3, 30);dogStart();checkDogExpireDate(updateExpireDateStatusCallBack);checkDogStatus(updateSessionStatusCallBack);char buf[1024] = {0 };if (0 == getDogMemoryInfo(buf, 1024))LOG(kDebug) << "dogMemoryInfo: " << buf << std::endl;LOG(kDebug) << "dogExpireDateStatus: " << dogExpireDateStatus() << std::endl;LOG(kDebug) << "dogCheckExpDateFlag: " << dogCheckExpDateFlag() << std::endl;LOG(kDebug) << "dogStatus: " << dogStatus() << std::endl;getchar();dogStop();return 0;}

python调用C库dogservice_api.h的示例

# ref: /ccxikka/p/9637545.html# ref: /WJUNSING/article/details/81126970from cffi import FFIimport osimport sysimport loggingimport timeimport utilityffi = FFI()ffi.cdef("""int dogStart();void dogStop();int getDogMemoryInfo(char* buf, int bufsize);void checkDogExpireDate(void(*cb)(int));int dogExpireDateStatus();void setDogTimerInterval(int interval, int expDateinterval);int dogStatus();void checkDogStatus(void(*cb)(int));int dogCheckExpDateFlag();""")LIB_NAME = 'dogservice.dll'@ffi.callback("void(int)")def updateExpireDateStatusCallBack(status):logging.debug("The superdog expire date status: %s", status) if status != 0:logging.error("The superdog have arrived expire date! Status: %s", status)@ffi.callback("void(int)")def updateSessionStatusCallBack(status):logging.debug("dog status: %s", status) if status != 0:logging.error("dog status error! status: %s", status)###########################class DogService():def __init__(self):self.is_running = None# 系统已正常工作self.lib = Nonedef load_library(self):lib_path = LIB_NAMEif os.path.exists(lib_path):self.lib = ffi.dlopen(lib_path)return Trueelse:lib_path = utility.search_file(".", lib_path)if lib_path:self.lib = ffi.dlopen(lib_path)return Trueelse:return Falsereturn Falsedef start(self):try:logging.debug("Start begin")# 加载动态链接库if not self.load_library():logging.info("load dogservice library %s failed", LIB_NAME)self.is_running = Falsereturnself.lib.setDogTimerInterval(3, 30)self.lib.dogStart()self.lib.checkDogExpireDate(updateExpireDateStatusCallBack)self.lib.checkDogStatus(updateSessionStatusCallBack)buf = ffi.new("char[]", 1024)if 0 == self.lib.getDogMemoryInfo(buf, 1024):logging.info("dogMemoryInfo: %s", ffi.string(buf)) logging.info("dogExpireDateStatus: %s", self.lib.dogExpireDateStatus())logging.info("dogCheckExpDateFlag: %s", self.lib.dogCheckExpDateFlag())logging.info("dogStatus: %s", self.lib.dogStatus())self.is_running = Trueexcept:logging.error("Start failed!!!!")self.is_running = Falsedef stop(self):self.is_running = Falseself.lib.dogStop()if __name__ == "__main__":utility.initLogging("dogservice.log")dogService = DogService()dogService.start()while dogService.is_running != False:time.sleep(10)dogService.stop()sys.exit(0)logging.error("app exit!")

Lua调用C库dogservice_api.h的示例

local ffi = require("ffi")ffi.cdef[[int dogStart();void dogStop();int getDogMemoryInfo(char* buf, int bufsize);void checkDogExpireDate(void(*cb)(int));int dogExpireDateStatus();void setDogTimerInterval(int interval, int expDateinterval);int dogStatus();void checkDogStatus(void(*cb)(int));int dogCheckExpDateFlag();]]local lib = ffi.load('dogservice')local function updateExpireDateStatusCallBack(status)print("The superdog expire date status:", status) if status ~= 0 thenprint("The superdog have arrived expire date! Status: ", status)endendlocal function updateSessionStatusCallBack(status)print("dog status: ", status) if status ~= 0 thenprint("dog status error! status: ", status)endendlocal function start()lib.setDogTimerInterval(3, 30)lib.dogStart()lib.checkDogExpireDate(updateExpireDateStatusCallBack)lib.checkDogStatus(updateSessionStatusCallBack)local buf = ffi.new('char[?]', 1024)if 0 == lib.getDogMemoryInfo(buf, 1024) thenprint("dogMemoryInfo: ", ffi.string(buf)) endprint("dogExpireDateStatus: ", lib.dogExpireDateStatus())print("dogCheckExpDateFlag: ", lib.dogCheckExpDateFlag())print("dogStatus: ", lib.dogStatus())endlocal function stop()lib.dogStop()endstart()io.read()

如何将C++代码封装成C模块 适配Python Lua和C++调用。多种语言(C++ python和lua)调用C++封装的看门狗sdk模块

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。