我正在尝试将 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/