I\'m using cmake to compile one of my work projets, here is the deal
-
client/
CMakeLists.txt
server/
CMakeLists.txt
libs/
libstuff/
CMak
A simple solution is to guard the add_subdirectory
calls in both the client and the server CMake list file with an if
using a TARGET conditional, i.e.:
if (NOT TARGET plugin)
add_subdirectory("${CMAKE_SOURCE_DIR}/common/libplugin")
endif()
This prevents the libplugin
sub-directory from being added more than once.
I would suggest putting three add_subdirectory calls in your root CMakeLists.txt. libstuff first, then client and server....
Setup the Stuff project as if it is standalone, but add variables to the cmake cache such that it can be "imported" by other projects. Then, in client and server, you can refer to the Stuff project... using ordinary call include_directories and target_link_libraries.
E.g. in libstuff...
# libstuff CMakeLists
project( Stuff )
# ... whatever you need here: collect source files, ...
add_library( LibStuff ${Stuff_Sources} )
# Then, define a very useful variables which gets exported to the cmakecache and can be
# used by other cmakelists
set( STUFF_INCLUDE_DIRS ${Stuff_SOURCE_DIR} CACHE STRING "Include-dir for Stuff." FORCE )
And then in Client (and similarly in Server)
# client CMakeLists
project( Client )
# refer to Stuff-includes here...
include_directories( ${STUFF_INCLUDE_DIRS} )
add_executable( Client client.h client.cpp main.cpp ) #
target_link_libraries( Client LibStuff )
You can then "compile" only the Client directory, by stepping into the Client dir and running make or msbuild there. Alternatively, you could add a cmake-flag to the root-cmakelistt which is used to filter between Client, Server or both...