简体   繁体   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;
    }

This seems to propagate several errors such as: 这似乎传播了一些错误,例如:

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

What's wrong? 怎么了?

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

Lua 5.1 has lua.hpp: 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"
}

Just #include <lua.hpp> . 只需#include <lua.hpp>

Encountered this linking error and I Had to change the 遇到此链接错误,我不得不更改

#define LUA_API extern

to

#define LUA_API extern "C"

I'm using Lua 5.1 BTW 我正在使用Lua 5.1 BTW

Probably nothing wrong with your code, you have a linking problem, it can't find the function definition for lua_tolstring. 您的代码可能没什么问题,您有链接问题,它找不到lua_tolstring的函数定义。 Add the lua library when linking and you should be fine. 链接时添加lua库,就可以了。

The Lua files are in C so you have to use Lua文件在C中,因此您必须使用

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

You are just getting linker errors. 您只是收到链接器错误。

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

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