Cmake scénario multi-bibliothèque

Nous aimerions organiser un projet C++ comme ceci:

 project/ lib1/ (first library) CMakeList.txt src/ lib1.c foo1.h build/ test/ (tests) CMakeList.txt test1.c test2.c lib2/ (second library) CMakeList.txt src/ CMakeList.txt os/ (OS dependent code) CMakeList.txt win32/ xxx.c (win32 implementation) linux/ xxx.c (linux implementation) lib2.c foo2.h build/ include/ (shared/public headers) lib1/ lib.h (shared library header included from apps) lib2/ lib.h (shared library header -"-) 

S’il vous plaît, comment écrire ces CMakeLists.txt quand même lib2 devrait utiliser link1 et quand par exemple lib2 devrait être portable (au moins Win32, Linux …)?

Correction : si certains fichiers CMakeList.txt ne sont pas à leur place, supposez-le. J’ai probablement oublié.

Toute la philosophie est de commencer par un CMakeLists.txt central pour tout votre projet. A ce niveau, toutes les cibles (bibliothèques, exécutables) vont être agrégées, il n’y aura donc aucun problème pour relier lib1 à lib2 par exemple. Si lib2 doit créer un lien vers lib1, lib1 doit d’abord être construit.

Les fichiers source spécifiques à la plate-forme doivent être définis de manière conditionnelle sur une variable. (Si vous devez définir une variable dans un sous-répertoire et l’utiliser dans un répertoire ci-dessus, vous devez la définir dans le cache, à l’aide de CACHE FORCE, etc. – voir le manuel de set )

Voici comment vous construisez correctement à partir de la source – comme le veut CMake:

 cd project-build cmake ../project 

Avoir des répertoires de construction séparés par bibliothèque n’est pas très complexe (il faut le dire) et nécessiterait probablement quelques piratages.

 project-build/ project/ CMakeLists.txt (whole project CMakeLists.txt) [ project(MyAwesomeProject) include_directories(include) # allow lib1 and lib2 to include lib1/lib.h and lib2/lib.h add_subdirectory(lib1) # this adds target lib1 add_subdirectory(lib2) # this adds target lib2 ] lib1/ (first library) CMakeList.txt [ add_library(lib1...) add_subdirectory(test) ] src/ lib1.c foo1.h test/ (tests) CMakeList.txt test1.c test2.c lib2/ (second library) CMakeList.txt [ add_subdirectory(src) ] src/ CMakeList.txt [ if(WIN32) set(lib2_os_sources os/win32/xxx.c) elsif(LINUX) set(lib2_os_sources os/linux/xxx.c) else() message(FATAL_ERROR "Unsupported OS") endif() add_library(lib2 SHARED lib2.c ${lib2_os_sources}) ] os/ (OS dependent code) win32/ xxx.c (win32 implementation) linux/ xxx.c (linux implementation) lib2.c foo2.h include/ (shared/public headers) lib1/ lib.h (shared library header included from apps) lib2/ lib.h (shared library header -"-)