LUA calls the C,c++ function case

Source: Internet
Author: User

This program is primarily an example of a mutual invocation between C + + and Lua.
What to do:
(1) Create a new lua_state
(2) Open common library, such as io,os,table,string, etc.
(3) Register C function
(4) All *.lua files in the directory where the importer is located
(5) calling the Mainentry () function in Lua

We may add our new C function to the Lua_functions.cpp file, combining C + + with Lua as a more powerful program.

After downloading to the local, change the Makefile.txt to makefile, then make it.

Problem: As if Mainentry () is called in Main (), the incoming argv is in Mainentry (...). is not in Arg in.
? 1. [File] main.cpp ~
#include <iostream>
#include <vector>

#include <lua.hpp>
#include "type.h"
#include "util.h"

using namespace Std;

extern lua_register_t lua_cfunction_list[];

void Open_all_libs (Lua_state *l)
{
Luaopen_base (L);
Luaopen_io (L);
Luaopen_os (L);
Luaopen_string (L);
Luaopen_table (L);
Luaopen_math (L);
Luaopen_bit32 (L);
}

void Register_functions (Lua_state *l)
{
lua_register_t *p = lua_cfunction_list;
while (P->name) {
Lua_pushcfunction (L, P->pfunc);
Lua_setglobal (L, p->name);
++p;
}
}

BOOL Load_lua_files (lua_state *l, const char *dir_path)
{
Vector<string> file_list;
Find_files ("./", ". Lua", file_list);
Vector<string>::iterator iter = File_list.begin ();
for (; ITER! = File_list.end (); ++iter) {
if (Lual_dofile (L, Iter->c_str ())) {
cout << "Err:loadfile" << *iter << "fail:" << lua_tostring (L,-1) << Endl;
return false;
}
}
return true;
}

int main (int argc, char **argv)
{
Lua_state *l = Lual_newstate ();
if (L = = NULL) {
cout << "err:new State fail!" << Endl;
return 0;
}

Open all Lua library, such as:io,os,string,table,math ...
Open_all_libs (L);

Register C functions to Lua
Register_functions (L);

Load Lua file in./directory.
Load_lua_files (L, "./");

Run Lua function "Mainentry ()"
Lua_getglobal (L, "mainentry");
if (Lua_type (L,-1) = = lua_tfunction) {
for (int i = 0; i < argc; ++i) {
Lua_pushstring (L, argv[i]);
}
Lua_pcall (L, argc, 0, 0);
} else {
cout << "Err:can" t find function \ "Mainentry\" "<< Endl;
}

return 0;
}
2. [File] Lua_functions.cpp ~
#include <iostream>
#include <lua.hpp>
#include "type.h"

using namespace Std;

/**
* Define all C LUA functions below
*/
int L_hello (lua_state *l)
{
cout << "hello! This is C function. "<< Endl;
}

Add function and name in below table.
lua_register_t Lua_cfunction_list [] = {
"Hello", L_hello,
Null
};
3. [File] type.h ~
#ifndef __lua_type_h__
#define __lua_type_h__

#include <lua.hpp>

struct lua_register_t {
const char *name;
Lua_cfunction Pfunc;
};

#endif//__lua_type_h__
4. [File] util.h ~
#ifndef __util_h__
#define __util_h__

#include <vector>
#include <string>

using namespace Std;

extern void Find_files (const string &dir_path, const string &part_name, vector<string> &file_list);

#endif//__util_h__
5. [File] Util.cpp ~
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

#include "util.h"

void Find_files (const string &dir_path, const string &part_name, vector<string> &file_list)
{http://www.enterdesk.com/special/shouhui/?
DIR *dir = Opendir (Dir_path.c_str ());
if (dir) {freehand picture
struct stat statbuf;
struct Dirent *entry = NULL;

while ((Entry = Readdir (dir)) = NULL) {
String Entry_name (Entry->d_name);

Lstat (Entry->d_name, &statbuf);
if (S_isdir (Statbuf.st_mode)) {
if (Entry_name = = "." | | entry_name = = "..")
Continue
Find_files (Dir_path + entry_name + '/', Part_name, file_list);
}

size_t pos = Entry_name.find (part_name);
if (pos! = Std::string::npos) {
String file_full_name = Dir_path + entry_name;
File_list.push_back (File_full_name);
}
}
Closedir (dir);
}
}
6. [File] Makefile.txt ~

Target = Run_lua
Lib = LUA_CFUNC.A

CC = g++
AR = AR

ALL:LUA_FUNCTIONS.O Lib
$ (CC)-O $ (Target)-lm LUA_FUNCTIONS.O $ (Lib)/USR/LOCAL/LIB/LIBLUA.A

LIB:MAIN.O UTIL.O
$ (AR) CRV $ (Lib) MAIN.O UTIL.O

%.cpp:%o
$ (CC)-C $<

Main.o:main.cpp type.h util.h
Util.o:util.cpp util.h
Lua_functions.o:lua_functions.cpp type.h

Clean
RM *.O
7. [File] Main.lua ~ 181B
Print ("Loading files ...");

function Mainentry (...)
Print ("This is Mainentry ()")
Hello ()
For I, V in Ipairs (ARG) does
Print (I.. "=" .. ToString (v))
End
End
? 8. Code

[README]

Main.cpp-The main function file, in main () contains the import library, register C functions, import LUA file operations, and finally call the Mainentray () function.
Util.h Util.cpp--Defines the find_files () function, which is to find the file name under the specified directory and return vector<string>
Type.h--Define Lua_cfunction_table function registration list
Lua_functions.cpp-Define the C function, add a new C function library to this file, do not need to change the other.

Run_lua--Compile the generated executable file
--Executes the file, registers all C functions in the lua_functions.cpp, recursively loads all *.lua files in the current directory, calls Mainentry ()
?

LUA calls the C,c++ function case

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.