In front of the word

I first encountered Lua because of the need for hotfix in Unity games, but I never understood how Lua was implemented in other languages and called each other. How does C interact with Lua

So how do you use Lua?

Lua is written in C and is open source. Download Lua’s source code at https://www.lua.org and try compiling it! Is it as exciting as I am, I have been using the integrated environment, writing the upper language, and today I want to touch the compilation!! How to compile ~?

Let’s summon the compiler artifact: GCC! The GNU Compiler Collection includes front ends for C, C++, objective-c, Fortran, Java, Ada, and Go, as well as libraries for these languages (e.g. Libstdc++, libgcj, etc.).

Install GCC on a Mac

If you have Homebrew installed, it should only be one line.

brew install gccCopy the code

After use

Brew info GCC or gcc-vCopy the code

Let’s see if it worked

Compile Lua

Once you have the compiler installed, compiling Lua is very simple



Lua’s official documentation says how to compile, but the default in the MakeFile is to compile into a static linked library

You are advised to install it in the /opt directory

Sudo su CD/opt curl -r -o http://www.lua.org/ftp/lua-5.3.4.tar.gz tar ZXF lua - 5.3.4. Tar. Gz CD lua - 5.3.4 make macosx test make macosx installCopy the code

Install lua -v to check if there is any information, congratulations, Lua compiled! ~

The bottom is officially dry ~

Write a C call to Lua Demo compiler run

The add. C content

#include <stdio.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" lua_State* L; int luaadd(int x, int y) { int sum; /* function name */ lua_getGlobal (L,"add"); */ lua_pushnumber(L, x); */ lua_pushnumber(L, y); /* Start calling the function with 2 arguments and 1 return value */ lua_call(L, 2, 1); Sum = (int)lua_tonumber(L, -1); sum = (int)lua_tonumber(L, -1); /* Clear the return stack */ lua_pop(L,1); return sum; } int main(int argc, char *argv[]) { int sum; L = luaL_newstate(); /* Create lua state machine */ luaL_openlibs(L); LuaL_dofile (L, "add.lua"); luaL_dofile(L, "add. Sum = luaadd(99, 10); sum = luaadd(99, 10); printf("The sum is %d \n",sum); /* Clear Lua*/ lua_close(L); return 0; }Copy the code

Add. Lua in the same directory as C, and write a simple function for C to call

function add(x,y)
       return x + y
end Copy the code

GCC add.c GCC add.c GCC add.c GCC add.c GCC add.c GCC add.c



It was wrong!

This is because add.c functions are not linked to the lua library we compiled earlier. How do I get him to specify which library to link to? See GCC’s documentation to see that the -l argument specifies the library to link to

The -l argument is used to specify the library to which the program is linked. The -l argument is followed by the library name. For example, the library name is m, and the name of the library is libm.so

GCC add.c -llua GCC add.c -llua GCC add.c -llua GCC add.c -llua



Execution successful!

How do I get Lua to call C?

Lua calls C, and I’ve learned that there are three ways

1. Call lua by registering functions in C. 2. Encapsulate the C dynamic link library, require 3 in Lua. You can use ffI high performance call C in LuaJIT (but LuaJIT is not supported on IOS).

Lua provides the lua_register function to register the C function to call the hello. C end of Lua

#include <stdio.h> #include <string.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" static int l_SayHello(lua_State *L) { const char *d = luaL_checkstring(L, 1); Int len = strlen(d); char str[100] = "hello "; strcat(str, d); lua_pushstring(L, str); /* Return the value of lua to push */ return 1; } int main(int argc, char *argv[]) { lua_State *L = luaL_newstate(); /* Create lua state machine */ luaL_openlibs(L); /* Open all Lua libraries */ lua_register(L, "SayHello", l_SayHello); // Register C function to lua const char* testFunc = "print(SayHello('lijia'))"; If (luaL_dostring(L, testfunc)); printf("Failed to invoke.\n"); /* Clear Lua*/ lua_close(L); return 0; }Copy the code

GCC -o hello hello.c -llua Compile and execute

2. Create a mylib. C file by calling the C dynamic link library, and compile it into the dynamic link library

#include <stdio.h> #include <math.h> #include <stdarg.h> #include <stdlib.h> #include <lua.h> #include <lauxlib.h> #include <lualib.h> /* All C functions registered with Lua have * "typedef int (*lua_CFunction) (lua_State *L);" The prototype. Static int l_sin(lua_State *L) {static int l_sin(lua_State *L) {static int l_sin(lua_State *L) {static int l_sin(lua_State *L) { double d = luaL_checknumber(L, 1); lua_pushnumber(L, sin(d)); /* push result */ /* C can return multiple results to Lua by calling lua_push*() multiple times, after which return returns the number of results. */ return 1; /* Number of results */} /* Requires a structure of type "luaL_Reg", where each element corresponds to a function supplied to Lua. * Each element contains the name of the function in Lua and a pointer to the function in the C library. * The last element is the "sentinel element" (two "nulls"), which tells Lua that no other functions need to be registered. */ static const struct luaL_Reg mylib[] = { {"mysin", l_sin}, {NULL, NULL} }; /* This function is a special function in the C library. * Register all functions in the C library by calling it and store them in the appropriate locations. * This function should be named as follows: * 1. Use "luaopen_" as the prefix. * 2. The name after the prefix will be used as an argument to "require". */ extern int luaopen_mylib(lua_State* L) { /* void luaL_newlib (lua_State *L, const luaL_Reg l[]); * Create a new "table" and register the functions listed in "L" as fields of "table". */ luaL_newlib(L, mylib); return 1; }Copy the code

Use GCC -o mylib. so-fpic-shared mylib. c-llua-LDL to compile so and then create a lua file to import our compiled C library

Luaopen_mylib (); luaopen_mylib(); luaopen_mylib(); ]] local mylib = require "mylib" -- the result is the same as in the above example, but this is done by calling a function from the C library. Print (mylib.mysin(3.14/2)) --> print(mylib.mysin(3.14/2)) --Copy the code

Lua has multiple virtual machines. Lua has multiple virtual machines.

lua: multiple Lua VMs detected

Why is that? I did some research and found that because Lua builds statically linked libraries by default, this can cause multiple linked VMS to conflict. So let’s compile a Lua interpreter ourselves and dynamically link it. mylua.c

#include <stdio.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" int main() { lua_State *L = luaL_newstate(); luaL_openlibs(L); if (luaL_loadfile(L, "a.lua") || lua_pcall(L, 0, 0, 0)) { printf("%s", lua_tostring(L, -1)); }}Copy the code

GCC -o mylua mylua.c -llua-ldL-lM-wall

conclusion

The GCC command, compiling Lua, compiling the C dynamic link library, these are relatively few contacts before. So I climbed a lot of pits, hahaha. How to parse binary protocol calls to Lua in C? How to encapsulate Luatable in C

Reference data: www.cnblogs.com/pied/archiv… Blog.csdn.net/vermilliont… Blog.csdn.net/casularm/ar…

www.cnblogs.com/lijiajia