kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ sudo apt-get install libluabind-dev
kuyu@ub16:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ sudo apt-get install lua
Consider this hellobind.cpp code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <luabind/luabind.hpp> | |
void greet() | |
{ | |
std::cout << "hello world!\n"; | |
} | |
extern "C" int init(lua_State* L) | |
{ | |
using namespace luabind; | |
open(L); | |
module(L) | |
[ | |
def("greet", &greet) | |
]; | |
return 0; | |
} |
Then consider this test.lua code:
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ cat test.lua
package.loadlib('./hellobind.so', 'init')()
greet()
To run the test.lua code:
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ cat commands.bash
#!/bin/bash
g++ hellobind.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o hellobind.so hellobind.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ ./commands.bash
hello world!
Sometimes, loading the C++ library in Lua can be problematic. To see what the problems are when loading the library, you can execute this lua file (execute "lua test1.lua"):
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ cat test1.lua
local initfunction, errormessage = package.loadlib('/home/kuyu/dkuyu/Dropbox/practice/lua/luabind/hellobind.so','init')
if errormessage then
print('Error loading hello_world:', errormessage)
end
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ cat commands.bash
#!/bin/bash
g++ hellobind.cpp -I/usr/include/lua5.2/ -c -fPIC
g++ -shared -Wl,--whole-archive -o hellobind.so hellobind.o -lluabind -Wl,--no-whole-archive
lua test.lua
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ ./commands.bash
hello world!
Sometimes, loading the C++ library in Lua can be problematic. To see what the problems are when loading the library, you can execute this lua file (execute "lua test1.lua"):
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind/hellobind$ cat test1.lua
local initfunction, errormessage = package.loadlib('/home/kuyu/dkuyu/Dropbox/practice/lua/luabind/hellobind.so','init')
if errormessage then
print('Error loading hello_world:', errormessage)
end
No comments:
Post a Comment