From 1783120eb77b9cd7d1a9ed02143ba18ef748ef68 Mon Sep 17 00:00:00 2001 From: Mat Booth Date: Mon, 17 Oct 2022 10:38:48 +0100 Subject: [PATCH] Improve the CMake project wizard template Fixes out-of-the-box deprecation warnings, the template now specifies a minimum CMake version of 3.10 Allows project names containing spaces by using freemarker syntax to remove them where necessary in the CMakeLists.txt file. Demonstrates how to implement the common idiom of configuration header files. --- .../templates/simple/CMakeLists.txt | 16 +++++++++++++--- .../templates/simple/config.h.in | 2 ++ .../templates/simple/main.cpp | 5 +++-- .../templates/simple/manifest.xml | 4 +++- 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 cmake/org.eclipse.cdt.cmake.core/templates/simple/config.h.in diff --git a/cmake/org.eclipse.cdt.cmake.core/templates/simple/CMakeLists.txt b/cmake/org.eclipse.cdt.cmake.core/templates/simple/CMakeLists.txt index 6ee2edbbdc6..8585ed73ced 100644 --- a/cmake/org.eclipse.cdt.cmake.core/templates/simple/CMakeLists.txt +++ b/cmake/org.eclipse.cdt.cmake.core/templates/simple/CMakeLists.txt @@ -1,5 +1,15 @@ -cmake_minimum_required (VERSION 2.6) +cmake_minimum_required(VERSION 3.10) -project (${projectName}) +# Set some basic project attributes +project (${projectName?replace(" ", "_")} + VERSION 0.1 + DESCRIPTION "A Hello World Project") -add_executable(${projectName} ${projectName}.cpp) +# This project will output an executable file +add_executable(${r"${PROJECT_NAME}"} ${projectName?replace(" ", "_")}.cpp) + +# Create a simple configuration header +configure_file(config.h.in config.h) + +# Include the configuration header in the build +target_include_directories(${r"${PROJECT_NAME}"} PUBLIC "${r"${PROJECT_BINARY_DIR}"}") diff --git a/cmake/org.eclipse.cdt.cmake.core/templates/simple/config.h.in b/cmake/org.eclipse.cdt.cmake.core/templates/simple/config.h.in new file mode 100644 index 00000000000..731d2a89868 --- /dev/null +++ b/cmake/org.eclipse.cdt.cmake.core/templates/simple/config.h.in @@ -0,0 +1,2 @@ +#define ${projectName?replace(" ", "_")}_VERSION_MAJOR @${projectName?replace(" ", "_")}_VERSION_MAJOR@ +#define ${projectName?replace(" ", "_")}_VERSION_MINOR @${projectName?replace(" ", "_")}_VERSION_MINOR@ diff --git a/cmake/org.eclipse.cdt.cmake.core/templates/simple/main.cpp b/cmake/org.eclipse.cdt.cmake.core/templates/simple/main.cpp index f1d3a296cb5..3356f130ccd 100644 --- a/cmake/org.eclipse.cdt.cmake.core/templates/simple/main.cpp +++ b/cmake/org.eclipse.cdt.cmake.core/templates/simple/main.cpp @@ -1,7 +1,8 @@ #include -using namespace std; +#include "config.h" int main(int argc, char **argv) { - cout << "Hello world"; + std::cout << "Hello World" << std::endl; + std::cout << "Version " << ${projectName?replace(" ", "_")}_VERSION_MAJOR << "." << ${projectName?replace(" ", "_")}_VERSION_MINOR << std::endl; return 0; } diff --git a/cmake/org.eclipse.cdt.cmake.core/templates/simple/manifest.xml b/cmake/org.eclipse.cdt.cmake.core/templates/simple/manifest.xml index 708c005910b..0ec38ab3025 100644 --- a/cmake/org.eclipse.cdt.cmake.core/templates/simple/manifest.xml +++ b/cmake/org.eclipse.cdt.cmake.core/templates/simple/manifest.xml @@ -1,7 +1,9 @@ + \ No newline at end of file