Last active
August 21, 2020 14:41
-
-
Save ultimateprogramer/031b61c7f14c31b2d821b3bd2c6729d5 to your computer and use it in GitHub Desktop.
Example on how to implement Lua functions that run C Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| gcc -Wall -shared -fPIC -o power.so -I/usr/include/lua5.1 -llua5.1 hellofunc.c | |
| # Notes: | |
| # gcc | |
| # The compiler | |
| # -Wall | |
| # Warn on almost anything | |
| # -shared | |
| # Compile to .so format, and don't error out just because there's no main() | |
| # -fPIC | |
| # Goes with -shared to tell the executable format. Subtle errors can sometimes creep in if you don't do this. | |
| # -o power.so | |
| # Name the shared library power.so | |
| # -I/usr/include/lua5.1 | |
| # Look for lua.h etc in /usr/include/lua5.1. Note this will be different on every system. | |
| # -llua5.1 | |
| # Compile with the Lua library. Note this will be different on every system. See here for how you deduce the -l argument. | |
| # hellofunc.c | |
| # The source file to the power library. It doesn't have to be named "power". | |
| # How it worked on my Ubuntu with Lua 5.4: | |
| # $ gcc -Wall -shared -fPIC -o power.so -I/usr/local/include/lua5.4 -llua hellofunc.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* hellofunc.c (C) 2011 by Steve Litt | |
| * gcc -Wall -shared -fPIC -o power.so -I/usr/include/lua5.1 -llua5.1 hellofunc.c | |
| * Note the word "power" matches the string after the underscore in | |
| * function luaopen_power(). This is a must. | |
| * The -shared arg lets it compile to .so format. | |
| * The -fPIC is for certain situations and harmless in others. | |
| * On your computer, the -I and -l args will probably be different. | |
| */ | |
| #include <lua.h> /* Always include this */ | |
| #include <lauxlib.h> /* Always include this */ | |
| #include <lualib.h> /* Always include this */ | |
| static int isquare(lua_State *L){ /* Internal name of func */ | |
| float rtrn = lua_tonumber(L, -1); /* Get the single number arg */ | |
| printf("Top of square(), nbr=%f\n",rtrn); | |
| lua_pushnumber(L,rtrn*rtrn); /* Push the return */ | |
| return 1; /* One return value */ | |
| } | |
| static int icube(lua_State *L){ /* Internal name of func */ | |
| float rtrn = lua_tonumber(L, -1); /* Get the single number arg */ | |
| printf("Top of cube(), number=%f\n",rtrn); | |
| lua_pushnumber(L,rtrn*rtrn*rtrn); /* Push the return */ | |
| return 1; /* One return value */ | |
| } | |
| /* Register this file's functions with the | |
| * luaopen_libraryname() function, where libraryname | |
| * is the name of the compiled .so output. In other words | |
| * it's the filename (but not extension) after the -o | |
| * in the cc command. | |
| * | |
| * So for instance, if your cc command has -o power.so then | |
| * this function would be called luaopen_power(). | |
| * | |
| * This function should contain lua_register() commands for | |
| * each function you want available from Lua. | |
| * | |
| */ | |
| int luaopen_power(lua_State *L){ | |
| lua_register( | |
| L, /* Lua state variable */ | |
| "square", /* func name as known in Lua */ | |
| isquare /* func name in this file */ | |
| ); | |
| lua_register(L,"cube",icube); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require("power") | |
| -- Test the 2 functions | |
| print(square(1.414213598)) | |
| print(cube(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment