c++ - 如何将函数从 C++ 可执行文件公开给 LuaJIT

标签 c++ function symbols ffi luajit

我正在尝试将 Lua 脚本加载到我的 C++ 应用程序中并运行它。 我决定使用 LuaJIT 来利用它的 FFI 库。 但是我有一个奇怪的问题,我的 Lua 脚本无法看到我在 C++ 代码中定义的函数符号,并且在运行我的应用程序时出现此错误:

undefined symbol: test_func_a

下面是我的 C++ 和 Lua 代码:

//C++//

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

#ifdef __cplusplus
  extern "C" {
#endif

void test_func_a ( void ) {
  printf ( "hello\n" );
}

#ifdef __cplusplus
  }
#endif

int main ( int argc, char** argv ) {
  lua_State *lua = luaL_newstate();
  assert ( lua );
  luaL_openlibs ( lua );
  const int status = luaL_dostring ( lua, lua_script_content );

  if ( status )
    printf ( "Couldn't execute LUA code: %s\n", lua_tostring ( lua, -1 ));

  lua_close ( lua );

  return 0;

//Lua//

local ffi = require('ffi');

ffi.cdef[[
  void test_func_a (void);
]]

ffi.C.test_func_a()

默认情况下gcc会导出所有符号,luajit怎么看不到它们?

最佳答案

使用:

extern "C" __declspec(dllexport) void test_func_a ( void ) {printf ("hello\n" );}

关于c++ - 如何将函数从 C++ 可执行文件公开给 LuaJIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38950536/

相关文章:

c++ - 使用波浪号获取 int 的 MAX 值

javascript - 在 Javascript/Node 中运行函数数组的规范方式

c++ - 那个赋值符号是什么意思

javascript - 测试字符串是否每个字母两侧都有 '+' 符号

c++ - 有没有办法测试输入是否是 C++ 中的数字?

c++ - Visual Studio Code 找不到 g++.exe (Windows 10)

ios - 核心情节: first/last point symbol is out of screen

C# 无法解析符号

c++ - 无法在 XmlHttpRequest 模块工作时获取断点(Javascript 引擎/XHR 请求)

java - 流口水功能