Building and linking external static lib to game with CMake

Building and linking external static lib to game with CMake
0

#1

I have a library that I need to add to the game, and while I have it working using a VS project file for Win32 and Android.mk (LOCAL_STATIC_LIBRARIES += mylib_static) for Android, I’m not quite sure how to add it the the new CMake build process in Cocos2d-x 3.17.

Would anyone be able to provide an example of how to add an external library to the game using the CMake build process?

The external library is my own, so I have full source code with it’s own CMakeLists.txt file. I can build it on it’s own perfectly fine using CMake, but I’m not sure how to include it in the game build process, or how to link the library.

I have almost no experience with CMake, and while I’ve made some attempts, I can’t seem to figure out how to do it. Looking through the CMakeLists.txt and .cmake files in the modules folder just caused more confusion, so I really would appreciate any help.

EDIT: I think I got it working, but any example of the correct way to do it would still be great in case the way I’ve set it up is not optimal.


#2

Could you share how you done it?

I’ve tried this:

add_library(MyLibName STATIC IMPORTED)
set_target_properties(MyLibName
PROPERTIES IMPORTED_LOCATION
my/lib/dir/MyLibName.lib)

cocos_build_app(${APP_NAME}
APP_SRC “${APP_SRC}”
DEPEND_COMMON_LIBS “cocos2d” “MyLibName”
DEPEND_ANDROID_LIBS “cocos2d_android”
)

and also this, if I was to add it Windows-only:

cocos_build_app(${APP_NAME}
APP_SRC “${APP_SRC}”
DEPEND_COMMON_LIBS “cocos2d”
DEPEND_WINDOWS_LIBS “MyLibName”
DEPEND_ANDROID_LIBS “cocos2d_android”
)

But neither works, I get: “cocos_use_pkg Function invoked with incorrect arguments for function named: cocos_use_pkg” error.


#3

The separate library has something similar to this CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)

add_definitions(-DRAPIDJSON_HAS_STDSTRING=1 -DMYLIB_STATIC)

if(WINDOWS)
  add_definitions(-DWIN32 -D_WINDOWS -D_LIB)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
  if(MSVC)
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od")
  endif()
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")

if (CMAKE_BUILD_TYPE STREQUAL "Debug") 
	add_definitions(-D_DEBUG)
endif()

set(MYLIB_SOURCE)
set(MYLIB_HEADER)


#Extra folders to include 
include(${CMAKE_CURRENT_LIST_DIR}/rapidjson/CMakeLists.txt)

include_directories(
	${CMAKE_CURRENT_LIST_DIR}
)

list(APPEND MYLIB_SOURCE
	${CMAKE_CURRENT_LIST_DIR}/SourceFile1.cpp
	${CMAKE_CURRENT_LIST_DIR}/SourceFile2.cpp
	)

list(APPEND MYLIB_HEADER
	${CMAKE_CURRENT_LIST_DIR}/SourceFile1.h
	${CMAKE_CURRENT_LIST_DIR}/SourceFile2.h
	)
	
list(APPEND MYLIB_SOURCE ${MYLIB_HEADER})

add_library(mylib STATIC ${MYLIB_SOURCE})

set_target_properties(mylib
    PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
)

In the CMakeLists.txt that exists within your cocos2d game folder, these are the additions:

set(GAME_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})

include_directories(
	Classes
	${COCOS2DX_ROOT_PATH}/cocos/audio/include/
# ==> Module paths
	${GAME_ROOT_PATH}/../MyLib/
)

...
# This is specific to your external library
if(USE_EXTERNAL_PREBUILT)
    set(_mylib_prefix MyLib)
    set(MyLib_INCLUDE_DIRS ${GAME_ROOT_PATH}/../MyLib)
else()
    cocos_find_package(MyLib MyLib REQUIRED)
endif()
message(STATUS "MyLib include dirs: ${MyLib_INCLUDE_DIRS}")

...

# Example of how to add "mylib" 
cocos_build_app(${APP_NAME}
                APP_SRC "${APP_SRC}"
                DEPEND_COMMON_LIBS "cocos2d" "mylib"
                DEPEND_ANDROID_LIBS "cocos2d_android" "sdkbox" "PluginFacebook"
                )

That is quite literally all that I’ve done to get it working, and I hope it helps you in some way.


#4

Thanks for quick reply.

I’ve managed to solve this by linking the “usual CMake way”, disregarding these special cocos custom commands:

add_library(MyLibName STATIC IMPORTED)
set_target_properties(MyLibName
                      PROPERTIES IMPORTED_LOCATION
                      ${CMAKE_CURRENT_SOURCE_DIR}/path/to/my/lib/MyLibName.lib)

and then:

target_link_libraries(${APP_NAME} MyLibName)
add_dependencies(${APP_NAME} MyLibName)