From 8cbf6437f5a15647ee104f2394f59cd9bd16a040 Mon Sep 17 00:00:00 2001 From: Robert Jeutter Date: Sat, 1 Jan 2022 21:37:36 +0100 Subject: [PATCH] small gcc introduction --- GCC Intro/hello | Bin 0 -> 17320 bytes GCC Intro/hello.c | 6 ++ GCC Intro/hello.cpp | 8 +++ GCC Intro/makefile | 10 +++ GCC Intro/readme.md | 144 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+) create mode 100755 GCC Intro/hello create mode 100644 GCC Intro/hello.c create mode 100644 GCC Intro/hello.cpp create mode 100644 GCC Intro/makefile create mode 100644 GCC Intro/readme.md diff --git a/GCC Intro/hello b/GCC Intro/hello new file mode 100755 index 0000000000000000000000000000000000000000..7e609897b10248502c27442348db1c62c99e9efc GIT binary patch literal 17320 zcmeHPZ*W{y6~DFWj5os-!>Qn|kh%o3_(Q4u#wvjqj+OEHI-#xEy z-)*B%m16H>(OrK(vV^ z_-qiX#eB5qNzCL2OaiW1-mEO9)e853l3qPk%Ft^}STN-t5+%L4NOa-TFKoOIBH1ylBS zJM<_nAGYu?_bY#Mjl&}7f= zz6ck|nD_rxStpFjYw=e{>YiBKX|_r~$?CFFSX>K((X3-Hgi{Hlt2uP9 z#M8-asIR+!AU&Xm01Oz(Z1}_3j}F56CcW>rXgLR+rUUdjn$8&l5s7jzqGJ&~V#LQ1 z1|ma^J&9B#nYa&kuale#)JPKpwnW?kRl-{9XwQTAXSd*nZ!-6lfw zn*N#3k`L5a`!y_09&wu*AL3q#IzP|3m5GI9<4(orb(Pn%rDKXZh#z!?~}-Ic>vn!&W(G z!>_PVVa;n=j~3hTgG!!!+Siq5Z8)!4r1rcGr}c@;VH+<0jxxigZUo#2xDjw8;6}iW zz(+0u|EO90Z*Ah;YVEPAx7G`xed(}KS$IvGc)9whyqChJ4B*+q$Fm?c4T9u{i8DL% zheDw+CEN4`Xm;jxt4&`tW@nCAZTf;SJM$d3VXaqM>k`r)+SfYTb&mEDN4v`2ZvI<1 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 zP+Vvot;C?5cEl4$DXF=-h^9wIlW`*+^S3p%HkB8moQs4W$z&q?^?1t2>=%17k&(C_%Z-fe zhl;JGL((V`9j2f5{n61;nK07Rym>SckH+^UvT;3{P9gmy7e#6Za!GpHbUl$KeKcE| zb;|58kfK6Jf>J)vLSjrL6+=FZsYmUmL_+9Wd$xB3d!UjV(!;t;2GRBI{$OuN4{htB zG#VZGO?pUEpw`td^sdir3-*RP!7J)?tQe4qIF^U)-Q5ErB+CRldP1-ok420K@^H4J zv#~@<&t>DJZqHK*1Y}x@N=2a?y}ZO6Wk;pAU@)_3eHg)@JRs@N%#tZ}cNsZ44qwO! zvSUcY`;WILBWo2qY>FFr1)?oJ1qRM{VBLGsgW@`_r^0>JO@P$9lT6D86gE# z8%-L*FCQ*Hs{XxcG^vt}M};5xRcOkH`7>!br~UC^^;{3fV2xX*4f6pvHMkB}k&y(9 znd%s1;m6`Kf(4^=(Yp+UzFA>O$KPzT@b`MAypL3DDa&=DAHzj^H`eFx|4e=0*$eO2 z7;8nvuD=uOFcqfszR+Iy?=)j!RP6e^pJ3`^K}DVVcLM(>_I})d-XAkvqx5)x?CgI8 zeA*kcKJQPMwo!0VsE<@-ecs0=fYB|pzO|20f8cok4G~$CIc|#2p+#m|-*@Qq{*Ecj&!zvYLw{T; zFb%MxqHK@p3l9Cmiovvs2gae#W?ypX^S+WP{|?3S&iMUY>054mN>b4)Rg;NR|5sqp z__6+SpDD?7&}>?tPW`vRvFr1GmZ?TeTjBY`&?!`CZ>W|XYoEq>anoqKlkg0(4||n^wsZgQ);6y3ZA`if0)jP zvRz*Ulys9x*$>-bJ#H_CGGXjL5HLBlO23|r+e*Mzr|MGWUx`~>*1yq0%nF;cQ=U6^ z(SR~otpax~m>Y0$|9Smc2#ot~*Vohr?~4v5`(;_$4mw06>z7+hiIoloM^pR + +int main() { + printf("Hello, world!\n"); + return 0; +} \ No newline at end of file diff --git a/GCC Intro/hello.cpp b/GCC Intro/hello.cpp new file mode 100644 index 0000000..5bd6cb4 --- /dev/null +++ b/GCC Intro/hello.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; + +int main() +{ + cout << "Hello, world!" << endl; + return 0; +} \ No newline at end of file diff --git a/GCC Intro/makefile b/GCC Intro/makefile new file mode 100644 index 0000000..4ac641e --- /dev/null +++ b/GCC Intro/makefile @@ -0,0 +1,10 @@ +all: hello.exe + +hello.exe: hello.o + gcc -o hello.exe hello.o + +hello.o: hello.c + gcc -c hello.c + +clean: + rm hello.o hello.exe \ No newline at end of file diff --git a/GCC Intro/readme.md b/GCC Intro/readme.md new file mode 100644 index 0000000..de27086 --- /dev/null +++ b/GCC Intro/readme.md @@ -0,0 +1,144 @@ +# GCC Intro +The GNU C and C++ compiler are called gcc and g++, respectively. + +The GNU Toolchain, including GCC, is included in all Unixes. It is the standard compiler for most Unix-like operating systems. Test its installed with +```bash +gcc --version +``` + +## Hello World - C +The first program is a simple "Hello World" in C. To compile the hello.c: +```bash +gcc hello.c +``` +This compiles and links source file hello.c into executable a. The default output executable is called 'a.out'. To run the program: +```bash +# grant executable rights +chmod a+x a.out +# run +./a.out +``` +To specify the output filename, use -o option: +```bash +gcc -o hello hello.c +chmod a+x hello +./hello +``` + +## Hello World - C++ +You need to use g++ to compile C++ program, as follows. We use the -o option to specify the output file name. It's very similar to gcc. +```bash +g++ -o hello hello.cpp +chmod a+x hello +./hello +``` + +There are a few more GCC Compiler Options. A few commonly-used GCC compiler options are: +- o: specifies the output executable filename. +- Wall: prints "all" Warning messages. +- g: generates additional symbolic debuggging information for use with gdb debugger. + +## Compile multiple source files +Suppose that your program has two source files: file1.cpp, file2.cpp. You could compile all of them in a single command: +```bash +g++ -o myprog.exe file1.cpp file2.cpp +``` +Usually compile each of the source files separately into object file, and link them together in the later stage. In this case, changes in one file does not require re-compilation of the other files. +```bash +g++ -c file1.cpp +g++ -c file2.cpp +g++ -o myprog.exe file1.o file2.o +``` + +## GNU Make +The `make` utility automates the mundane aspects of building executable from source code. `make` uses a so-called makefile, which contains rules on how to build the executables. + +Simply create a file named `makefile` in the same directory as the source file, this file contains rules to build the exectable. +To run the the `make` utility use +```bash +make +``` +Running make without argument starts the target "all" in the makefile. + +### Rulesets +A makefile consists of a set of rules. A rule consists of 3 parts: a target, a list of pre-requisites and a command, like +```bash +target: pre-req-1 pre-req-2 ... + command +``` +You can also specify the target to be made in the make command by using its targets name of the makefile +```bash +make clean +``` + +### Comments +Makefiles can be commented by prevailing a line with `#` like +```makefile +# this is a comment +target: requisites ... + command +``` + +### Variables +Next to that you can use Variables for a more programatically way of building your projects. Variables begin with a `$` and are enclosed within parantheses or braces +```makefile +$(CC), $(CC_FLAGS), $@, $^. +``` +Automatic variables are set by make after a rule is matched. There include: +- `$@`: the target filename. +- `$*`: the target filename without the file extension. +- `$<`: the first prerequisite filename. +- `$^`: the filenames of all the prerequisites, separated by spaces, discard duplicates. +- `$+`: similar to $^, but includes duplicates. +- `$?`: the names of all prerequisites that are newer than the target, separated by spaces. + +E.g. an advanced makefile could look like +```makefile +all: hello.exe + +# $@ matches the target; $< matches the first dependent +hello.exe: hello.o + gcc -o $@ $< + +hello.o: hello.c + gcc -c $< + +clean: + rm hello.o hello.exe +``` + +### Virtual Paths +Use VPATH (uppercase) to specify the directory to search for dependencies and target files, e.g. +```makefile +# Search for dependencies and targets from "src" and "include" directories +# directories are separated by space +VPATH = src include +``` + +Use vpath (lowercase) to be more precise about the file type and its search directory, e.g. +```makefile +# Search for .c files in "src" directory; .h files in "include" directory +# The pattern matching character '%' matches filename without the extension +vpath %.c src +vpath %.h include +``` + +### Pattern Rules +A pattern rule, which uses pattern matching character `%` as the filename, can be applied to create a target, if there is no explicit rule. For example, +```makefile +# Applicable for create .o object file. +# '%' matches filename. +# $< is the first pre-requisite +# $(COMPILE.c) consists of compiler name and compiler options +# $(OUTPUT_OPTIONS) could be -o $@ +%.o: %.c + $(COMPILE.c) $(OUTPUT_OPTION) $< + +# Applicable for create executable (without extension) from object .o object file +# $^ matches all the pre-requisites (no duplicates) +%: %.o +$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@ +``` + + +Find more informations at [GNU GCC](http://gcc.gnu.org/) \ No newline at end of file