projtec:cmake
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
projtec:cmake [2017/01/25 23:03] – orel | projtec:cmake [2017/03/02 12:20] – [Tests Unitaires] orel | ||
---|---|---|---|
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) | ||
</ | </ | ||
- | Pour générer un Makefile et compiler notre projet, il faut taper les commandes suivantes : | + | Pour générer un Makefile et compiler notre projet |
cmake . | cmake . | ||
- | make # ou make VERBOSE=ON | + | make clean ; make |
+ | | ||
+ | make help # pour voir la liste des cibles possibles | ||
+ | Pour compiler "en dehors des sources" | ||
+ | |||
+ | mkdir build | ||
+ | cd build | ||
+ | cmake .. | ||
+ | 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 61: | Line 75: | ||
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) | ||
install(TARGETS hello RUNTIME DESTINATION bin) | install(TARGETS hello RUNTIME DESTINATION bin) | ||
</ | </ | ||
+ | |||
+ | Notons que CMake scanne automatiquement les dépendances (sans faire néanmoins de preprocessing). Il en résulte que la dépendance sur //pouet.h// est ajouté implicitement. | ||
Ensuite, on fait... | Ensuite, on fait... | ||
cmake . | cmake . | ||
- | make | + | make |
- | make install | + | make install |
+ | | ||
| | ||
==== Utilisation d'une bibliothèque interne ==== | ==== Utilisation d'une bibliothèque interne ==== | ||
Line 147: | Line 169: | ||
</ | </ | ||
- | Voici le fichier //CMake// pour compiler et exécuter les tests... | + | Voici le fichier //CMake// pour compiler et exécuter les tests. |
<code text tests/ | <code text tests/ | ||
Line 159: | Line 181: | ||
add_test(test1 test-mylib-foo) | add_test(test1 test-mylib-foo) | ||
- | set_tests_properties(test1 PROPERTIES PASS_REGULAR_EXPRESSION " | + | set_tests_properties(test1 PROPERTIES PASS_REGULAR_EXPRESSION " |
add_test(test2 test-mylib-bar) | add_test(test2 test-mylib-bar) | ||
- | set_tests_properties(test2 PROPERTIES PASS_REGULAR_EXPRESSION "bar") | + | set_tests_properties(test2 PROPERTIES PASS_REGULAR_EXPRESSION "foo" |
- | add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}) | + | # add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}) |
</ | </ | ||
Bah vous connaissez la formule... Pour lancer les tests : | Bah vous connaissez la formule... Pour lancer les tests : | ||
- | make check | + | make test |
+ | Pour aller encore plus loin... | ||
- | ==== Biblio ==== | + | <code text tests/ |
+ | cmake_minimum_required(VERSION 3.0) | ||
- | * https:// | + | option(ENABLE_DEBUG "debug options" |
- | | + | if(ENABLE_DEBUG) |
- | | + | |
+ | | ||
+ | INCLUDE(CTest ) # or Dart ? | ||
+ | else() | ||
+ | set(CMAKE_C_FLAGS " | ||
+ | endif() | ||
+ | add_executable(foo foo.c) | ||
+ | |||
+ | enable_testing() # enable test (not useful if CTest included) | ||
+ | |||
+ | add_test(test1 ./foo 2) | ||
+ | add_test(test2 ./foo 5) | ||
+ | </ | ||
+ | |||
+ | cmake . -DENABLE_DEBUG=ON | ||
+ | make test | ||
+ | make ExperimentalCoverage | ||
+ | more Testing/ | ||
+ | |
projtec/cmake.txt · Last modified: 2024/03/18 15:06 by 127.0.0.1