diff --git a/Cmake Intro/.gitignore b/Cmake Intro/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/Cmake Intro/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/Cmake Intro/CMakeLists.txt b/Cmake Intro/CMakeLists.txt new file mode 100644 index 0000000..4ecbd26 --- /dev/null +++ b/Cmake Intro/CMakeLists.txt @@ -0,0 +1,8 @@ +# set a minimum CMake version required +cmake_minimum_required(VERSION 2.4) + +# start a new CMake project +project(HelloWorld) + +# create build target app +add_executable(app hello.cpp) \ No newline at end of file diff --git a/Cmake Intro/hello.cpp b/Cmake Intro/hello.cpp new file mode 100644 index 0000000..e52a3fb --- /dev/null +++ b/Cmake Intro/hello.cpp @@ -0,0 +1,7 @@ +#include + +int main() +{ + std::cout << "Hello World!\n"; + return 0; +} \ No newline at end of file diff --git a/Cmake Intro/readme.md b/Cmake Intro/readme.md new file mode 100644 index 0000000..b120975 --- /dev/null +++ b/Cmake Intro/readme.md @@ -0,0 +1,60 @@ +# C++ with CMAKE +The most basic project is an executable built from source code files. +For simple projects, a three line `CMakeLists.txt` file is all that is required. Inside this file are all commands for CMAKE. + +You can build and run our project now. First, run the cmake executable (or cmake-gui) to configure the project and then build it with your chosen build tool. + +Move to the CMake source code tree and create a build directory: +```bash +mkdir build +``` +Next, navigate to the build directory and run CMake to configure the project and generate a native build system: +```bash +cd build +cmake .. +``` +Then call that build system to actually compile/link the project: +```bash +cmake --build . +``` + +## Version Numbering and Header Files +Provide the executable and project with a version number, using CMakeLists.txt provides more flexibility. This can be done by using the `VERSION` append to the project name. +```cmake +cmake_minimum_required(VERSION 3.10) + +# set the project name and version +project(HelloWorld VERSION 1.0) +``` + +You can also configure header files to pass version numbers to the source code. Here a header file `Hello.h.in` will be read and the version numbers passed to get `Hello.h` +```bash +configure_file(Hello.h.in Hello.h) +``` +As the configured file will be written into the binary tree, add that directory to the list of paths to search for include files. +```bash +target_include_directories(HelloWorld PUBLIC + "${PROJECT_BINARY_DIR}" + ) +``` +When CMake configures header files the values inside `@...@` will be replaced. + +## Specify the C++ Standard +Sometimes you need to explicitly state in the CMake code that it should use the correct flags. The easiest way to enable support for a specific C++ standard in CMake is by using the `CMAKE_CXX_STANDARD` variable. Make sure to add the `CMAKE_CXX_STANDARD` declarations above the call to `add_executable`. +```cmake +cmake_minimum_required(VERSION 3.10) + +# set the project name and version +project(HelloWorld VERSION 1.0) + +# specify the C++ standard +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED True) +``` + +## Rebuild +Build our project again. As you already created the bild directory skip this step and proceed: +```bash +cd build +cmake --build . +``` \ No newline at end of file