layout: true
Class 9: Libraries
--- class: center, middle # Libraries ### Class 9 --- # Overview 1. Announcements 2. Review 3. Q&A 4. Basic assignment --- # Announcements * Python assignments due today * Libraries assignments due June 19 --- # Review * Static libraries go directly inside the executable * Libraries are an inherent part of the executable * Dynamic/shared libraries are referred to by the executable * Libraries are loaded at load/runtime --- # Review * Really easy to link a library * Toss `-lname` _at the end of the compilation command_ * `-lpng` for `libpng.so` or `libpng.a` * `-lm` for `libm.so` or `libm.a` * (Linux) `-l:libname.a` is an explicit way to specify library file to link * `-Ldir` can specify additional directories to look for libraries * You can also link against a library by providing the path to the library file as an argument (particularly useful for static libraries) --- # Exercise * Download the following [file](https://www.eecs.umich.edu/courses/eecs201/sp2026/files/examples/lib.tar.gz) ```bash https://www.eecs.umich.edu/courses/eecs201/sp2026 |- /files/examples/lib.tar.gz ``` * Compile with `make` and run: ```bash $ make g++ -g -std=c++20 -o calc.exe calc.cpp $ ./calc.exe calc> 3 + 7 10 ``` --- # Exercise * It would be nice to add features such as using the up/down keys to scroll through input history, using left/right to navigate a line, and so on * The standard Python interpreter has these features, for example * Luckily, there's a library for that: `readline` * It's probably already installed on your machine * Find documentation for how to modify `calc.cpp` to incorporate `readline` * Edit the `Makefile` to properly compile with the `readline` library --- # Review ### Creating a library * Create object code with `-c` * e.g. `gcc -c -o file.o file.c` * Dynamic library object code requires `-fPIC` flag * e.g. `gcc -c -fPIC -o file.o file.c` * Static: `ar rcs libname.a file.o` * Dynamic: `gcc -shared -o libname.so file.o` --- class: center, middle # Q&A --- class: center, middle # Basic assignment