Makefile Android.mk 引发的思索
在我们编写 Android 平台 cocos2d-x 游戏的时候,我们除了编写 Classes
之内的源代码文件之外,我们还需要维护其编译文件 Android.mk,如我们在 Classes 添加新的源文件,那么我们就要在 Android.mk 配置添加其编译路径,如:
LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp
每添加一个源文件,我们就要手动添加一个配置,始其能够被编译,同样的,在 proj.linux 的 Makefile 文件也有这样的情况:
OBJECTS = ./main.o \ ../Classes/AppDelegate.o \ ../Classes/HelloWorldScene.o
当然让我们手动维护其配置,当然可以,不过麻烦非常,对于像我这样“懒惰”之人,当然需要想办法让其自动管理喽 ~
自动编译、自动维护
如果要自动维护编译文件之内的源代码文件,我们需要的无非就是所有的源代码文件及其路径,而这样的工作可以通过 Linux 强大的命令 find 来实现自动完成,Android.mk 文件如下:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := game_shared LOCAL_MODULE_FILENAME := libgame # 定义 all-cpp-files 返回当前路径和 Classes 路径想的所有 cpp 文件,注意:这里只考虑 cpp 而没有 c,如果需要自行添加 define all-cpp-files $(patsubst jni/%,%, $(shell find $(LOCAL_PATH)/../../Classes/ $(LOCAL_PATH)/hellocpp -name "*.cpp")) endef # 这里使用新的方式替换换来的方式,以自动添加源文件 LOCAL_SRC_FILES := $(call all-cpp-files) #LOCAL_SRC_FILES := hellocpp/main.cpp \ # ../../Classes/AppDelegate.cpp \ # ../../Classes/HelloWorldScene.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static include $(BUILD_SHARED_LIBRARY) $(call import-module,CocosDenshion/android) \ $(call import-module,cocos2dx) \ $(call import-module,extensions) # 根据自己需要是否启用,上面的静态库同样
这样一个 Android.mk 算是万能的配置了,基本能满足我们编写 cocos2d-x 游戏的大多数需求了,当然如果你使用了第三方库,当然还是需要手动添加一下配置了,不过就源文件来说,不需要手动维护,倒是省事许多。下面在贴一个 Makefile 的万能配置:
CC = gcc CXX = g++ TARGET = leafsoar # 为了保持通用性,干脆起个不相干的目标文件,此名随意 CCFLAGS = -Wall CXXFLAGS = -Wall VISIBILITY = # COCOS2DX_ROOT = /home/leafsoar/... # 如果已经配置过此环境变量,可以不需要此,否则添加此变量值 COCOS2DX_PATH = $(COCOS2DX_ROOT)/cocos2dx INCLUDES = -I../ \ -I../Classes \ -I$(COCOS2DX_PATH) \ -I$(COCOS2DX_PATH)/platform/third_party/linux \ -I$(COCOS2DX_PATH)/platform/third_party/linux/libfreetype2 \ -I$(COCOS2DX_PATH)/cocoa \ -I$(COCOS2DX_PATH)/include \ -I$(COCOS2DX_PATH)/platform \ -I$(COCOS2DX_PATH)/platform/linux \ -I$(COCOS2DX_PATH)/platform/third_party/linux/glew-1.7.0/glew-1.7.0/include/ \ -I$(COCOS2DX_PATH)/kazmath/include \ -I$(COCOS2DX_PATH)/platform/third_party/linux/libxml2 \ -I$(COCOS2DX_PATH)/platform/third_party/linux/libjpeg \ -I$(COCOS2DX_ROOT)/CocosDenshion/include \ # -I$(COCOS2DX_ROOT)/extensions/ \ # 根据自己需要是否包含 extensions 扩展 DEFINES = -DLINUX # 获取源文件列表 define all-cpp-files $(patsubst ./%,%, $(shell find ../Classes ./ -name "*.cpp")) endef # 我是打算让所以编译后的 ".o" 临时文件,全部生成在 "obj" 目录,而不是和源代码同目录 define all-cpp-dir $(patsubst ../%,obj/%, $(shell find ../Classes -type d)) endef # obj 默认目录 OBJDIR=obj/Classes # 获取所有的编译文件列表 OBJECTS=$(patsubst %.cpp,$(OBJDIR)/%.o,$(call all-cpp-files)) # 获取所有的编译文件路径,如果不存在路径则,编译可能出现问题 OBJECTS_DIR=$(call all-cpp-dir) # 如果目录不存在,则创建相应的目录,-p 命令保证了,如果存在,不需要重新创建,这样没有修改的源文件就无需重新编译,提高速度 $(shell mkdir -p obj) $(shell mkdir -p $(OBJECTS_DIR)) #echo: # @echo $(OBJECTS_DIR) #OBJECTS = ./main.o \ # ../Classes/AppDelegate.o LBITS := $(shell getconf LONG_BIT) ifeq ($(LBITS),64) STATICLIBS_DIR = $(COCOS2DX_ROOT)/cocos2dx/platform/third_party/linux/libraries/lib64 else STATICLIBS_DIR = $(COCOS2DX_ROOT)/cocos2dx/platform/third_party/linux/libraries endif STATICLIBS = STATICLIBS = $(STATICLIBS_DIR)/libfreetype.a \ $(STATICLIBS_DIR)/libxml2.a \ $(STATICLIBS_DIR)/libpng.a \ $(STATICLIBS_DIR)/libjpeg.a \ $(STATICLIBS_DIR)/libtiff.a \ # $(STATICLIBS_DIR)/libGLEW.a \ SHAREDLIBS = ifeq ($(LBITS),64) SHAREDLIBS_DIR = $(COCOS2DX_ROOT)/CocosDenshion/third_party/fmod/lib64/api/lib SHAREDLIBS = -L$(SHAREDLIBS_DIR) -lfmodex64 else SHAREDLIBS_DIR = $(COCOS2DX_ROOT)/CocosDenshion/third_party/fmod/api/lib SHAREDLIBS = -L$(SHAREDLIBS_DIR) -lfmodex endif SHAREDLIBS += -lglfw -lGL SHAREDLIBS += -Wl,-rpath,$(SHAREDLIBS_DIR) SHAREDLIBS += -L$(COCOS2DX_ROOT)/cocos2dx/platform/third_party/linux/glew-1.7.0/glew-1.7.0/lib -lGLEW SHAREDLIBS += -Wl,-rpath,$(COCOS2DX_ROOT)/cocos2dx/platform/third_party/linux/glew-1.7.0/glew-1.7.0/lib BIN_DIR_ROOT=bin BIN_DIR = $(BIN_DIR_ROOT) debug: BIN_DIR = $(BIN_DIR_ROOT)/debug debug: CCFLAGS += -g3 -O0 debug: CXXFLAGS += -g3 -O0 debug: SHAREDLIBS += -L$(COCOS2DX_ROOT)/lib/linux/Debug -lcocos2d -lrt -lz -lcocosdenshion debug: SHAREDLIBS += -Wl,-rpath,$(COCOS2DX_ROOT)/lib/linux/Debug/ debug: DEFINES += -DDEBUG debug: $(TARGET) release: BIN_DIR = $(BIN_DIR_ROOT)/release release: CCFLAGS += -O3 release: CXXFLAGS += -O3 release: SHAREDLIBS += -L.$(COCOS2DX_ROOT)/lib/linux/Release -lcocos2d -lrt -lz -lcocosdenshion release: SHAREDLIBS += -Wl,-rpath,$(COCOS2DX_ROOT)/lib/linux/Release/ release: DEFINES += -DNDEBUG release: $(TARGET) ####### Build rules $(TARGET): $(OBJECTS) mkdir -p $(BIN_DIR) $(CXX) $(CXXFLAGS) $(INCLUDES) $(DEFINES) $(OBJECTS) -o $(BIN_DIR)/$(TARGET) $(SHAREDLIBS) $(STATICLIBS) ####### Compile $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) $(INCLUDES) $(DEFINES) $(VISIBILITY) -c $< -o $@ %.o: %.c $(CC) $(CCFLAGS) $(INCLUDES) $(DEFINES) $(VISIBILITY) -c $< -o $@ clean: rm -f $(OBJECTS) $(TARGET) core