COCOS2DX 4.X 加入CJSON 库LUA

2020-01-28  本文已影响0人  罐头过期

刚刚调通在COCOS2DX 4.x 加入 CJSON 到LUA中使用 ,android编译老是提示错误 multiple definition of luaL_setfuncs 。

注意Cocos2dx 4.x需要安装 CMake 3.1X
环境
lua 5.3
windows 10
vs 2015
cocos2dx 4.0
Cmake 3.16

1、修改 frameworks\cocos2d-x\external\lua\cjson\lua_cjson.c 文件行数1323 ,注释 lua_State 函数 。

2、添加 frameworks\cocos2d-x\external\lua\cjson\CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.6)

set(lib_name cjson)
set(target_name ext_${lib_name})

project(${lib_name})

set(${target_name}_src
  fpconv.c
  lua_cjson.c
  strbuf.c
)

add_library(${target_name} STATIC
  ${${target_name}_src}
)

target_include_directories(${target_name} PRIVATE ../luajit/include)
target_include_directories(${target_name} PUBLIC .)

set_target_properties(${target_name}
    PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    FOLDER "External"
)

3、修改 frameworks\cocos2d-x\external\CMakeLists.txt 文件 行数106

# use lua/js specific libs by property to prevent conflict
if(BUILD_LUA_LIBS)
    add_subdirectory(lua/luajit)
    add_subdirectory(lua/tolua)
    add_subdirectory(lua/cjson)
    add_subdirectory(lua/luasocket)
    set_property(TARGET external APPEND PROPERTY 
        CC_LUA_DEPEND ext_luajit ext_tolua ext_cjson ext_luasocket
    )
endif()

4、修改 frameworks\cocos2d-x\cocos\scripting\lua-bindings\manual\network\lua_extensions.c 文件

#include "lua/cjson/lua_cjson.h"
static luaL_Reg luax_exts[] = {
    {"socket.core", luaopen_socket_core},
    {"mime.core", luaopen_mime_core},
    {"cjson", luaopen_cjson_safe }, 
    {NULL, NULL}
};

5、启动win32 模拟器 cocos compile -p win32

6、启动android APK cocos compile -p android
出现 “BUILD SUCCESSFUL” 编译成功

上一篇下一篇

猜你喜欢

热点阅读