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 0000000..8b55a02 Binary files /dev/null and b/21st_century_c/first_compile/erf differ diff --git a/21st_century_c/first_compile/erf.c b/21st_century_c/first_compile/erf.c new file mode 100644 index 0000000..22f0cd6 --- /dev/null +++ b/21st_century_c/first_compile/erf.c @@ -0,0 +1,10 @@ +#include //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; +} +