1.简介
最近学习lua时,想编写一个C的共享库,然后在lua中去调用相关函数,于是我在CLion中创建了一个C共享库的工程,代码如下:
library.h
#ifndef LUA_1_LIBRARY_H
#define LUA_1_LIBRARY_H
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static int L_Add(lua_State *L);
int luaopen_libAdd(lua_State *L);
#endif
library.c
#include "library.h"
static int L_Add(lua_State *L) {
int n = lua_gettop(L);
if(n < 2)
return 0;
long a = luaL_checkinteger(L, 1);
long b = luaL_checkinteger(L, 2);
lua_pushnumber(L, a+b);
return 1;
}
static

本文介绍了如何在CLion中创建C共享库,并在Lua5.3中调用。编译lua5.3时遇到错误,通过添加-fPIC选项解决。在lua中使用C共享库时,发现lua缺少动态库支持,经过重新编译lua,最终成功调用C库。
最低0.47元/天 解锁文章
4227

被折叠的 条评论
为什么被折叠?



