繁体   English   中英

C ++ Lua 5.1问题

[英]C++ Lua 5.1 Issue

#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 = lua_open();   /* opens Lua */
      luaL_openlibs(L);

      while (fgets(buff, sizeof(buff), stdin) != NULL) {
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
                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;
    }

这似乎传播了一些错误,例如:

错误LNK2019:无法解析的外部符号“ char const * __cdecl lua_tolstring(struct lua_State *,int,unsigned int *)”(?lua_tolstring @@ YAPBDPAUlua_State @@ HPAI @ Z)在函数_main main.obj中引用

怎么了?

您需要将lua头文件包装在extern "C"以获取正确的符号链接以及链接到lib(如果未将其编译到项目中)

Lua 5.1具有lua.hpp:

// lua.hpp
// Lua header files for C++
// <<extern "C">> not supplied automatically because Lua also compiles as C++

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

只需#include <lua.hpp>

遇到此链接错误,我不得不更改

#define LUA_API extern

#define LUA_API extern "C"

我正在使用Lua 5.1 BTW

您的代码可能没什么问题,您有链接问题,它找不到lua_tolstring的函数定义。 链接时添加lua库,就可以了。

Lua文件在C中,因此您必须使用

extern "C" { #include "luafiles.cpp" }

您只是收到链接器错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM