Repository navigation
Expand file tree
/
Copy pathcpp.tmd
More file actions
80 lines (58 loc) · 1.97 KB
/
Copy pathcpp.tmd
File metadata and controls
80 lines (58 loc) · 1.97 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
---
title: Tmdoc - examples for the C++ programming language
author: Detlef Groth
date: 2025-09-14 16:11
include-before: |
<style>
body { max-width: 1000px; font-family: Candara, sans-serif; }
pre { font-family: Consolas; monospaced; }
table { min-width: 400px; border-spacing: 5px; border-collapse: collapse; }
</style>
<center>some links on top</center>
---
`tcl include header.md`
## Introduction
Below are some C++ coding examples.
## Hello World
```{.shell cmd="g++ %i -o cpp-hello&&./cpp-hello > %o" chunk.ext=cpp ext=txt}
#include <iostream>
int main() {
std::cout << "Hello C++ World!" << std::endl;
return(0);
}
```
## Image Creation
Here an example which does create an image using the
[Cimg](https://cimg.eu) library. On a Linux Debian based system you should be able to install the library like this:
```
sudo apt install cimg-dev imagemagic
```
The imagemagick tools are required to write for instance various output
formats like png in the following example.
```
'``{.shell cmd="g++ -O2 %i -o cimg&&./cimg -lX11 -lpthread -lm " chunk.ext=cpp ext=png include=false}
... code ..
'``
```
Output:
```{.shell label=cimg cmd="g++ -O2 %i -o %b -lX11 -lpthread -lm &&./%b %o" chunk.ext=cpp ext=png include=false}
#include <CImg.h>
using namespace cimg_library;
int main(int argc, char **argv) {
// Create a 300x200 RGB image, initialized to black
CImg<unsigned char> img(300, 200, 1, 3, 0);
// Define colors
const unsigned char red[] = {255, 0, 0};
const unsigned char white[] = {255, 255, 255};
// Draw a filled red rectangle (box) at (50,50) with size 200x100
img.draw_rectangle(50, 50, 249, 149, red);
// Draw "Hello World" in white, at position (90, 90)
img.draw_text(90, 90, "Hello World", white);
// Display the image in a window
//img.display("Hello World Example");
// Optionally, save the image to a file
img.save(argv[1]);
return 0;
}
```
