继上一篇windows下lua调用C++的想法,我那么linux下也想折腾一下,看看他们之间有多大的差别,使用环境centos7 ,vscode,lua5.3.4版本 有这三样就行了



extern "C" {

#include <lua.h>

#include <lauxlib.h>

#include <lualib.h>

};

#include <iostream>  

#include <vector>

#include <set>

using namespace std;



static const char* const ERROR_ARGUMENT_COUNT = "error param num!";

static const char* const ERROR_ARGUMENT_TYPE = "error param type!";

class Solution {

public:

	vector<string> Permutation(string str, vector<string>&result) {

		loc_Permutation(str, 0);

		if (str.size() == 0)return result;

		for (auto item : all_str){

			result.push_back(item);

		}

		//result = all_str;

		return result;

	}

	void loc_Permutation(string str, int loc){

		all_str.insert(str);

		//all_str.push_back(str);

		//cout << str << endl;

		int size = str.size();

		if (loc == size - 1)return;

		//loc_Permutation(str, loc + 1);

		for (int idx_swap = loc; idx_swap < size; idx_swap++){

			//cout << loc << " " << idx_swap << " ";

			swap(str[loc], str[idx_swap]);

			loc_Permutation(str, loc + 1);

			swap(str[loc], str[idx_swap]);

		}



	}

public:

	set<string> all_str;

};



void ErrorMsg(lua_State* L, const char* const pszErrorInfo)

{

	lua_pushstring(L, pszErrorInfo);

	lua_error(L);

}



// ��⺯�����ò��������Ƿ�����

void CheckParamCount(lua_State* L, int paramCount)

{

	// lua_gettop��ȡջ��Ԫ�ظ���.  

	if (lua_gettop(L) != paramCount)

	{

		ErrorMsg(L, ERROR_ARGUMENT_COUNT);

	}

}

//ȫ�����㷨

extern "C" int AllSort(lua_State* L)

{

	



	// ��ȡ����.  

	const char* str= luaL_checkstring(L, 1);



	if (str)

	{

		Solution sln;

		vector<string> strResult;

		strResult = sln.Permutation(str, strResult);

		//lua_settop(L, 0);

		lua_newtable(L);

		lua_newtable(L);

		int count = 1;

		for (int i = 0; i < strResult.size();i++)

		{

			lua_pushstring(L, strResult[i].c_str());

			//lua_setfield(L, -2, i);

			lua_rawseti(L, -2, count++);

		}

		//Ƕ��school������������

		lua_pushstring(L, "array");

		lua_insert(L, -2); /*	school table */

		lua_settable(L, -3); /* param.school = school */

		//return strResult.size();

	}

	else

	{

		ErrorMsg(L, ERROR_ARGUMENT_TYPE);

	}



	// ����ֵ����Ϊ0��.  

	return 1;

}





// ��ʾWindows�Ի���.  

// @param [in] pszMessage string 1  

// @param [in] pszCaption string 2  

extern "C" int ShowMsgBox(lua_State* L)

{

	const char* pszMessage = "hdc";

	const char* pszCaption = "hdc";



	// �����������Ƿ���ȷ.  

	CheckParamCount(L, 2);



	// ��ȡ����.  

	pszMessage = luaL_checkstring(L, 1);

	pszCaption = luaL_checkstring(L, 2);



	if (pszCaption && pszMessage)

	{

		cout << "Hello world from clibs!" << endl;

		lua_pushstring(L, "Hello world from clibs!");

	}

	else

	{

		ErrorMsg(L, ERROR_ARGUMENT_TYPE);

	}



	// ����ֵ����Ϊ0��.  

	return 1;

}

 extern "C" int sub2(lua_State* L)

 {

	   double op1 = luaL_checknumber(L, 1);

	   double op2 = luaL_checknumber(L, 2);

	   int temp = op1 - op2;

	   lua_pushnumber(L, temp);

	   return 1;

 }



// ���������б�.  

static luaL_Reg luaLibs[] =

{

	{ "ShowMsgBox", ShowMsgBox },

	{ "sub2", sub2 },

	{ "AllSort", AllSort },

	{ NULL, NULL }

};

// part two: DLL��ں�����Lua���ô�DLL����ں���.  

extern "C" 

int luaopen_WinFeature(lua_State* L) {   //WinFeature��modole��, ����require�������

	const char* const LIBRARY_NAME = "WinFeature"; //����ҲдWinFeature

	//luaL_register(L, LIBRARY_NAME, luaLibs);  //�ؼ�һ��, ��luaState��ע������lib

		lua_newtable(L);

		luaL_setfuncs(L,  luaLibs,0);

	return 1;

}

跟windows版本的区别在于

extern "C" __declspec(dllexport)
int luaopen_WinFeature(lua_State* L) {   //WinFeature是modole名, 将来require这个名字
    const char* const LIBRARY_NAME = "WinFeature"; //这里也写WinFeature
    //luaL_register(L, LIBRARY_NAME, luaLibs);  //关键一行, 在luaState上注册好这个lib
        lua_newtable(L);
        luaL_setfuncs(L,  luaLibs,0);
    return 1;
}

这个函数__declspec(dllexport)这个linux下是不需要的,否则编译不通过,C++代码准备好了,那一般系统安装的是5.1版本的lua,先升级到5.3.4,

wget http://www.lua.org/ftp/lua-5.3.4.tar.gz
yum install libtermcap-devel ncurses-devel libevent-devel readline-devel -y
tar -xzf lua-5.3.4.tar.gz
cd lua-5.3.4
 make linux test

编译成功后,

rm -rf /usr/bin/lua
ln -s /home/hdc001/testLua/lua-5.3.4/src/lua /usr/bin/lua

lua -v

Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio

复制文件库和头文件到系统目录

cp /home/hdc001/testLua/lua-5.3.4/src/liblua.a  /usr/lib

cp /home/hdc001/testLua/lua-5.3.4/src/lua.h  /usr/include

cp /home/hdc001/testLua/lua-5.3.4/src/lauxlib.h  /usr/include

cp /home/hdc001/testLua/lua-5.3.4/src/lualib.h  /usr/include

cp /home/hdc001/testLua/lua-5.3.4/src/luaconf.h  /usr/include

接下来配置makefile文件

WinFeature.so : DllMain.cpp
	g++ -g -Wall --shared -fPIC -llua DllMain.cpp -o WinFeature.so
clean :
	rm *.so

main.lua文件

local WinFeature=require "WinFeature"

print(WinFeature.sub2(1,2));

local msg=WinFeature.AllSort("123456")

for i = 1, #msg.array do

    print(msg.array[i])

end

print(msg);

lua main.lua命令运行,得到结果

顺利运行,整个过程碰到一个坑怎么都调用so出错,后来才知道,一定要用g++编译才行,gcc报错的,这两者区别可以谷歌一下。

工程示例下载地址https://download.csdn.net/download/huangdecai2/12400642

Logo

更多推荐