linux下lua與c++交互lua5.3調用so-64位

繼上一篇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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章