projtec:cmake
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
projtec:cmake [2017/02/09 14:36] – [Tests Unitaires] orel | projtec:cmake [2024/03/18 15:06] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
======CMake ====== | ======CMake ====== | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
==== Hello World ==== | ==== Hello World ==== | ||
Line 18: | Line 22: | ||
<code text CMakeLists.txt> | <code text CMakeLists.txt> | ||
cmake_minimum_required(VERSION 2.6) | cmake_minimum_required(VERSION 2.6) | ||
- | project(HelloWorld) | + | project(HelloWorld |
add_executable(hello hello.c) | add_executable(hello hello.c) | ||
</ | </ | ||
Line 35: | Line 39: | ||
cmake .. | cmake .. | ||
make | make | ||
+ | | ||
+ | Un des avantages de cette méthode est quelle permet de ne pas mélanger les fichiers générés par CMake et Make avec les fichiers sources. Du coup, il suffit de supprimer le répertoire //build// pour tout nettoyer. | ||
+ | |||
==== Un peu plus compliqué ==== | ==== Un peu plus compliqué ==== | ||
Line 69: | Line 76: | ||
cmake_minimum_required (VERSION 2.6) | cmake_minimum_required (VERSION 2.6) | ||
project (HelloWorld C) | project (HelloWorld C) | ||
+ | |||
+ | set(CMAKE_BUILD_TYPE DEBUG) | ||
+ | set(CMAKE_VERBOSE_MAKEFILE ON) | ||
+ | set(CMAKE_C_COMPILER gcc) | ||
set(CMAKE_C_FLAGS " | set(CMAKE_C_FLAGS " | ||
set(CMAKE_INSTALL_PREFIX " | set(CMAKE_INSTALL_PREFIX " | ||
+ | |||
add_executable(hello hello.c pouet.c) | add_executable(hello hello.c pouet.c) | ||
target_link_libraries(hello m) | target_link_libraries(hello m) | ||
Line 139: | Line 151: | ||
</ | </ | ||
- | ==== Tests Unitaires | + | ==== Tests Dynamiques |
On ajoute à notre projet un sous-répertoire //tests// pour vérifier les fonctions de //mylib// ({{projtec: | On ajoute à notre projet un sous-répertoire //tests// pour vérifier les fonctions de //mylib// ({{projtec: | ||
Line 158: | Line 170: | ||
</ | </ | ||
- | Voici le fichier //CMake// pour compiler et exécuter les tests. Par defaut, pour chaque test, CMake vérifie que le programme termine normalement (pas de signal SEGV par exemple ) et que la valeur de retour vaut EXIT_SUCCESS. On peut également rajouter d' | + | Voici le fichier //CMake// pour compiler et exécuter les tests. Par defaut, pour chaque test, CMake vérifie que le programme termine normalement (pas de signal SEGV par exemple ) et que la valeur de retour vaut EXIT_SUCCESS |
<code text tests/ | <code text tests/ | ||
+ | include(CTest) | ||
enable_testing() | enable_testing() | ||
Line 174: | Line 187: | ||
set_tests_properties(test2 PROPERTIES PASS_REGULAR_EXPRESSION " | set_tests_properties(test2 PROPERTIES PASS_REGULAR_EXPRESSION " | ||
- | add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}) | ||
</ | </ | ||
- | Bah vous connaissez la formule... | + | Pour lancer les tests : |
+ | make test | ||
- | make check | + | ==== Fuites Mémoire ==== |
+ | Pour aller un peu plus loin, on peut lancer des tests de varification de la mémoire (memcheck) : | ||
- | ==== Biblio ==== | + | make ExperimentalMemCheck |
+ | |||
+ | Ces tests se base sur la variable MEMORYCHECK_COMMAND qui est par défault sur / | ||
- | * https://cmake.org/ | + | Les logs sont disponibles dans : Testing/Temporary/MemoryChecker.*.log |
- | | + | |
- | * https://cmake.org/cmake/help/v3.3/manual/cmake-commands.7.html | + | Il est possible d' |
+ | |||
+ | | ||
+ | set(MEMORYCHECK_COMMAND_OPTIONS " | ||
+ | |||
+ | |||
+ | ==== Couverture de Code ==== | ||
+ | |||
+ | Pour aller encore plus loin... | ||
+ | |||
+ | <code text tests/CMakeLists.txt> | ||
+ | cmake_minimum_required(VERSION 3.0) | ||
+ | |||
+ | include(CTest) | ||
+ | enable_testing() | ||
+ | |||
+ | set(CMAKE_C_FLAGS " | ||
+ | |||
+ | add_executable(foo foo.c) | ||
+ | |||
+ | add_test(test1 ./foo 2) | ||
+ | add_test(test2 ./foo 5) | ||
+ | </code> | ||
+ | |||
+ | | ||
+ | make ExperimentalTest # ou make test | ||
+ | make ExperimentalCoverage | ||
+ | more Testing/CoverageInfo/* | ||
+ | |||
+ | Il est plus élégant de n' | ||
+ | |||
+ | <code text tests/CMakeLists.txt> | ||
+ | cmake_minimum_required(VERSION | ||
+ | |||
+ | include(CTest) | ||
+ | enable_testing() | ||
+ | |||
+ | set(CMAKE_C_FLAGS " | ||
+ | set(CMAKE_C_FLAGS_DEBUG " | ||
+ | |||
+ | add_executable(foo foo.c) | ||
+ | |||
+ | add_test(test1 ./foo 2) | ||
+ | add_test(test2 ./foo 5) | ||
+ | </ | ||
+ | |||
+ | |||
+ | Il faut alors compiler le code en mode DEBUG (et non pas en mode RELEASE) : | ||
+ | |||
+ | | ||
+ | make ExperimentalTest | ||
+ | make ExperimentalCoverage | ||
+ | more Testing/ | ||
projtec/cmake.1486651004.txt.gz · Last modified: 2024/03/18 15:05 (external edit)