-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_8.html
More file actions
88 lines (74 loc) · 3.16 KB
/
Copy pathCpp2_8.html
File metadata and controls
88 lines (74 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<article>
<h1>C++ II: Page 8 - Build: CMake</h1>
<section>
<h2>Automating the Build Process</h2>
<p>CMake is the industry-standard meta-build system. It doesn't build your code directly; it generates the necessary build files (like Makefiles or Visual Studio projects) for your specific platform.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cmake">
# File: CMakeLists.txt
# Comprehensive project configuration with subdirectories
cmake_minimum_required(VERSION 3.10)
project(ModernApp VERSION 1.0)
# C++17 Standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Directory Structure
# /src - Source files
# /include - Header files
# /tests - Unit tests
# Include headers
include_directories(include)
# Add subdirectories for modularity
add_subdirectory(src)
add_subdirectory(tests)
# Define library if needed
add_library(CoreLib src/Core.cpp)
# Define executable
add_executable(ModernApp src/main.cpp)
# Link library
target_link_libraries(ModernApp PRIVATE CoreLib)
# Compiler options for debugging and warnings
if(MSVC)
target_compile_options(ModernApp PRIVATE /W4)
else()
target_compile_options(ModernApp PRIVATE -Wall -Wextra -Wpedantic)
endif()
# Installation rules
install(TARGETS ModernApp DESTINATION bin)
# Documentation (Optional)
option(BUILD_DOCS "Build documentation" OFF)
if(BUILD_DOCS)
find_package(Doxygen REQUIRED)
# ... Doxygen config ...
endif()
# Summary of CMake commands:
# 1. cmake_minimum_required: Enforces minimum CMake version.
# 2. project(): Defines project name and version.
# 3. add_executable(): Creates the final binary target.
# 4. target_link_libraries(): Links libraries to targets.
# 5. target_compile_options(): Adds compiler-specific flags.
# 6. file(GLOB...): Used for source files (use with care).
# 7. option(): Defines configurable build options.
# ...........................................................
# ...........................................................
# ...........................................................
# ...........................................................
# ...........................................................
</code></pre>
<div class="interactive-sim">
<h3>CMake Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_8');
out.innerHTML = 'cmake ..;<br>';
setTimeout(() => out.innerHTML += '>> Generating Makefiles...<br>>> Build files ready!', 800);
">Run Simulation</button>
<div id="sim-output-cpp2_8" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp2_7.html" style="text-decoration: none; color: #6c757d;">← Previous: Modern C++: Auto & Lambdas</a>
<a href="#" data-file="Cpp2_9.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Testing: Unit Testing →</a>
</div>
</footer>
</article>