C呼び出しLua--簡単な解釈器プログラム実装

1996 ワード

C呼び出しLua–簡単な解釈プログラム実装
  • CはLuaの簡単な解釈器プログラムを呼び出して
  • を実現する.
    November 5, 2015 10:57 PM
    *Luaプログラム設計第2版*ch 24の例示的なプログラムを模倣し、Lua 5.3.1バージョンでgccコンパイルを利用してこのコードを実行することに成功した.
    まずソースプログラムは以下の通りです.
    #include <stdio.h>
    #include <string.h>
    #include "lua.h"
    #include "lauxlib.h"
    #include "lualib.h"
    
    int main(void){
        char buff[256];
        int error;
        lua_State *L = luaL_newstate();             /*opens lua*/
        luaL_openlibs(L);                       /*opens the standard libraries*/
    
        while(fgets(buff, sizeof(buff), stdin) != NULL) {
            error = luaL_loadstring(L, buff) || lua_pcall(L, 0, 0, 0);
            if(error) {
                fprintf(stderr, "%s
    "
    , lua_tostring(L, -1)); lua_pop(L, 1); /* pop error message from the stack */ } } lua_close(L); return 0; }

    コンパイルリンクの段階で多くの問題が発生し、常にundefined reference to ...のエラーが発生します.最終的にgcc 24-1.c -o 24-1.exe -llua -lm -ldlコマンドを使用してコンパイルに成功し、実行プログラムはLua環境に成功した.-llua-lm-ldlのオプションの役割については、自分でgoogleしてください.