MWE embed of Lua into a CMake project with a tiny demo
Run the build.sh
script to
observe the outputs on each branch
$ ./build/test
5 3
55
Lua code
function fib(n)
if n == 1 or n == 2 then
return 1,1
end
prev, prevPrev = fib(n-1)
return prev+prevPrev, prev
end
print(fib(5))
print((fib(10)))
$ ./build/test
2.5
Lua code (userLib is a custom "C" library)
a = userLib.avg(1,2,3,4)
print(a)
$ ./build/test
6.0
0.0
Lua code (userLib encapsulate a C++ class's member functions)
userLib.pushValues(1,2,3)
a = userLib.sumValues()
print(a)
userLib.clearValues()
b = userLib.sumValues()
print(b)
Extends the previous example by wrapping the class pointers into a struct. Then setting Lua's extra space as that struct
$ ./build/test
6.0
0.0
Class Foo here!
Lua code (userLib encapsulate a C++ class's member functions)
userLib.pushValues(1,2,3)
a = userLib.sumValues()
print(a)
userLib.clearValues()
b = userLib.sumValues()
print(b)
userLib.fooSay()
Lua is built using the CMake incantations
file(GLOB LUA_SRC "vendored/lua-5.4.4/src/*.c")
include_directories(src include)
add_library(Lua STATIC ${LUA_SRC})
and linked to an exectuable as
target_link_libraries(test Lua)
The only change from base Lua 5.4.4 is the removal of lua.c
from vendored/lua-5.4.4/src/
this allows your code to define an entry point.