zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Hive 使用问题集锦
    scala def/val/lazy val区别以及call-by-name和call-by-value
    spark学习流程
    Hadoop
    Hive
    Chrome快捷键
    Java API帮助文档
    Java 访问修饰符与非访问修饰符
    java 关键字
    Solr配置Ikanalyzer分词器
  • 原文地址:https://www.cnblogs.com/zengqh/p/2564405.html
Copyright © 2011-2022 走看看