CIS*2750 Lecture 2a - The C Tool Chain PDF
Document Details
![ComfortableBowenite2676](https://quizgecko.com/images/avatars/avatar-16.webp)
Uploaded by ComfortableBowenite2676
University of Guelph
Tags
Summary
These lecture notes cover the C tool chain, outlining the different stages involved in the compilation process. The document also includes information about creating intermediate files and provides examples of compiling a list library.
Full Transcript
CIS*2750 Lecture 2a: the C tool chain Based on CIS*2750 notes from previous generations of CIS*2750 instructors Announcement Volunteer note taker needed Student Accessibility Services are looking for volunteer notetakers who are willing to share their class notes....
CIS*2750 Lecture 2a: the C tool chain Based on CIS*2750 notes from previous generations of CIS*2750 instructors Announcement Volunteer note taker needed Student Accessibility Services are looking for volunteer notetakers who are willing to share their class notes. In being a volunteer notetaker, you can contribute to academic success and make a di erence in the lives of your fellow peers. The volunteer notetaking position is a one semester commitment and volunteers can request to be removed from the program at any time. As a volunteer notetaker, your course notes will only be shared with students registered with our o ce, who have a documented disability and who have been approved for notetaking services. Link: uoguel.ph/notetaker 2 ff ffi What is a “tool chain”? Developing a software project is inherently tied to the tools used to develop the project - language(s), libraries, as well as the additional software that forms part of the development tool chain The tool chain is an informal term describing the sequence of software tools that converts your source code (one or more text les) into binary code On the surface it consists of just the compiler, but things are more complex than that - you use a sequence (chain) of tools 3 fi Lecture overview In this course, we will use C for the "native" portion (A1-A2), and Python/SQL for the UI/ database portion (A3). C covers the rst half of the course, so we will look at the Big Picture of the tools that many C programmers - at lest the ones working with *nix-family operating systems - use regularly Expose them all to view Some are “hiding” inside others! How they “chain” together in sequence How to control them Bring them up from the level of ritualized mumbo-jumbo to one of knowledge 4 fi The usual suspects C Preprocessor C Compiler Linker Loader Make 5 IDE The same tools are in use “under the hood,” but normally managed for you by the IDE “IDE” = Interactive Development Environment Windows - Visual Studio (becoming available on other platforms) macOS - Xcode Multi-platform: Eclipse, IntelliJ, Codelite, CodeBlocks, etc. Di erent languages have di erent tool chains Although often one IDE can accommodate more than one language/toolchain 6 ff ff A word on compilers There is more than one C compiler out there Popular FOSS (free, open-source) GNU Compiler Collection (GCC) compiler is a widespread and popular choice Free, open-source, available on many platforms Often installed by default with Linux Clang is a more recent compiler and an alternative to GCC Free, open-source, available on many platforms Installed on macOS with Xcode Both implement the C standards, and provide custom extensions There are MANY others We mostly use the C compiler from GCC as the platform in SoCS 7 The Big Picture Components of the C computer toolchain.h.c Your project files.o/.obj.so/.dll all appears to be “gcc”.i.s cpp* gcc* gas ld* C Preprocessor C Compiler (Assembler) Linker prog CPPFLAGS CFLAGS (SFLAGS) a.out LDFLAGS.exe.h.a/.lib System Libraries ld.so dlopen.so/.dll Loader *Managed by Make Run time Process Memory Build time data flow 8 Note: creating intermediate files The intermediate les discussed here (.i,.s, and.o) are not always generated by default We sometimes want to create.o les We almost never need the.i and.s les If you want to see them, compile your code with the -save-temps ag 9 fi fi fi (1) C Preprocessor Purpose: Interpret all the # directives in the.h and.c les before compiler sees the source code (intermediate.i le) #include: merge in header les #define: macros (with and without arguments) macros are “expanded” by processor through nd-and-replace #ifdef, if, else, endif: conditional compilation Exception: #pragma is passed on as compiler directive (ignored if compiler does not recognize) Note: The C preprocessor is often abbreviated to "cpp" - which is confusing, because "cpp" also happens to be a common acronym for C++ (and extension for C++ source les). We are not using C++ in this course, so "cpp" for us means "C preprocessor" 10 fi fi fi Prevent multiple/circular includes “Best practice”.h pattern in le headername.h #ifndef HEADERNAME_H #define HEADERNAME_H...body of.h file... #endif On the rst #include “headername.h”, symbol HEADERNAME_H gets de ned If it is #included again (even indirectly), the le’s contents is skipped 11 fi fi fi Headers and paths The #include directive has slightly di erent syntax for di erent headers #include This variant is used for system header les It searches for a le named le in a standard list of system directories #include "file" This variant is used for header les of your own program It searches for a le named le as follows: rst in the directory containing the current le then in the "quote" directories then the same directories used for < le>. 12 fi fi fi fi fi fi fi ff Headers and paths You must not include the path to the le in the #include statement e.g. #include "../includeDir/Parser.h" - do not do this! If the header and/or the le that uses the header move to a di erent location, the path breaks - and your code no longer compiles. That's bad. Instead, you always include the le itself, e.g. #include "Parser.h" Let the compiler toolchain take care of the paths 13 fi fi fi Headers and paths A directory that contains a header le can be passed to the compiler using the -I option: e.g. gcc -I../includeDir myFile.c These paths can be relative or absolute You can pass more than one include directory: gcc -I../includeDir -I/home/username/stuff/otherIncludeDir myFile.c 14 fi (2) C compiler Purpose: Compile C language source code (.i le) into assembly language (.s) Diagnoses abuses of language, and issues warnings and/or errors Intermediate.i and.s les normally deleted after assembly → not seen by user unless requested (-save-temps option) See the sample.c,.i,.s, etc. les in the GCC_intermediate_files.zip example posted on the course website (Week 1) Compile with gcc -save-temps test.c 15 fi fi (2+) Assembler = [G]AS Purpose: Assemble assembly code (.s) from compiler into object code (.o) This tool step is normally transparent There are options for controlling it, but rare to use them You can insert assembly statements into your C program, e.g., to utilize vector instructions for the CPU's vector processing unit 16 (3) Linker (link editor) = ld Purpose: to stitch together objects (.o) into a single binary program le - executable or library All the.o object les that make up the user program, plus All the referenced system libraries Linker creates libraries Static (.a /.lib): inserted in the executable le Dynamic (.so /.dll): linked in at run time We will see this in one of the upcoming lectures 17 fi fi What is the linker doing? In the object les, there are tables of external references (e.g., call “printf”) It reads all the libraries until it nds a matching external de nition ( the actual code of the printf() function in stdio.h) Libraries come in two avours - static and dynamic. They are linked di erently: Static linking: It pulls the de nitions (function code and global variables) into the program le, and xes up all the refs to point to their locations Dynamic linking: It “makes a note” of what library le to get the de nition from at run time 18 fi fi fl fi fi Linking and paths Linking a library The linker will only automatically link the standard C libraries - excluding the C math library The names of all other libraries must be explicitly passed to the linker using the -l option For example, to link the C math library, you add the -lm option to the compiler where m is the name of the math library the convention for library naming will be discussed in a later lecture on creating shared libraries E.g. gcc myCode.c -lm //Includes the standard math library gcc myCode.c -lsomeLibrary //Includes the standard math library But how does the linker know where the libraries m and someLibrary are? 19 Linking and paths Giving the linker a path to a library The linker has to know where the disk location of the binary les containing the libraries By default, it only looks in a small number of directories containing system libraries, e.g. /usr/lib, /usr/local/lib, etc. Notably, it does not look in the current directory! So if myCode.c and the someLibrary are in the same directory, and you run gcc myCode.c -lsomeLibrary, you will get an error: /usr/bin/ld: cannot nd -lsomeLibrary: No such le or directory 20 fi Linking and paths Giving the linker a path to a library We can explicitly give the linker additional paths to look in using the -L ag e.g. gcc myCode.c -L/path/to/a/library -lsomeLibrary So if myCode.c and the someLibrary are in the same directory, and you are running the compiler from that directory, you need to run: gcc myCode.c -L. -lsomeLibrary 21 Recap: preprocessor and linker paths -I - provides paths for the preprocessor (locations of header les for #include, AKA include paths) -L - provides paths to the linker (locations of libraries) -l - provides the names of libraries that the linker has to link with 22 Why is gcc involved in tools 1-3? GCC acts as a front end for {cpp, gcc, ld} You keep executing “gcc” to get all these tools invoked Nonetheless, gcc is not cpp or ld; it is just calling them for you! Reason: convenience gcc knows where all standard libraries are installed, so it secretly beefs up *FLAGS for you, otherwise your commands would be much longer/messier and less portable 23 (4) Loader = ld.so The steps we examined so far are concerned with creating binary les - e.g. executables using libraries What happens when we execute a le? Command shell (or program) tells OS to execute a program le… OS opens a fresh process (ref. CIS*3110) and calls loader to “ ll it up” by copying the segments of the program le into di erent regions of the (virtual) memory for the process Program instructions (“text” segment) Static data (“data” segment) OS creates a new stack and an empty heap, then transfers control to rst instruction of program 24 fi ff fi (4+) Loader = dlopen Program using dynamically linked libraries: ld.so will load these when program starts but references (calls) will be xed up on demand Because much/most of libraries will not be referenced Some programs use dlopen to load shared object “plugins” on demand at run time OS pauses process execution while loader grabs the object and links it into already-loaded code, then continues where left o 25 fi f Loader and paths On Linux, to see what dependencies your shared object has, use the ldd command e.g. ldd a.out The loader has to know where to nd these shared libraries at run time To see what directories the loader knows about, look at the les in the directory /etc/ld.so.conf.d 26 fi Adding a path for the loader By default, the current directory is not included in any of the paths in /etc/ld.so.conf.d - so the loader will not look for your shared library in the current directory You can x this by updating the environment variable LD_LIBRARY_PATH: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. If you do not want to retype this statement every time you log in, add it at the very end of the.bashrc le in your home directory For example, we will need to do this to load the List API library we will use in this course We will also need to do this when we link the A1 parser library with a main program that would test it 27 fi fi Example: compiling a list library Your assignments will use a simple list library, and the sample code consists of three les: LinkedListAPI.c - implementations of library functions LinkedListAPI.h - headers for the library (function prototypes, constants, etc.) StructListDemo.c - demo executable with a main() function To run the example, we need to Compile the library (more on that in an upcoming lecture) Compile the demo, which needs The location of library headers The location of the library binary le The name of the library binary le 28 fi fi Example: compiling a list library For now, we will compile the library using the provided Makefile (which we will examine soon) It creates a binary le called liblist.so NOTE: when we link this library, we refer to it as just list - this weird naming convention will be explained later 29 fi Example: compiling a list library If we then compile try to compile the executable demo le with gcc StructListDemo.c we get a bunch of errors: /usr/bin/ld: /tmp/ccUliKAO.o: in function `main': StructListDemo.c:(.text+0x1f4): undefined reference to `initializeList' /usr/bin/ld: StructListDemo.c:(.text+0x2d4): undefined reference to `insertBack'... What component of the C compiler toolchain generates this error? What do we need to x? 30 fi Example: compiling a list library If we then compile try to compile the executable demo le with gcc StructListDemo.c we get a bunch of errors: /usr/bin/ld: /tmp/ccUliKAO.o: in function `main': StructListDemo.c:(.text+0x1f4): undefined reference to `initializeList' /usr/bin/ld: StructListDemo.c:(.text+0x2d4): undefined reference to `insertBack'... What component of the C compiler toolchain generates this error? - the linker What do we need to x? - tell the linker what library to link and where it is 31 fi Example: compiling a list library Solution: gcc StructListDemo.c -L. -llist Where -L. tells the linker to look for a library in the current directory, in addition to the global path -llist tells the linker link with the library called list This library happens to be in the le called liblist.so - again, the naming convention will be explained later 32 fi Example: executing a list demo When we try to execute the le StructListDemo, we also get another error:./StructListDemo: error while loading shared libraries: liblist.so: cannot open shared object file: No such file or directory What causes this error? How do we x it? 33 fi fi Example: executing a list demo When we try to execute the le StructListDemo, we also get another error:./StructListDemo: error while loading shared libraries: liblist.so: cannot open shared object file: No such file or directory What causes this error? The loader How do we x it? We tell the loader to look in the current directory See Slide 27 for details Once the loader knows the path, the program executes correctly 34 fi fi