分享

shared libraries

 quasiceo 2014-01-12

I am making a wrapper between a C++ engine and Lua, I'm using LuaJIT and because of this I'm using ffi as the "wrapper" between these two, since the engine has much and different parts I was thinking it would be nice to divide them in files and then requiring them, however, after reading a bit about LuaJIT I found that for external libraries you have to load the library, so I came with this: When and where I should load the library? In the "glue" code (the one wich unifies all the modules)?, in everyone?, or it would be better to keep it as a single file? Also, for deciding this how much slow is loading a library?

asked May 8 '13 at 18:14
Sheosi
84

add comment

1 Answer

up vote 1 down vote accepted

You can create a 'core' module that loads the library: engine/core.lua

local ffi = require'ffi'

local C = ffi.load('engine') -- loads .so/.dll

ffi.cdef[[
/* put common function/type definitions here. */
]]

-- return native module handle
return C

Then you would create a module for each of the engine parts: engine/graphics.lua

local ffi = require'ffi' -- still need to load ffi here.

local C = require'engine.core'

-- load other dependent parts.
-- If this module uses types from other parts of the engine they most
-- be defined/loaded here before the call to the ffi.cdef below.
require'engine.types'

ffi.cdef[[
/* define function/types for this part of the engine here. */
]]

local _M = {}

-- Add glue functions to _M module.
function _M.glue_function()
  return C.engine_function()
end

return _M

The code in the 'engine.core' module will only be executed once. The biggest issue with separating the engine into parts will be to handle cross type dependencies. To solve this add 'typedef struct name name;' to the 'engine.core' module for types that are used in multiple parts.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多