{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# CMake\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# **C**ross platform **Make**\n", "\n", "* One system that generates native build files for:\n", " * UNIX - Makefiles\n", " * MacOS - XCode \n", " * Windows - Visual Studio\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Basic Use \n", "\n", "In root directory of the project you wish to build, run\n", "\n", "```\n", "> cmake\n", "```\n", "\n", "You’ll likely need a few option variables to get it right:\n", "\n", "|Option |Significance|\n", "|:------|------------|\n", "|`CMAKE_INSTALL_PREFIX=/a/path`| Installation path is `/a/path`|\n", "|`BUILD_SHARED_LIBS=ON`| Build shared (dynamic) libraries|\n", "|`CMAKE_BUILD_TYPE=Debug`| Generate files with debug flags set|\n", "|`CMAKE_C_COMPILER=icc`| Sets C Language compiler to `icc`|\n", "|`CMAKE_CXX_FLAGS=\"-O3\"`| Sets C++ compiler optimization level 3|\n", "|`CMAKE_PREFIX_PATH=/a/path`| Search for dependencies in `/a/path`|\n", "\n", "\n", "Many more at: http://www.cmake.org/Wiki/CMake_Useful_Variables\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Examples of `CMake` configuration.\n", "\n", "Options can be given on the command line:\n", "\n", "```\n", "> cmake -DCMAKE INSTALL PREFIX=/usr/local .\n", "```\n", "\n", "Or given in a bash script:\n", "\n", "```\n", "#!/usr/bin/env bash\n", "cmake -DCMAKE_INSTALL_PREFIX=/usr/local \\\n", " -DCMAKE_BUILD_TYPE=Release \\\n", " -DCMAKE_CXX_COMPILER=icpc \\\n", " /path/to/src\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Compile, Test, and Install\n", "\n", "After configuration is complete, type\n", "\n", "```\n", "> make\n", "```\n", "\n", "Can usually build in parallel on multi-core machines, to build on 2 processors\n", "\n", "```\n", "> make -j2\n", "```\n", "\n", "If the project has a test suite, \n", "\n", "```\n", "> make test or > ctest\n", "```\n", "\n", "To install in prefix location (must have permissions) \n", "\n", "```\n", "> make install\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Other\n", "\n", "* With further configuration you can export to Apple XCode or Microsoft Visual Studio Projects\n", "* Interactive interface with `ccmake`. \n", "* Tip:\n", " * Always create a separate build directory within the source for building to keep the source directory clean.\n", " \n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Creating your own CMake projects\n", "\n", "CMake instructions go in `CMakeLists.txt` file\n", "\n", "For example, a typical project directory structure might have:\n", "\n", "```\n", "my_project/\n", "├── CMakeLists.txt \n", "├── src/\n", "| ├── my_source_file_1.cc\n", "| ├── my_source_file_2.cc\n", "| └── CMakeLists.txt\n", "└── test/\n", " ├── my_test_1.cc\n", " ├── my_test_2.cc\n", " └── CMakeLists.txt\n", "```\n", "\n", "The top level `CMakeLists.txt` would have the commands:\n", "\n", "```\n", "add_subdirectrory(src)\n", "add_subdirectrory(test)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Common commands\n", "\n", "To create a compiled library:\n", "\n", "```\n", "add_library(library_name library_source_file_1.cc library_source_file_2.cc)\n", "```\n", "\n", "To create a compiled executable:\n", "\n", "```\n", "add_executable(executable_name executable_source_file_1.cc library_source_file_2.cc)\n", "```\n", "\n", "To add header file locations:\n", "\n", "```\n", "target_include_directories(library_or_executable_name /path/to/header_files)\n", "```\n", "\n", "To link against other libraries:\n", "\n", "```\n", "target_link_libraries(library_or_executable_name library_name_to_link_against)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Other\n", "\n", "* Can create intername CMake variables with `set()`\n", "* Conditional logic with `if()/endif()`, e.g.\n", " ```\n", " set(MY_BOOL_VAR ON)\n", " if(MY_BOOL_VAR)\n", " #Do something\n", " else()\n", " #Do something else\n", " endif()\n", " ```\n", "* Resource for learning CMake:\n", " * https://cmake.org/documentation/\n", " * Existing projects!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true, "init_cell": true, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "%%javascript\n", "function hideElements(elements, start) {\n", " for(var i = 0, length = elements.length; i < length;i++) {\n", " if(i >= start) {\n", " elements[i].style.display = \"none\";\n", " }\n", " }\n", "}\n", "\n", "var prompt_elements = document.getElementsByClassName(\"prompt\");\n", "hideElements(prompt_elements, 0)" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" }, "livereveal": { "autolaunch": false, "footer": "", "progress": true, "scroll": true, "theme": "simple" } }, "nbformat": 4, "nbformat_minor": 2 }