본문 바로가기

프로그램언어/Lua

C++ 함수를 lua에서 호출하는 방법

  첨부파일 (1)

출처 컴포넌트.. | 손병욱
원문 http://blog.naver.com/gsi451/20032951999

C++의 함수를 lua에서 호출하는 예제 입니다.


///////////////////////////////////////////////////////////////////////////////////

제가 해봤던 테스트 코드를 우선 올려 봅니다.

실행하면 아래와 같은 내용이 나오면 정상입니다. ^^


 


///////////////////////////////////////////////////////////////////////////////////

출처 /본문 아래와 같습니다.


Calling C++ Functions from Lua Lua

2005/07/06 14:00

http://blog.naver.com/juney/14717024

Calling C++ Functions from Lua

My second tutorial dealt with calling Lua functions from C++. This one deals with just the opposite situation - calling C++ functions from Lua. I couldn't think of a simple example that I was happy with, so I borrowed the average function from the official Lua documentation.

In this tutorial we will create a function in C++, tell the Lua interpreter about it, and finally call it from Lua and use the results. I will also talk a little about error checking in Lua programs.

Defining the function

The first step is to define the function. All C or C++ functions that will be called from Lua will be called using a pointer of this type:

typedef int (*lua_CFunction) (lua_State *L);

In other words, functions must have a Lua interpreter as the only argument and return only an integer. Since a Lua interpreter is used for the argument, the function can actually take any number of arguments that are read from the stack. The integer returned is actually the number of values that have been pushed on the stack as we will see later. If you have existing C++ functions that you would like to call from Lua, wrappers can easily be created to meet this requirement.

The C++ average() function given below will demonstrate accepting multiple arguments and returning more than one value. Note that it's definition matches the typedef given above.

  1. The lua_gettop() function returns the index of the top of the stack. Since the stack is numbered starting from 1 in Lua, this is also the size of the stack and can be used as the number of arguments passed to the function.
  2. The for loop calculates the sum all of the arguments.
  3. The average of the arguments is pushed onto the stack with lua_pushnumber().
  4. Then the sum of the arguments is pushed onto the stack.
  5. Finally, the function returns 2, indicating two return values have been pushed to the stack.

Now that this function has been defined, we must tell the Lua interpreter about it. This is done in the main() function, just after the Lua interpreter is initialized and the libraries are loaded:

	/* register our function */
	lua_register(L, "average", average);

Save this file as luaavg.cpp. If you'd like to use straight C instead of C++, just name the file luaavg.c and remove the extern "C".

#include <stdio.h>

extern "C" {
	#include "lua.h"
	#include "lualib.h"
	#include "lauxlib.h"
}

/* the Lua interpreter */
lua_State* L;

static int average(lua_State *L)
{
	/* get number of arguments */
	int n = lua_gettop(L);
	double sum = 0;
	int i;

	/* loop through each argument */
	for (i = 1; i <= n; i++)
	{
		/* total the arguments */
		sum += lua_tonumber(L, i);
	}

	/* push the average */
	lua_pushnumber(L, sum / n);

	/* push the sum */
	lua_pushnumber(L, sum);

	/* return the number of results */
	return 2;
}

int main ( int argc, char *argv[] )
{
	/* initialize Lua */
	L = lua_open();

	/* load Lua base libraries */
	lua_baselibopen(L);

	/* register our function */
	lua_register(L, "average", average);

	/* run the script */
	lua_dofile(L, "avg.lua");

	/* cleanup Lua */
	lua_close(L);

	return 0;
}

This simple Lua script calls the function with five arguments and prints both of the returned values. Save this as avg.lua

-- call a C++ function

avg, sum = average(10, 20, 30, 40, 50)

print("The average is ", avg)
print("The sum is ", sum)

Compiling

On Linux you can compile this program by typing this command:

g++ luaavg.cpp -llua -llualib -o luaavg

Then run the program by typing:

./luaavg

If everything worked correctly, the program should print the average and sum of the numbers.

In Visual C++ you'll need to follow these steps:

  1. Create a new Win32 Console Application Project.
  2. Add the "luaavg.cpp" file to your project.
  3. Go to Project, Settings and click the Link tab.
  4. Add lua+lib.lib to the list of Object/library modules.
  5. At this point, you should be able to press F7 to Build the program.

Before we can run the program, you'll need to put the "lua+lib.dll" file where Windows can find it. Copy the file from "C:\Program Files\Lua-5.0" to your new Visual C++ project's folder. If your program compiled without errors, you can now press Ctrl+F5 to execute the program.

Error Handling

If you've read the Lua API documentation, you know that I left the error checking out of my version of the average function above. This was done to make it easier to explain, but in a real program you will want to do some error tests. In the example above, we should at least check each argument to make sure that it is a number. This can be done by adding the following piece of code inside the for loop:

if (!lua_isnumber(L, i)) {
	lua_pushstring(L, "Incorrect argument to 'average'");
	lua_error(L);
}

Checks like this are easy to add and can really make debugging easier. This is very important when dealing with a program written in two different languages. You'll find yourself debugging C++ and Lua.

Downloads

  • luaavg.zip - Tutorial Source and Project files for Visual C++ 6 on Windows.
  • luaavg.tar.gz - Tutorial Source and Makefile for Linux.

Older Versions

Here are the files for Lua 4.0.

  • luaavg-4.zip - Tutorial Source and Project files for Visual C++ 6 on Windows.
  • luaavg-4.tar.gz - Tutorial Source and Makefile for Linux.

You should now be able to set up Lua on your computer, call Lua functions from C++, and call C++ functions from Lua. This will probably be the last Lua tutorial for now. One of these days I'll come up with a more complicated program that ties all of these ideas together. Until then, feel free to experiment on your own and let me know what you come up with.


출처 : http://tonyandpaige.com/tutorials/lua3.html

 

'프로그램언어 > Lua' 카테고리의 다른 글

Lua 배열을 손쉽게  (0) 2008.07.22
LuaApi  (0) 2008.07.22
Lua 기본 명령어  (0) 2008.07.22