From 576a223a295c3aa447b8d96411b68a4adac8844e Mon Sep 17 00:00:00 2001 From: androiddrew Date: Thu, 19 Mar 2020 23:36:24 -0400 Subject: [PATCH] Adding erf files --- 21st_century_c/first_compile/README.md | 84 +++++++++++++++++++++++++ 21st_century_c/first_compile/erf | Bin 0 -> 11000 bytes 21st_century_c/first_compile/erf.c | 10 +++ 3 files changed, 94 insertions(+) create mode 100644 21st_century_c/first_compile/README.md create mode 100755 21st_century_c/first_compile/erf create mode 100644 21st_century_c/first_compile/erf.c diff --git a/21st_century_c/first_compile/README.md b/21st_century_c/first_compile/README.md new file mode 100644 index 0000000..02dd518 --- /dev/null +++ b/21st_century_c/first_compile/README.md @@ -0,0 +1,84 @@ +# Compilation + +## Things you should know + +- There is an implicit -lc passed to the compiler for the linker to link the standard libc. +- Adding the -g flag adds symbols for debugging. Without it gdb can't give you variable or function names. + - There is some overhead in size of the execuable when using this command but it is worth the ability debug +- Add the standard `-std=gnu11` you are targeting to the compile command. +- `-Wall` adds compiler warnings. +- `Werror` will actually treat warnings as errors preventing compilation. Trust the compiler. If an error or + warning occurs fix it. + +## Paths + +The linker will normally just look for the header and object files in the file system hierarchy's usual places. +In the `erf.c` program we can explicitly pass the math and the libc libraries to the linker like so: + +``` +gcc erf.c -o erf lm -g -Wall -O3 +``` + +If we have a library in a non-standard location you have to tell the compiler where the header files are +and you have to tell linker where it is located. + +``` +gcc -I/usr/local/include use_useful.c -o use_useful -L/usr/local/lib -luseful +``` + +- `-I` Adds the given path to the include search path that the compiler searches for header files. +- `-L` Adds the library (object code) to the Linker's search path. +- `-l` Says to link the `useful` library + +### Order matters in dependencies + + +If you have `specific.o` depend on `lbroad` which depends on `lgeneral` then you need: +``` +gcc specific.o -lbroad -lgeneral +``` + +Any other order will probably fail. + +### Hunting for libraries + +`pkg-config` addresses many problems by creating a repository of flags and locations that packages self-report +as being necessary for compilation. + +``` +pkg-config --libs gsl libxml-2.0 +pkg-config --cflags gsl libxml-2.0 + +> -lgsl -lgslcblas -lm -lxml2 +> -I/usr/include/libxml2 +``` + + +If these libraries are installed it will produce exactly the flags needed to compile using GSL and LibXML2. This +example command also demonstrates how GSL is dependent on the GNU Basic Linear Algebra Subprograms which in turn +is dependent on the math.c library (`-lm`). + +Since we don't see a `-L` we know that all the libraries are located in the typical `lib` locations, but the `-I` +tells use that the header files for `libxml2` are in a different location. + +When you want to use `pkg-config` to evaluate the at the shell we can use backticks or `$()`(command substituation). +The `$(...)` is the more common (modern) syntax, but both are valid. + +``` +gcc `pkg-config --cflags --libs gsl libxml2` -o specific specific.c +``` +This first evaluates the backticks which is then used as input to the command producing: +``` +gcc -I/usr/include/libxml2 -lgsl -lgslcblas -lm -lxml2 -o specific specific.c +``` + +As you can probably tell the more libraries your program begins using, the more locations we need to include/link to. + +### Path Environment Variables +You can also set the path for the C compiler and Linker with environmental variables. They tend to be different on each OS, +so you need to read the `man` for your compiler. + +1. `man gcc` +2. Search for pattern: `/ENVIRONEMT` + + diff --git a/21st_century_c/first_compile/erf b/21st_century_c/first_compile/erf new file mode 100755 index 0000000000000000000000000000000000000000..8b55a02d4fb9c14bcf0cad7ec9033b1245420f47 GIT binary patch literal 11000 zcmeHNeQaCTb-#}v5^32KB{{JiuklM`C30#}--?{fF)2$9naYmqNNt+Letsk$DaI5j zJU;p(1y(n1x@t=@wcQW{TY+u;a*fW`;AHLAHd*F% zi?!dm?;eVLqSkc4fc@bMym!v|-E+=8_k-`9dmleKHND@bX@ZMS+$Ts%2jUWP??No! zCPjsfi?E1`9)q`#Q$D;7%CQ+=n#$NI)bVwG}iNcNljlkWWoXJtz~jyfcYc{46r162R@ zrE`D$mA|K+9nkD-qW!*g@HXIj6McdH%~N;Agkj|8 z%O#`Yn6_gWRFQEj7VJXF$)TPgRX=YQO5*%AX1++aSJdW|LQG; zFlp>jFzY>g#Wm5S` zdo=hrV6J`@s)HAjXVKo^4^xXTA5AX4nmqgcA03)Wy>#IM)RQk=JS53W@jzT${SD~9 zMeQ%&jSkHw`vyRgX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qTar? z|56R~vZ11E$Ml-Vp7YLLW$&GD)t#Ex>)^;2_n-5@UGLJ~4~OJ)pyttK*!~d@&h0bZ zsO&vz;uETUuV1!feaip67}foevhU=MDFd(Wo$J_tv&%L34`s*1Yd^Y<{l(3$1%0ll zFJAkEviJ7$g!<5x=O4Cm!uCw%A$5HNivJ!Cj;ZMKzItOjMo9KF;Z~^^#FAS0w*wu; p#p_O0 //erf, sqrt +#include //printf + +int main(){ + printf("The integral of a Normal(0,1) distribution " + "between -1.96 and 1.96 is: %g\n", erf(1.96*sqrt(1/2.))); + + return 0; +} +