c - 你如何创建一个调用c lua api的lua插件?

标签 c lua

我正在编写的 lua 插件中出现错误,这些错误是在 lua 运行时的两个副本中链接的症状,根据此消息: http://lua-users.org/lists/lua-l/2008-01/msg00671.html

引用:

Which in turn means the equality test for dummynode is failing. This is the usual symptom, if you've linked two copies of the Lua core into your application (causing two instances of dummynode to appear).

A common error is to link C extension modules (shared libraries) with the static library. The linker command line for extension modules must not ever contain -llua or anything similar!

The Lua core symbols (lua_insert() and so on) are only to be exported from the executable which contains the Lua core itself. All C extension modules loaded afterwards can then access these symbols. Under ELF systems this is what -Wl,-E is for on the linker line. MACH-O systems don't need this since all non-static symbols are exported.

这正是我看到的错误...我不知道我应该怎么做。

我已经将 lua src 目录添加到作为我的 lua 插件的 c 组件的 DLL 的包含路径中,但是当我链接它时,我得到了一堆错误,例如:

Creating library file: libmo.dll.a
CMakeFiles/moshared.dir/objects.a(LTools.c.obj): In function `moLTools_dump':
d:/projects/mo-pong/deps/mo/src/mo/lua/LTools.c:38: undefined reference to `lua_gettop'
d:/projects/mo-pong/deps/mo/src/mo/lua/LTools.c:47: undefined reference to `lua_type'
d:/projects/mo-pong/deps/mo/src/mo/lua/LTools.c:48: undefined reference to `lua_typename'
d:/projects/mo-pong/deps/mo/src/mo/lua/LTools.c:49: undefined reference to `lua_tolstring'

所以,总而言之,我有这种情况:

  1. 静态链接到 lua 运行时的父二进制文件。

  2. 一个 lua 库,它加载一个包含 C 代码的 DLL。

  3. DLL中的C代码需要调用lua c api (eg. lua_gettop())

我如何链接它?动态库当然不能“看到”父二进制文件中的符号,因为父二进制文件不是从 DLL 加载它们,它们是静态链接的。

...但是如果我将符号作为插件的一部分进行链接,则会出现上述错误。

帮助?这似乎是一个应该经常出现的问题(dll 取决于父二进制文件中的符号,你如何链接它?)但我似乎看不到任何有用的线索。

(在你问之前,不,我无法控制父二进制文件,我无法让它从 DLL 加载 lua 符号)

最佳答案

最好为此使用 libtool 以使您的链接更容易和更便携。可执行文件需要与 -export-dynamic 链接以导出其中的所有符号,包括静态库中的 Lua 符号。然后该模块需要与 -module -shared -avoid-version 链接,如果在 Windows 上,还需要 -no-undefined;如果在 MacOS 上,另外 -no-undefined -flat_namespace -undefined suppress -bundle; Linux 和 FreeBSD 不需要其他符号。这将使模块留下父级中满足的 undefined symbol 。如果有任何缺失,模块将无法被父级dlopen

每个环境的语义略有不同,因此可能需要一些调整。有时标志的顺序很重要。同样,推荐使用 libtool,因为它隐藏了很多不一致之处。

关于c - 你如何创建一个调用c lua api的lua插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11492194/

相关文章:

c++ - 动态内存分配是如何工作的

c - 当空格应该被忽略时,我应该如何在 C 中读取一个字符?

for-loop - lua中的并行迭代

lua - Logitech 脚本,按住键直至松开

lua - Pandoc:Lua 过滤器,用于用 <div>abc</div> 替换 {{helloworld}}

c - 在 C 编程中对字符串进行标记

c - 同时运行的自旋锁

c - 使用 C 中的隐藏变量和方法进行测试驱动开发

lua - 是否有任何关键字像 const 或其他任何东西在 lua 中与它做同样的工作?

lua - #在Lua中是什么意思?