How to add a submodule inside Classes/ folder and say CMake to find and link it

Hi,
I would like to have a folder inside my Classes folder(i.e a submodule). I would like to make a CMakeLists.txt file in that submodule folder that will be used to add if any new files is added in that. And using that folder like a library in the main CMakeLists.txt.

Classes/
subModule/
hello.h
hello.cpp
CMakeLists.txt --> submodule level cmake
AppDelegate.cpp
AppDelegate.h
CMakeLists.txt —> main CMakeLists.txt

As above, I need to use that submodule folder as a library or something in the main CMakeLists.txt, so that I dont have to add the files inside the submodule in the main CMakeLists.txt. Could you say me how to make a CMakeLists.txt file for the above case?

I dont understand what you want to do and why?

Can you explain exactly what you need to do with some examples of what these submodules are?

You probably shouldn’t put the subModule folder inside Classes. This is how I personally deal with this:
image

The external folder is where all sub-modules (libraries) go. So, for example, I have a version of Box2D in mine:
image

Now, in your root CMakeLists.txt, you include a library like this:

target_link_libraries(${APP_NAME} cocos2d)
target_include_directories(${APP_NAME}
    PRIVATE Classes
    PRIVATE ${COCOS2DX_ROOT_PATH}/cocos/audio/include/
)

# All libraries/sub-modules go after the lines you see above

# Add module
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/MyModule ${PROJECT_BINARY_DIR}/MyModule)
get_target_property(mymodule_INCLUDE_DIRS mymodule-static INTERFACE_INCLUDE_DIRECTORIES)
target_link_libraries(${APP_NAME} mymodule-static)
target_include_directories(${APP_NAME} PRIVATE ${mymodule_INCLUDE_DIRS})

This is an example of a CMakeLists.txt for a sub-module:

cmake_minimum_required(VERSION 3.6)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(MYMODULE_SOURCE)
set(MYMODULE_HEADER)

include_directories(
	${CMAKE_CURRENT_LIST_DIR}
)

list(APPEND MYMODULE_SOURCE
	${CMAKE_CURRENT_LIST_DIR}/SourceFile.cpp
	)

list(APPEND MYMODULE_HEADER
	${CMAKE_CURRENT_LIST_DIR}/SourceFile.h
	)
	
list(APPEND MYMODULE_SOURCE ${MYMODULE_HEADER})

add_library(mymodule-static STATIC ${MYMODULE_SOURCE})

if(WINDOWS)
	target_compile_definitions(mymodule-static PUBLIC WIN32 _WINDOWS _LIB)
endif()

if(WINDOWS)
	target_compile_options(mymodule-static PUBLIC "$<$<CXX_COMPILER_ID:MSVC>:$<$<CONFIG:DEBUG>:/Od>>")
endif()

if(ANDROID)
endif()

if(IOS)

endif()

set_target_properties(mymodule-static
    PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
)
target_include_directories(mymodule-static PUBLIC ${CMAKE_CURRENT_LIST_DIR})

I have a button class which is made for my style extending ui::button and some other common classes which I ll be using in all of my projects. I wish to make them as a submodule and using them for different projects. For that I m not well sure how I could configure my cmakelists.txt file inside my submodule and compile it as a shared object file and link it to the main project. Creating a submodule and adding the files in main CMakeLists will work, but I don’t want that. I wish to do all the build in submodule itself and use it. Just like cocos2d folder inside the project. But here my submodule needs to use classes inside cocos2d folder as well. So how to write CMakeLists.txt file for my submodule keeping all these factors. Is it possible?

You need to refactor your code. If you have something you need to put into a sub-module, then it shouldn’t have dependencies to code in your main app. Why don’t you move those dependencies into their own library as well?

You don’t really need to use a separate library for all of this either. You can just have a separate folder, which you can put in Classes if you want, as you originally mentioned above, and just copy it between your projects. It can have it’s own CMakeLists.txt, but no need to build a separate library from it. For example, the CMakeLists.txt would look something like this:

include_directories(
	${CMAKE_CURRENT_LIST_DIR}
)

list(APPEND GAME_SOURCE
	${CMAKE_CURRENT_LIST_DIR}/File1.cpp
	${CMAKE_CURRENT_LIST_DIR}/File2.cpp
	${CMAKE_CURRENT_LIST_DIR}/File3.cpp
	${CMAKE_CURRENT_LIST_DIR}/File4.cpp
   
    )
	 
list(APPEND GAME_HEADER
	${CMAKE_CURRENT_LIST_DIR}/File1.h
	${CMAKE_CURRENT_LIST_DIR}/File2.h
	${CMAKE_CURRENT_LIST_DIR}/File3.h
	${CMAKE_CURRENT_LIST_DIR}/File4.h
    )

In your root CMakelists.txt, you include it like this:

include(${CMAKE_CURRENT_LIST_DIR}/Classes/SubModule/CMakeLists.txt)

This does not solve the problem of the sub-module dependencies, so again, if a sub-module has dependencies on other code in Classes, then you really need to re-think the structure of your code.