准备
下载和安装
系统环境检查
BOOST_ROOT
LUA_PATH
PATH
编译
vscode 打开luaBindMaster
设置cmake安装位置
新建CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(luabindMaster)
INCLUDE_DIRECTORIES($ENV{LUA_PATH}/include)
INCLUDE_DIRECTORIES($ENV{BOOST_ROOT})
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})
aux_source_directory(${PROJECT_SOURCE_DIR}/src SOURCE)
add_library(luabindMaster ${SOURCE} )
target_link_libraries(luabindMaster
PUBLIC
$ENV{LUA_PATH}/lua5.1.5.lib
)
修改object.hpp文件
basic_iterator<basic_access> 和 basic_iterator<raw_access> 增加boost:optional
再次编译, 成功
测试
新建项目luabind
新建main.cpp, CMakeLists.txt, test.lua
设置cmakePath
main.cpp
#include <iostream>
#include <luabind/luabind.hpp>
#include <luabind/lua_include.hpp>
#ifdef WIN32
#define LUA_EXPORT __declspec(dllexport)
#endif
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
}
void greet()
{
std::cout << "hello world!\n";
std::cout << "hi, eric xiang!\n";
}
void welcome()
{
std::cout << "welcome to join us!\n";
}
void echo(const char * str)
{
std::cout << str << std::endl;
}
extern "C" int LUA_EXPORT luaopen_main(lua_State* L)
{
using namespace luabind;
open(L);
module(L)
[
def("greet", &greet),
def("regard", &welcome),
def("echo", (void (*)(const char *))&echo)
];
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(main)
INCLUDE_DIRECTORIES($ENV{LUA_PATH}/include)
# luabind 头文件位置
INCLUDE_DIRECTORIES(C:/Users/Administrator/Downloads/luabind-master/luabind-master)
INCLUDE_DIRECTORIES($ENV{BOOST_ROOT})
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})
aux_source_directory(${PROJECT_SOURCE_DIR} SOURCE)
add_definitions(-D_WIN32)
add_library(main SHARED ${SOURCE})
#target_compile_definitions(main PRIVATE _WIN32)
# luabind lib的位置
target_link_libraries(main
PUBLIC
$ENV{LUA_PATH}/lua5.1.lib
PUBLIC
C:/Users/Administrator/Downloads/luabind-master/luabind-master/proj.cmake/build/Debug/luabindMaster.lib
)
test.lua
require("main")
greet()
regard()
echo("lua program")
编译生成main.dll
把生成的main.dll 和 test.lua 放到同一目录
运行
lua5.1.exe test.lua