You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.2 KiB

3 years ago
  1. # Lecture 4: An overview of C and C++
  2. 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.
  3. ## The Hello-Wolrd on C
  4. The code used for this example is
  5. ```c
  6. //Gerardo Marx 24/04/2021
  7. #include <stdio.h>
  8. int main(int argc, char *argv[]){
  9. printf("Hello World from BeagleBoard!!\n");
  10. return 0;
  11. }
  12. ```
  13. then to compile on an executable code use
  14. ```bash
  15. $ gcc hello-world.c -o <output-name>
  16. ```
  17. here the `<output-name>` is the name that you want for your executable code.
  18. Now it is possible to execute your new code by using:
  19. ```bash
  20. $ ./<output-name>
  21. $ Hello World from BeagleBoard!!
  22. ```
  23. ## The Chain process
  24. 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.
  25. ```bash
  26. ➜ l4-CAndCpp git:(master) ls -l
  27. total 32
  28. -rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md
  29. -rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c
  30. -rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c
  31. -rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp
  32. -rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp
  33. ➜ l4-CAndCpp git:(master) g++ -E hello-world.cpp > processed.cpp
  34. ➜ l4-CAndCpp git:(master) ✗ ls -l
  35. total 688
  36. -rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md
  37. -rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c
  38. -rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c
  39. -rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp
  40. -rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp
  41. -rw-r--r-- 1 debian debian 668283 Apr 24 15:42 processed.cpp
  42. ```
  43. then, by using the `less` command you visualize the file's content
  44. ```bash
  45. # 1 "hello-world.cpp"
  46. # 1 "<built-in>"
  47. # 1 "<command-line>"
  48. # 1 "/usr/include/stdc-predef.h" 1 3 4
  49. # 1 "<command-line>" 2
  50. # 1 "hello-world.cpp"
  51. # 1 "/usr/include/c++/8/iostream" 1 3
  52. # 36 "/usr/include/c++/8/iostream" 3
  53. ```
  54. 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.
  55. The same can be done for the assambled file:
  56. ```bash
  57. ➜ l4-CAndCpp git:(master) ✗ g++ -S processed.cpp -o helloworld.s
  58. ➜ l4-CAndCpp git:(master) ✗ ls -l
  59. total 692
  60. -rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md
  61. -rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c
  62. -rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c
  63. -rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp
  64. -rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp
  65. -rw-r--r-- 1 debian debian 3191 Apr 24 16:19 helloworld.s
  66. -rw-r--r-- 1 debian debian 668283 Apr 24 15:42 processed.cpp
  67. ```
  68. and for the output file:
  69. ```bash
  70. ➜ l4-CAndCpp git:(master) ✗ g++ -c helloworld.s
  71. ➜ l4-CAndCpp git:(master) ✗ ls -l
  72. total 696
  73. -rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md
  74. -rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c
  75. -rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c
  76. -rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp
  77. -rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp
  78. -rw-r--r-- 1 debian debian 2356 Apr 24 16:20 helloworld.o
  79. -rw-r--r-- 1 debian debian 3191 Apr 24 16:19 helloworld.s
  80. -rw-r--r-- 1 debian debian 668283 Apr 24 15:42 processed.cpp
  81. ```
  82. and finally obtain the executable file by:
  83. ```bash
  84. ➜ l4-CAndCpp git:(master) ✗ g++ helloworld.o -o hello-out
  85. ➜ l4-CAndCpp git:(master) ✗ ls -l
  86. total 708
  87. -rw-r--r-- 1 debian debian 231 Apr 24 13:28 Readme.md
  88. -rwxr-xr-x 1 debian debian 8144 Apr 24 13:33 hello-c
  89. -rwxr-xr-x 1 debian debian 8944 Apr 24 16:22 hello-out
  90. -rw-r--r-- 1 debian debian 137 Apr 24 13:33 hello-world.c
  91. -rw-r--r-- 1 debian debian 183 Apr 24 13:49 hello-world.cpp
  92. -rwxr-xr-x 1 debian debian 8944 Apr 24 13:58 hellocpp
  93. -rw-r--r-- 1 debian debian 2356 Apr 24 16:20 helloworld.o
  94. -rw-r--r-- 1 debian debian 3191 Apr 24 16:19 helloworld.s
  95. -rw-r--r-- 1 debian debian 668283 Apr 24 15:42 processed.cpp
  96. ```
  97. Now you can execute it:
  98. ```bash
  99. ➜ l4-CAndCpp git:(master) ✗ ./hello-out
  100. Hello world from beagleboard on c++!!
  101. ```