{"id":991,"date":"2015-11-23T19:00:20","date_gmt":"2015-11-23T19:00:20","guid":{"rendered":"http:\/\/coolt.ch\/notizen\/?p=991"},"modified":"2016-03-15T09:16:50","modified_gmt":"2016-03-15T09:16:50","slug":"cmake-introduction","status":"publish","type":"post","link":"https:\/\/coolt.ch\/notizen\/cmake-introduction\/","title":{"rendered":"Cmake introduction"},"content":{"rendered":"<p><strong>Automatisches Generieren von Makefiles<\/strong><\/p>\n<pre class=\"lang:sh decode:true\">$ mkdir build\r\n$ cd build\r\n$ cmake ..   \/\/ f\u00fchrt die CMakeList.txt aus = generiert Makefiles\r\n             \/\/ cmakeList.txt liegt ein Verzeichnis weiter unten\r\n$ make       \/\/ f\u00fchrt die Makefiles aus<\/pre>\n<p>Mit cmake werden Makefiles generiert. Alle Makefiles liegen im Ordner build. Alle Dateien, die die Programme zur Ausf\u00fchrung brauchen, m\u00fcssen im build liegen.<\/p>\n<p><strong>Mehrere Executables<\/strong><\/p>\n<pre class=\"lang:sh decode:true \">CMAKE_MINIMUM_REQUIRED(VERSION 2.6)\r\nPROJECT(HuffmanDecoder)\r\n\r\nADD_DEFINITIONS(\"-std=c++11 -g\")\r\n\r\nADD_EXECUTABLE(decoder\r\n    main.cpp\r\n    FileStreamReader.cpp\r\n    HuffmanDecoder.cpp\r\n    BinaryTree.cpp\r\n    EndoscopeImage.cpp\r\n)\r\n\r\nADD_EXECUTABLE(decoder_fullversion\r\n    decoder.cpp\r\n    FileStreamReader.cpp\r\n    HuffmanDecoder.cpp\r\n    BinaryTree.cpp\r\n    EndoscopeImage.cpp\r\n)\r\n\r\nADD_EXECUTABLE(encoder\r\n    encoder.cpp\r\n    FileStreamReader.cpp\r\n    HuffmanDecoder.cpp\r\n    BinaryTree.cpp\r\n    EndoscopeImage.cpp\r\n)\r\n\r\nADD_EXECUTABLE(testTree\r\n    testTree.cpp\r\n    BinaryTree.cpp\r\n)<\/pre>\n<p>Will man nur ein bestimmtes Projekt kompilieren, schreibt man:<\/p>\n<pre class=\"lang:sh decode:true\">$ make testTree &amp;&amp; .\/testTree<\/pre>\n<p>Durch &amp;&amp; .\/executalbe wird das Programm direkt ausgef\u00fchrt.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Referenzen im Netz<\/strong><\/p>\n<p><a href=\"https:\/\/cmake.org\/cmake-tutorial\/\">Link1: Anleitung f\u00fcr Programmiererinnen<\/a><\/p>\n<p><a href=\"https:\/\/wiki.ubuntuusers.de\/cmake\">Link2: Einfache Einf\u00fchrung<\/a><\/p>\n<p>Make bedeutet <strong>kompilieren (und linken)<\/strong>. Will man mehrere Dateien kompilieren und dann auch linken, gen\u00fcgt ein einfacher Shell-Befehl <span class=\"lang:sh decode:true crayon-inline\">gcc -o main.c test<\/span>\u00a0 nicht mehr. Man macht dann ein <strong>Makefile<\/strong>, in dem durch Pfad-Variablen und Variablen der Datein, das Kompilieren, Linken vereinfacht wird.<\/p>\n<p>Die Aufgabe von <strong>Cmake<\/strong> ist es, Makefiles automatisiert zu erstellen. Der Aufruf cmake funktioniert nur, wenn im Zielordner auch die Datei <strong>CMakeLists.txt<\/strong> liegt. In jedem<strong> Unterordner<\/strong> liegt eine <strong>CMakeLists.txt<\/strong><\/p>\n<pre class=\"lang:sh decode:true\"><strong>cmake<\/strong> pfad\/zu\/dateien<\/pre>\n<p>Alle wichtigen Ergebnisse des Kompilieren, Linkens legt das Programm selbst\u00e4ndig in diese Datei ab.<\/p>\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.<\/p>\n<p><strong>Projektstruktur<\/strong><br \/>\nproject \/\u00a0 CMakeList.txt\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ das <strong>main<\/strong> CMake<br \/>\n.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/ source \/\u00a0\u00a0CMakeList.txt\u00a0\u00a0\u00a0 \u00a0 \/\/ jeder Unterordner braucht sein CMake<br \/>\n.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 main.cpp<br \/>\n.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 classb.cpp<br \/>\n.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/build\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ nur hier kein CMakeList.txt<br \/>\n.<\/p>\n<p>Erh\u00e4lt man ein Programm aus einer Plattform, wird das Programm oft so gebildet:<\/p>\n<pre class=\"lang:sh decode:true\">$ mkdir build\r\n$ cd build\r\n$ cmake ..   \/\/ f\u00fchrt die CMakeList.txt aus = generiert Makefiles\r\n$ make       \/\/ f\u00fchrt die Makefiles aus<\/pre>\n<p>&nbsp;<\/p>\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.<\/p>\n<p>CMake legt auch eine <strong>CMakeCache.txt<\/strong> Datei selbst\u00e4ndig an. Darin sind alle <strong>Konfigurationen<\/strong> gespeichert. Diese Datei sollte nicht manuell ge\u00e4ndert werden.<\/p>\n<p>Das <strong>Programm<\/strong> wird oft nicht im Ordner der Quelldateien abgelegt. Man kann einen <strong>Ordner build<\/strong> angeben oder \/user\/bin nehmen.<\/p>\n<p><strong><br \/>\nHauptdatei: CMakeLists.txt<\/strong><\/p>\n<pre class=\"\">add_executable(hallowelt <strong>main.c<\/strong>)<\/pre>\n<p>Hat man mehrere Files, so werden diese in einer Varibale zusammengefasst<\/p>\n<p><span class=\"lang:sh decode:true crayon-inline\">SET( SOURCE main.c calculator.c) <\/span>\u00a0 Mit set werden <strong>Variablen<\/strong> gesetzt<\/p>\n<p>Braucht man sp\u00e4ter die Variable, so geschieht dies mit <span class=\"lang:sh decode:true crayon-inline \">${SOURCE}<\/span>\u00a0 .<\/p>\n<pre class=\"\">add_executable(calculator <strong>${SOURCE}<\/strong>)<\/pre>\n<p><span class=\"lang:sh decode:true crayon-inline \">PROJECT( name )<\/span><br \/>\n<span class=\"lang:sh decode:true crayon-inline \">INCLUDE_DIRECTORY( \/usr\/bin )<\/span><br \/>\n<span class=\"lang:sh decode:true crayon-inline \">TARGET_LINK_LIBRARIES()<\/span><br \/>\n<span class=\"lang:sh decode:true crayon-inline\">ADD_DEFINITIONS()<\/span><\/p>\n<p><span class=\"lang:sh decode:true crayon-inline\">INSTALL( TARGETS &lt;programm&gt; RUNTIME DESTINATION bin) <\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Variable zu bereits<strong> bestehenden Variablen hinzuf\u00fcgen<\/strong><\/p>\n<p><span class=\"lang:sh decode:true crayon-inline\">SET( EXTRALIBS <strong>${EXTRALIBS}<\/strong> \/usr\/local\/bla)<\/span><br \/>\nFett stehen hier alle alten Pfade zu Bibliotheken, danach kommt der neue Pfad.<br \/>\nEin typisches Beispiel ist die Umgebungsvariable PATH. Dort wird auch zu ${PATH} &lt;neuer Pfad&gt; dazugef\u00fchrt.<\/p>\n<p>..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Automatisches Generieren von Makefiles $ mkdir build $ cd build $ cmake .. \/\/ f\u00fchrt die CMakeList.txt aus = generiert Makefiles \/\/ cmakeList.txt liegt ein Verzeichnis weiter unten $ make \/\/ f\u00fchrt die Makefiles aus Mit cmake werden Makefiles generiert. Alle Makefiles liegen im Ordner build. Alle Dateien, die die Programme zur Ausf\u00fchrung brauchen, m\u00fcssen &hellip; <a href=\"https:\/\/coolt.ch\/notizen\/cmake-introduction\/\" class=\"more-link\"><span class=\"screen-reader-text\">Cmake introduction<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[37],"_links":{"self":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/991"}],"collection":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/comments?post=991"}],"version-history":[{"count":9,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/991\/revisions"}],"predecessor-version":[{"id":1423,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/991\/revisions\/1423"}],"wp:attachment":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/media?parent=991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/categories?post=991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/tags?post=991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}