From 7c05525f461866d39ccf6b2801fb985a231bc51e Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Thu, 21 Feb 2019 22:28:07 +0100 Subject: [PATCH] Fixing #8, and starting basic example --- chapters/basics/programs.md | 2 +- chapters/intro/newcmake.md | 2 +- examples/CMakeLists.txt | 2 ++ examples/simple-project/CMakeLists.txt | 36 ++++++++++++++++++++++ examples/simple-project/simple_example.cpp | 13 ++++++++ examples/simple-project/simple_lib.cpp | 5 +++ examples/simple-project/simple_lib.hpp | 5 +++ 7 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 examples/simple-project/CMakeLists.txt create mode 100644 examples/simple-project/simple_example.cpp create mode 100644 examples/simple-project/simple_lib.cpp create mode 100644 examples/simple-project/simple_lib.hpp diff --git a/chapters/basics/programs.md b/chapters/basics/programs.md index 23b46ec..d9f2e69 100644 --- a/chapters/basics/programs.md +++ b/chapters/basics/programs.md @@ -39,7 +39,7 @@ Here, the generation happens after `some_target` is complete, and happens when y ## Included common utilities -A useful tool in writing CMake builds that work cross-platform is `cmake -E ` (seen in CMake files as `${CMAKE_COMMAND} -E`). This mode allows CMake to do a variety of things without calling system tools explicitly, like `copy`, `make_directory`, and `remove`. It is mostly used for the build time commands. Note that the very useful `create_symlink` mode only works on Unix systems. [See the docs](https://cmake.org/cmake/help/latest/manual/cmake.1.html#command-line-tool-mode). +A useful tool in writing CMake builds that work cross-platform is `cmake -E ` (seen in CMake files as `${CMAKE_COMMAND} -E`). This mode allows CMake to do a variety of things without calling system tools explicitly, like `copy`, `make_directory`, and `remove`. It is mostly used for the build time commands. Note that the very useful `create_symlink` mode used to be Unix only, but was added for Windows in CMake 3.13. [See the docs](https://cmake.org/cmake/help/latest/manual/cmake.1.html#command-line-tool-mode). [execute_process]: https://cmake.org/cmake/help/latest/command/execute_process.html diff --git a/chapters/intro/newcmake.md b/chapters/intro/newcmake.md index 63e1ef0..be8fa51 100644 --- a/chapters/intro/newcmake.md +++ b/chapters/intro/newcmake.md @@ -155,7 +155,7 @@ shiny new Python find module (2 and 3 versions too), and many more. * `string(JOIN` and `list(JOIN`, and `list(TRANSFORM` * `file(TOUCH` and `file(GLOB CONFIGURE_DEPENDS` * C++20 support -* CUDA as a language improvements: CUDA < 7.5 supported +* CUDA as a language improvements: CUDA 7 and 7.5 now supported * Support for OpenMP on macOS (command line only) * Several new properties and property initializers * CPack finally reads `CMAKE_PROJECT_VERSION` variables diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8424905..713ee83 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -4,6 +4,8 @@ project(ModernCMakeExamples) enable_testing() +add_subdirectory(simple-project) + add_subdirectory(root-usefile) add_subdirectory(root-simple) add_subdirectory(root-simple-3.11) diff --git a/examples/simple-project/CMakeLists.txt b/examples/simple-project/CMakeLists.txt new file mode 100644 index 0000000..1879d17 --- /dev/null +++ b/examples/simple-project/CMakeLists.txt @@ -0,0 +1,36 @@ +# Almost all CMake files should start with this +# You should always specify a range with the newest +# and oldest tested versions of CMake. This will ensure +# you pick up the best policies. +cmake_minimum_required(VERSION 3.1...3.13) + +# This is your project statement. You should always list languages; +# Listing the version is nice here since it sets lots of useful variables +project(ModernCMakeExample VERSION 1.0 LANGUAGES CXX) + +# If you set any CMAKE_ variables, that can go here. +# (But usually don't do this, except maybe for C++ standard) + +# Find packages go here. + +# You should usually split this into folders, but this is a simple example + +# This is a "default" library, and will match the *** variable setting. +# Other common choices are STATIC, SHARED, and MODULE +# Including header files here helps IDEs but is not required. +# Output libname matches target name, with the usual extensions on your system +add_library(MyLibExample simple_lib.cpp simple_lib.hpp) + +# Link each target with other targets or add options, etc. + +# Adding something we can run - Output name matches target name +add_executable(MyExample simple_example.cpp) + +# Make sure you link your targets with this command. It can also link libraries and +# even flags, so linking a target that does not exist will not give a configure-time error. +target_link_libraries(MyExample PRIVATE MyLibExample) + +# This part is so the Modern CMake book can verify this example builds. For your code, +# you'll probably want tests too +enable_testing() +add_test(NAME MyExample COMMAND MyExample) diff --git a/examples/simple-project/simple_example.cpp b/examples/simple-project/simple_example.cpp new file mode 100644 index 0000000..e9d6ca1 --- /dev/null +++ b/examples/simple-project/simple_example.cpp @@ -0,0 +1,13 @@ +// This is an example file as part of Modern-CMake + +#include "simple_lib.hpp" + +#include + +int main() { + + std::cout << "Simple example C++ compiled correctly and ran." << std::endl; + std::cout << simple_lib_function() << std::endl; + + return 0; +} diff --git a/examples/simple-project/simple_lib.cpp b/examples/simple-project/simple_lib.cpp new file mode 100644 index 0000000..bd70958 --- /dev/null +++ b/examples/simple-project/simple_lib.cpp @@ -0,0 +1,5 @@ +#include + +std::string simple_lib_function() { + return "Compiled in library"; +} diff --git a/examples/simple-project/simple_lib.hpp b/examples/simple-project/simple_lib.hpp new file mode 100644 index 0000000..421ad18 --- /dev/null +++ b/examples/simple-project/simple_lib.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include + +std::string simple_lib_function();