Today I tried how to use luajit in cocos2d-x, found that the cocos2d-x itself luajit support is not good, but there are some places to do is not perfect, my platform is linux, I don't know if it is a platform problem, maybe the mac is very well done, so write this article to hope to be helpful to the development of cocos2d-x on the linux platform.
First, why should we use luajit? I can think of two reasons: efficiency and encryption. The execution efficiency after luajit is greatly improved, especially in android. In addition, the file obtained after luajit is a binary bytecode and cannot be decompiled. Therefore, for commercial products, improved the cost of Reverse Solution
So what is the difference between luajit and lua? There is no difference in usage. Because the header files are consistent, there are two steps to replace lua with luajit. One is to replace the header file with luajit, the other is to replace the dynamic or static link library with luajit
Development Platform: Ubuntu 13.04 64-bit
Cocos2d-x: 2.2.1
First let's take a look at how to make cocos2d-x support luajit on the linux platform
In fact, on the linux platform cocos2d-x for each module are dynamic links, so we can handle luajit separately, and finally reference luajit in Makefile
In fact, for linux platform, cocos2d-x does not support luajit, you can view $ {COCOS_ROOT}/scripting/lua/proj. linux
TARGET = liblua.soINCLUDES += -I.. -I../lua -I../tolua \ -I../Classes -I../../../CocosDenshion/include -I../../../extensions -I../../../external/chipmunk/include/chipmunkSOURCES = ../lua/lapi.o \ ../lua/lauxlib.c \ ../lua/lbaselib.c \ ../lua/lcode.c \ ../lua/ldblib.c \ ../lua/ldebug.c \ ../lua/ldo.c \ ../lua/ldump.c \ ../lua/lfunc.c \ ../lua/lgc.c \ ../lua/linit.c \ ../lua/liolib.c \ ../lua/llex.c \ ../lua/lmathlib.c \ ../lua/lmem.c \ ../lua/loadlib.c \ ../lua/lobject.c \ ../lua/lopcodes.c \ ../lua/loslib.c \ ../lua/lparser.c \ ../lua/lstate.c \ ../lua/lstring.c \ ../lua/lstrlib.c \ ../lua/ltable.c \ ../lua/ltablib.c \ ../lua/ltm.c \ ../lua/lundump.c \ ../lua/lvm.c \ ../lua/lzio.c \ ../lua/print.c \ ../tolua/tolua_event.c \ ../tolua/tolua_is.c \ ../tolua/tolua_map.c \ ../tolua/tolua_push.c \ ../tolua/tolua_to.c \ ../cocos2dx_support/tolua_fix.c \ ../cocos2dx_support/CCLuaBridge.cpp \ ../cocos2dx_support/CCLuaEngine.cpp \ ../cocos2dx_support/CCLuaStack.cpp \ ../cocos2dx_support/CCLuaValue.cpp \ ../cocos2dx_support/Cocos2dxLuaLoader.cpp \ ../cocos2dx_support/LuaCocos2d.cpp \ ../cocos2dx_support/CCBProxy.cpp \ ../cocos2dx_support/Lua_extensions_CCB.cpp \ ../cocos2dx_support/lua_cocos2dx_extensions_manual.cppinclude ../../../cocos2dx/proj.linux/cocos2dx.mkTARGET := $(LIB_DIR)/$(TARGET)SHAREDLIBS += -lextensionall: $(TARGET)$(TARGET): $(OBJECTS) $(CORE_MAKEFILE_LIST)@mkdir -p $(@D)$(LOG_LINK)$(CXX) $(CXXFLAGS) $(OBJECTS) -shared -o $@ $(SHAREDLIBS) $(STATICLIBS)$(OBJ_DIR)/%.o: ../%.cpp $(CORE_MAKEFILE_LIST)@mkdir -p $(@D)$(LOG_CXX)$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEFINES) -c $< -o $@$(OBJ_DIR)/%.o: ../%.c $(CORE_MAKEFILE_LIST)@mkdir -p $(@D)$(LOG_CC)$(CC) $(CCFLAGS) $(INCLUDES) $(DEFINES) -c $< -o $@
Obviously, for linux, lua is directly used instead of luajit. Since we want to use luajit, there are two options: one is to replace all lua in Makefile with luajit, the other is to delete the lua Part Of The Makefile and divide the so file into two parts. We use the second method because we are not very familiar with luajit. The new Makefile file is as follows:
TARGET = liblua.soINCLUDES += -I.. -I../luajit/include -I../tolua \ -I../Classes -I../../../CocosDenshion/include -I../../../extensions -I../../../external/chipmunk/include/chipmunkSOURCES = ../tolua/tolua_event.c \ ../tolua/tolua_is.c \ ../tolua/tolua_map.c \ ../tolua/tolua_push.c \ ../tolua/tolua_to.c \ ../cocos2dx_support/tolua_fix.c \ ../cocos2dx_support/CCLuaBridge.cpp \ ../cocos2dx_support/CCLuaEngine.cpp \ ../cocos2dx_support/CCLuaStack.cpp \ ../cocos2dx_support/CCLuaValue.cpp \ ../cocos2dx_support/Cocos2dxLuaLoader.cpp \ ../cocos2dx_support/LuaCocos2d.cpp \ ../cocos2dx_support/CCBProxy.cpp \ ../cocos2dx_support/Lua_extensions_CCB.cpp \ ../cocos2dx_support/lua_cocos2dx_extensions_manual.cppinclude ../../../cocos2dx/proj.linux/cocos2dx.mkTARGET := $(LIB_DIR)/$(TARGET)SHAREDLIBS += -lextensionall: $(TARGET)$(TARGET): $(OBJECTS) $(CORE_MAKEFILE_LIST)@mkdir -p $(@D)$(LOG_LINK)$(CXX) $(CXXFLAGS) $(OBJECTS) -shared -o $@ $(SHAREDLIBS) $(STATICLIBS)$(OBJ_DIR)/%.o: ../%.cpp $(CORE_MAKEFILE_LIST)@mkdir -p $(@D)$(LOG_CXX)$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEFINES) -c $< -o $@$(OBJ_DIR)/%.o: ../%.c $(CORE_MAKEFILE_LIST)@mkdir -p $(@D)$(LOG_CC)$(CC) $(CCFLAGS) $(INCLUDES) $(DEFINES) -c $< -o $@
But then there will be no luajit. What should I do? Of course you need to compile a libluajit. so out, this is actually very simple, in ${COCOS_ROOT}/scripting/lua/luajit/LuaJIT-2.0.1
Then make will generate libluajit under src. so, just copy the file to $ {COCOS_ROOT}/lib/linux/release and $ {COCOS_ROOT}/lib/linux/debug.
Now we have prepared luajit, but we need to make a small modification to the Makefile file in the project. Take $ {COCOS_ROOT}/samples/Lua/HelloLua as an example.
The new proj. linux/Makefile is as follows:
EXECUTABLE = HelloLuaCOCOS_ROOT = ../../../..INCLUDES = -I../ -I../Classes -I$(COCOS_ROOT)/CocosDenshion/include \ -I$(COCOS_ROOT)/scripting/lua/luajit/include \ -I$(COCOS_ROOT)/scripting/lua/tolua \ -I$(COCOS_ROOT)/scripting/lua/cocos2dx_support \ -I$(COCOS_ROOT)/extensions \SOURCES = main.cpp ../Classes/AppDelegate.cpp SHAREDLIBS += -lcocos2d -lcocosdenshion -llua -lluajit -lextensionCOCOS_LIBS = $(LIB_DIR)/libcocos2d.so $(LIB_DIR)/libcocosdenshion.so $(LIB_DIR)/liblua.so $(LIB_DIR)/libluajit.soinclude $(COCOS_ROOT)/cocos2dx/proj.linux/cocos2dx.mk$(TARGET): $(OBJECTS) $(STATICLIBS) $(COCOS_LIBS) $(CORE_MAKEFILE_LIST)@mkdir -p $(@D)$(LOG_LINK)$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ $(SHAREDLIBS) $(STATICLIBS) $(LIBS)$(OBJ_DIR)/%.o: ../%.cpp $(CORE_MAKEFILE_LIST)@mkdir -p $(@D)$(LOG_CXX)$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEFINES) $(VISIBILITY) -c $< -o $@$(OBJ_DIR)/%.o: %.cpp $(CORE_MAKEFILE_LIST)@mkdir -p $(@D)$(LOG_CXX)$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEFINES) $(VISIBILITY) -c $< -o $@
Obviously, we have added-lluajit and $ {LIB_DIR}/libluajit. so, which can be used in linux now.
Next let's take a look at how to make cocos2d-x support luajit on android platform
In fact, luajit is relatively well supported in android. There is a shell file to complete most compilation tasks, $ {COCOS_ROOT}/scripting/lua/luajit/build_android.sh
But this file is a bit small problem, one is 5th lines SRCDIR = $ DIR/LuaJIT-2.0.1 was originally LuaJit, maybe this file was originally written on mac, so not case sensitive
The other is that some commands in it do not use arch. You can directly view the final version of the file as follows:
#!/bin/shDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"host_os=`uname -s | tr "[:upper:]" "[:lower:]"`arch_os=`uname -m | tr "[:upper:]" "[:lower:]"`SRCDIR=$DIR/LuaJIT-2.0.1cd "$SRCDIR"NDK=$NDK_ROOTNDKABI=8NDKVER=$NDK/toolchains/arm-linux-androideabi-4.7NDKP=$NDKVER/prebuilt/${host_os}-${arch_os}/bin/arm-linux-androideabi-NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"# Android/ARM, armeabi (ARMv5TE soft-float), Android 2.2+ (Froyo)DESTDIR=$DIR/android/armeabirm "$DESTDIR"/*.amake cleanmake HOST_CC="gcc -m32" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF"if [ -f $SRCDIR/src/libluajit.a ]; then mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.afi;# Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.0+ (ICS)NDKARCH="-march=armv7-a -mfloat-abi=softfp -Wl,--fix-cortex-a8"DESTDIR=$DIR/android/armeabi-v7arm "$DESTDIR"/*.amake cleanmake HOST_CC="gcc -m32" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF $NDKARCH"if [ -f $SRCDIR/src/libluajit.a ]; then mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.afi;# Android/x86, x86 (i686 SSE3), Android 4.0+ (ICS)NDKABI=14DESTDIR=$DIR/android/x86NDKVER=$NDK/toolchains/x86-4.7NDKP=$NDKVER/prebuilt/${host_os}-${arch_os}/bin/i686-linux-android-NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-x86"rm "$DESTDIR"/*.amake cleanmake HOST_CC="gcc -m32" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF"if [ -f $SRCDIR/src/libluajit.a ]; then mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.afi;make clean
After the modification, we can directly execute./build_android.sh to generate the. a file used in android.
Note: If this step reports an error, it should be the problem of your compilation environment, rather than the problem of cocos2d-x, I encountered a problem is missing mulitlib library, so if you report an error in this step
Take a closer look at what the error is saying, and then press the error to google to solve the problem, because the error here is generally not a problem of cocos2d-x.
After this step is completed, android is actually replaced with luajit.
I don't know how to change it because I don't have a mac machine. It's ugly here.