2013年3月28日 星期四

liblua.so的製作

從lua官方下載到原始碼之後,手動編譯它,然後就會有lua, luac, 以及liblua.a三個二元檔,以及一些header和文件等..等等,少了什麼的樣子。親愛的動態連結程式庫勒??

難不成lua沒辦法做成動態連結程式庫?直覺上來說,不可能。(光是有這個想法就讓我覺得自己..)Makefile裡面沒有寫不代表生不出來,更何況提供precompiled binary的網站也有提供動態連結程式庫,所以這樣的東西一定可以被做出來。

所以秉持這樣的信念,改了./Makefile:

--- lua-5.2.1/Makefile  2012-05-17 22:05:54.000000000 +0800
+++ lua-5.2.1-m/Makefile        2013-03-28 22:12:33.549010236 +0800
@@ -4,13 +4,13 @@
 # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
 
 # Your platform. See PLATS for possible values.
-PLAT= none
+PLAT= linux
 
 # Where to install. The installation starts in the src and doc directories,
 # so take care if INSTALL_TOP is not an absolute path. See the local target.
 # You may want to make INSTALL_LMOD and INSTALL_CMOD consistent with
 # LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h.
-INSTALL_TOP= /usr/local
+INSTALL_TOP= ~/applications/lua-5.2.1-so
 INSTALL_BIN= $(INSTALL_TOP)/bin
 INSTALL_INC= $(INSTALL_TOP)/include
 INSTALL_LIB= $(INSTALL_TOP)/lib
@@ -41,7 +41,7 @@
 # What to install.
 TO_BIN= lua luac
 TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp
-TO_LIB= liblua.a
+TO_LIB= liblua.a liblua.so
 TO_MAN= lua.1 luac.1
 
 # Lua version and release.
以及./src/Makefile:
--- lua-5.2.1/src/Makefile      2012-03-10 00:32:16.000000000 +0800
+++ lua-5.2.1-m/src/Makefile    2013-03-28 22:24:37.717029984 +0800
@@ -7,7 +7,7 @@
 PLAT= none
 
 CC= gcc
-CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS)
+CFLAGS= -O2 -Wall -fPIC -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS)
 LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS)
 LIBS= -lm $(SYSLIBS) $(MYLIBS)
 
@@ -42,8 +42,10 @@
 LUAC_T=        luac
 LUAC_O=        luac.o
 
+LUA_SO = liblua.so
+
 ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O)
-ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)
+ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) $(LUA_SO)
 ALL_A= $(LUA_A)
 
 # Targets start here.
@@ -59,6 +61,9 @@
        $(AR) $@ $(BASE_O)
        $(RANLIB) $@
 
+$(LUA_SO): $(BASE_O)
+       $(CC) -shared -o $@ $(LDFLAGS) $(BASE_O) $(LIBS)
+
 $(LUA_T): $(LUA_O) $(LUA_A)
        $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)
接著回到原始碼根目錄(source root),執行make && make install就大功告成。

接著來試試看烘出來的麵包會不會讓人肚子痛:

/*a.c*/
#include <lua.h>                                              
#include <lualib.h>                                           
#include <lauxlib.h>                                          
#include <stdio.h>                                            
                                                                                                                                  
int main()                                                           
{                                                                                                                                 
  lua_State* s = luaL_newstate();                                    
  luaL_openlibs(s);                                                                                                               
  lua_getglobal(s, "print");                                                                          
  lua_pushstring(s, "Hello LUA!");                                                                    
  lua_pcall(s, 1, 0, 0);                                                                                                          
  lua_close(s);                                                                                                                   
}
編譯它:
$ gcc -Wl,-rpath,\$ORIGIN\
> -I${HOME}/applications/lua-5.2.1-so/include\
> -L${HOME}/applications/lua-5.2.1-so/lib -llua a.c
執行它:
$ cp ~/applications/lua-5.2.1-so/lib/liblua.so
$ ./a.out
Hello LUA!
$ ldd ./a.out
        linux-vdso.so.1 =>  (0x00007fffdabfe000)
        liblua.so => /tmp/./liblua.so (0x00007f88fb392000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003120e00000)
        libm.so.6 => /lib64/libm.so.6 (0x0000003121e00000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003121600000)
        libreadline.so.6 => /lib64/libreadline.so.6 (0x0000003123a00000)
        libncurses.so.5 => /lib64/libncurses.so.5 (0x0000003133000000)
        libtinfo.so.5 => /lib64/libtinfo.so.5 (0x000000312c000000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003120600000)
收工啦!(啃餅乾)

沒有留言:

張貼留言