lua 调用C/C++

/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2012/06/26
* File: demo.cpp
* Blog: http://www.cnblogs.com/zengqh/
**/


#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <lua.hpp>


int hello1(lua_State* L)
{
    printf("hello1\n");

    return 0;
}

int hello2(lua_State* L)
{
    printf("hello2\n");

    return 0;
}

static const luaL_Reg functions[] =
{
    {"hello1", hello1},
    {"hello2", hello2}
};

int main()
{
    lua_State * L = lua_open();
    luaL_openlibs(L);

    /* love table */
    lua_newtable(L);
    lua_pushvalue(L, -1);
    lua_setglobal(L, "love");

    lua_newtable(L);

    luaL_register(L, 0, functions);

    lua_pushvalue(L, -1);

    lua_setfield(L, -3, "graphics");

    /* empty the stack */
    lua_pop(L, 2);

    if(luaL_loadfile(L, "test.lua") == 0)
    {
        lua_call(L, 0, 0);
    }    

    system("pause");
}

test.lua:

function print_hello1()
    love.graphics.hello1()
end

function print_hello2()
    love.graphics.hello2()
end

print_hello1()
print_hello2()

output:

hello1

hello2
原文链接: https://www.cnblogs.com/zengqh/archive/2012/06/26/2564405.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/53510

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午4:52
下一篇 2023年2月9日 上午4:52

相关推荐