In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to call C code in LUA. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Compile the C code into a DLL file and let the LUA code call:
/ / mytestlib.cpp#include # include / / the C function to be registered, the declared form of which is given in the above example. / / it is important to note that the function must be exported as C, so extern "C" is required. / / the function code is the same as the previous example, so I won't repeat it here. Extern "C" int add (lua_State* L) {double op1 = luaL_checknumber (LMagne1); double op2 = luaL_checknumber (LMagne2); lua_pushnumber (LMagneOp1 + op2); return 1;} extern "C" int sub (lua_State* L) {double op1 = luaL_checknumber (LMagne1); double op2 = luaL_checknumber (LMagne2); lua_pushnumber (LMagneop1-op2); return 1 The first field of the} / / luaL_Reg structure is a string that is used to notify Lua of the name of the function when it is registered. / / the first field is a C function pointer. / / both fields of the last element in the structure array are NULL, indicating that the Lua registration function has reached the end of the array. Static luaL_Reg mylibs [] = {{"add", add}, {"sub", sub}, {NULL, NULL}}; / / the only entry function of the C library. Its function signature is equivalent to the registration function above. See the following notes: / / 1. We can simply understand this function as the factory function of the module. / / 2. Its function name must be luaopen_xxx, where xxx represents the library name. The Lua code require "xxx" needs to correspond to it. / / 3. In the call to luaL_register, the first string argument is the module name "xxx" and the second argument is the array of functions to be registered. / / 4. It is important to emphasize that all code that needs to use "xxx", whether C or Lua, must be consistent, which is the convention of Lua, / / otherwise it will not be called. Extern "C" _ declspec (dllexport) int luaopen_mytestlib (lua_State* L) {const char* libName = "mytestlib"; luaL_register; return 1;}
Taking VS 2012 as an example, this paper introduces the solutions to minor problems encountered in the process of compilation:
1. Header file and library reference settings:
Set the above configuration item to point to the inlucde and lib subdirectories of the LUA installation directory.
Then set the link library name so that you can compile normally.
2. How to configure the C code corresponding to the DLL storage path if it is not in the default search path of LUA.
Specified by the environment variable LUA_CPATH, but must be the full path containing the file name.
Add: how to call C code through LUA under Linux
Http://blog.csdn.net/wdlove58/article/details/52191248
I'm taking lua5.1 as an example.
Installation of Lua development environment under Ubuntu:
/ / install lua running environment and development environment sudo apt-get install luasudo apt-get install lua5.1-0-dev
Check the library file installation path:
Locate liblua
Edit the mytestlib.c file
# include # include int add (lua_State* L) / / removed the extern "C" from the original code, followed by the similar removal of {double op1 = luaL_checknumber (L, 1); double op2 = luaL_checknumber (L, 2); lua_pushnumber (L, op1 + op2); return 1;} int sub (lua_State* L) {double op1 = luaL_checknumber (L, 1) Double op2 = luaL_checknumber (L, 2); lua_pushnumber (L, op1-op2); return 1;} / / the first field of the luaL_Reg structure is a string that is used to notify Lua of the name of the function when registered. / / the first field is a C function pointer. / / both fields of the last element in the structure array are NULL, indicating that the Lua registration function has reached the end of the array. Static luaL_Reg mylibs [] = {{"add", add}, {"sub", sub}, {NULL, NULL}}; / / the only entry function of the C library. Its function signature is equivalent to the registration function above. See the following notes: / / 1. We can simply understand this function as the factory function of the module. / / 2. Its function name must be luaopen_xxx, where xxx represents the library name. The Lua code require "xxx" needs to correspond to it. / / 3. In the call to luaL_setfuncs, the second argument is the array of functions to be registered. / / 4. It is important to emphasize that all code that needs to use "xxx", whether C or Lua, must be consistent, which is the convention of Lua, / / otherwise it will not be called. Int luaopen_mytestlib (lua_State* L) {const char* libName = "mytestlib"; luaL_register (L, libName, mylibs); / / since the function luaL_register is no longer available in lua-5.3, replace it with the following two lines of code / / lua_newtable (L); / / luaL_setfuncs (L, mylibs, 0); return 1;}
Compile directives:
Gcc mytestlib.c-fPIC-shared-I/usr/include/lua5.1/-L/usr/lib/x86_64-linux-gnu/-llua5.1-o mytestlib.so-Wall
The a.lua file is as follows:
Local mylib = require ("mytestlib")-corresponds to the package name print (mylib.add (1.0)) print (mylib.sub (20.1) in teste.c)
The result of executing a.lua is as follows:
This is the end of xxxx@ubuntu:~/work_space/lua_call_c$ lua a.lua31.1 's article on "how to call C code in LUA". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.