|
|
@ -1,4 +1,114 @@ |
|
|
|
# Lecture 4: An overview of C and C++ |
|
|
|
A long this lecture let us test two basic Hello World scripts on C and C++, respectively. The idea is to remember the compilation chain process to obtain an executable application. |
|
|
|
|
|
|
|
## C-code |
|
|
|
## The Hello-Wolrd on C |
|
|
|
The code used for this example is |
|
|
|
```c |
|
|
|
//Gerardo Marx 24/04/2021 |
|
|
|
#include <stdio.h> |
|
|
|
int main(int argc, char *argv[]){ |
|
|
|
printf("Hello World from BeagleBoard!!\n"); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
``` |
|
|
|
|
|
|
|
then to compile on an executable code use |
|
|
|
```bash |
|
|
|
$ gcc hello-world.c -o <output-name> |
|
|
|
``` |
|
|
|
here the `<output-name>` is the name that you want for your executable code. |
|
|
|
|
|
|
|
Now it is possible to execute your new code by using: |
|
|
|
```bash |
|
|
|
$ ./<output-name> |
|
|
|
$ Hello World from BeagleBoard!! |
|
|
|
``` |
|
|
|
|
|
|
|
## The Chain process |
|
|
|
|
|
|
|
In some cases you will need to get access to the intermediate files that are produced by your code during the compilation process. Thus, following the next steps you can obtain the processed, assambled and output files. |
|
|
|
|
|
|
|
```bash |
|
|
|
➜ l4-CAndCpp git:(master) ls -l |
|
|
|
total 32 |
|
|
|
-rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md |
|
|
|
-rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c |
|
|
|
-rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c |
|
|
|
-rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp |
|
|
|
-rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp |
|
|
|
➜ |
|
|
|
➜ l4-CAndCpp git:(master) g++ -E hello-world.cpp > processed.cpp |
|
|
|
➜ l4-CAndCpp git:(master) ✗ ls -l |
|
|
|
total 688 |
|
|
|
-rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md |
|
|
|
-rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c |
|
|
|
-rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c |
|
|
|
-rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp |
|
|
|
-rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp |
|
|
|
-rw-r--r-- 1 debian debian 668283 Apr 24 15:42 processed.cpp |
|
|
|
``` |
|
|
|
then, by using the `less` command you visualize the file's content |
|
|
|
```bash |
|
|
|
# 1 "hello-world.cpp" |
|
|
|
# 1 "<built-in>" |
|
|
|
# 1 "<command-line>" |
|
|
|
# 1 "/usr/include/stdc-predef.h" 1 3 4 |
|
|
|
# 1 "<command-line>" 2 |
|
|
|
# 1 "hello-world.cpp" |
|
|
|
|
|
|
|
|
|
|
|
# 1 "/usr/include/c++/8/iostream" 1 3 |
|
|
|
# 36 "/usr/include/c++/8/iostream" 3 |
|
|
|
``` |
|
|
|
by pressing the `SPC` key you will continue to the next lines, and by pressing the `q` key you will quit and close the file exploration through `less` command. |
|
|
|
|
|
|
|
The same can be done for the assambled file: |
|
|
|
```bash |
|
|
|
➜ l4-CAndCpp git:(master) ✗ g++ -S processed.cpp -o helloworld.s |
|
|
|
➜ l4-CAndCpp git:(master) ✗ ls -l |
|
|
|
total 692 |
|
|
|
-rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md |
|
|
|
-rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c |
|
|
|
-rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c |
|
|
|
-rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp |
|
|
|
-rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp |
|
|
|
-rw-r--r-- 1 debian debian 3191 Apr 24 16:19 helloworld.s |
|
|
|
-rw-r--r-- 1 debian debian 668283 Apr 24 15:42 processed.cpp |
|
|
|
``` |
|
|
|
|
|
|
|
and for the output file: |
|
|
|
```bash |
|
|
|
➜ l4-CAndCpp git:(master) ✗ g++ -c helloworld.s |
|
|
|
➜ l4-CAndCpp git:(master) ✗ ls -l |
|
|
|
total 696 |
|
|
|
-rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md |
|
|
|
-rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c |
|
|
|
-rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c |
|
|
|
-rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp |
|
|
|
-rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp |
|
|
|
-rw-r--r-- 1 debian debian 2356 Apr 24 16:20 helloworld.o |
|
|
|
-rw-r--r-- 1 debian debian 3191 Apr 24 16:19 helloworld.s |
|
|
|
-rw-r--r-- 1 debian debian 668283 Apr 24 15:42 processed.cpp |
|
|
|
``` |
|
|
|
|
|
|
|
and finally obtain the executable file by: |
|
|
|
```bash |
|
|
|
➜ l4-CAndCpp git:(master) ✗ g++ helloworld.o -o hello-out |
|
|
|
➜ l4-CAndCpp git:(master) ✗ ls -l |
|
|
|
total 708 |
|
|
|
-rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md |
|
|
|
-rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c |
|
|
|
-rwxr-xr-x 1 debian debian 8944 Apr 24 16:22 hello-out |
|
|
|
-rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c |
|
|
|
-rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp |
|
|
|
-rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp |
|
|
|
-rw-r--r-- 1 debian debian 2356 Apr 24 16:20 helloworld.o |
|
|
|
-rw-r--r-- 1 debian debian 3191 Apr 24 16:19 helloworld.s |
|
|
|
-rw-r--r-- 1 debian debian 668283 Apr 24 15:42 processed.cpp |
|
|
|
``` |
|
|
|
Now you can execute it: |
|
|
|
```bash |
|
|
|
➜ l4-CAndCpp git:(master) ✗ ./hello-out |
|
|
|
Hello world from beagleboard on c++!! |
|
|
|
``` |
|
|
|
|