Lecture File
sep_comp.pdf (Separate Compilation)
Build & Modularity
Compilation pipeline, declarations vs definitions, and modular C/C++ design.
File/lecture name: sep_comp.pdf (Separate Compilation)
Main theme: Understanding the C/C++ compilation pipeline, managing declarations vs. definitions, and implementing modular programming using header files and separate compilation.
Prereqs assumed by slides: Basic C syntax (variables, functions, structs), familiarity with gcc command line usage, understanding of memory (stack vs. heap).
What this lecture enables you to do:
.c and .o files)..h) that provide interfaces without exposing implementation details.#define, #ifdef) to control code inclusion.#include, #define, and #ifdef to manipulate source text before compilation..o) using the -c flag..h files to declare interfaces for modules.struct List internals) from the client.void foo(int x); is a definition. It is a declaration.// Declaration
void foo(int x);
// Definition
void foo(int x) { printf("%d", x); }
void bar(int x); // Forward declaration
void foo(int x) { bar(x); } // foo can call bar
void bar(int x) { ... } // Definition comes later
.o)gcc file.c produces an executable immediately (it does, but it also links).gcc -c file.c produces file.o.# processed before compilation. They manipulate the source text (copy, paste, define, conditionally include).#include for libraries, #define for constants, and #ifdef for build configurations.#define creates a variable. It creates a text replacement token.#define MAX 100
int x = MAX; // x becomes 100
struct List l;).struct List; // Declaration only (Incomplete)
struct List *l; // Valid pointer
struct List l; // Error: storage size not known
next pointers directly).// Header only declares struct List
struct List;
// Client cannot access l->len directly
main.c containing main().gcc main.c.gcc runs all 4 steps (preprocess, compile, assemble, link).main is missing, the linker will fail with "undefined reference to _start".module.c (no main).gcc -c module.c.module.o.main.main.o, module.o).gcc main.o module.o -o executable_name.gcc assumes .o files are object files and skips compilation steps..h)module.h.#include "module.h" in client files.#define DEBUG.#ifdef DEBUG
printf("Debug info\n");
#endif
gcc -DDEBUG file.c.#ifdef is included only if the flag is passed.struct List;.struct List *l = createList();.struct List l; (Error).struct List in the header.void bar(int x); // Forward declaration
void foo(int x) { bar(x); }
void bar(int x) { ... }
// List.h
struct List; // Incomplete type
void printList(struct List *);
struct List *createList();
#include Syntax#include <...> for standard libraries, #include "..." for custom headers.#include <stdio.h> // Standard library
#include "mylib.h" // Custom header
< > for custom headers (compiler looks in system paths, not current directory).undefined reference to 'main'main as an executable entry point.main.c contains main, or link against a library that provides it.implicit declaration of function 'foo'storage size of 'l' isn't knownstruct List *l) or include the full definition in the header.ld returned 1 exit statusgcc to link automatically.PREX is not undeclared#define PREX 5 or gcc -DPREX=5.#include not finding custom header< > instead of " ".#include "header.h" for local files.l->len = 10).struct List;) in header to hide fields.gcc produces executable but crashes on deleteList.#ifdef code not compiling.-DDEBUG to gcc command.gcc complains about #define inside string.#define does not replace text inside quotes.#define is for code, not strings.gcc treats .o file as source.-c flag or passing .o to gcc without -o.gcc -c file.c for object files, or gcc file.o -o exe for linking.gcc produces warning about implicit-function-declaration.void printArray(int *, size_t); before usage.void foo(int x); and void foo(int x) { ... }?
A: The first is a declaration (promise of existence), the second is a definition (implementation + space allocation).gcc file.c produce a linker error if main is missing?
A: The linker needs an entry point (main) to build an executable. Without it, it fails.-c flag do in gcc?
A: It compiles the source file into an object file (.o) without linking.#include "file.h" vs #include <file.h>?
A: Double quotes look in the current directory; angle brackets look in system paths.#define FLAG without a value?
A: It defines the identifier FLAG as an empty token (true/false check).struct List; called an incomplete type?
A: It declares the name but not the members/size, so the compiler doesn't know how much space to allocate.bar"?
A: Add a forward declaration void bar(int x); before foo calls it..o) into a single executable, resolving references between them.struct List fields directly if the header only declares struct List;?
A: The compiler doesn't know the fields exist, so it rejects direct access (encapsulation).gcc -DDEBUG file.c?
A: The preprocessor includes code inside #ifdef DEBUG blocks.-DNAME=value (e.g., gcc -DPREX=5 file.c).gcc -c file.c -> file.o.gcc file.o -o exe or gcc file.c -o exe (auto-links).#include <stdio.h> (System)#include "file.h" (Local)#define MAX 100 (Constant)#ifdef FLAG (Conditional)void foo(int x); (No space allocated).void foo(int x) { ... } (Space allocated).struct List; (Pointer only, no stack allocation).main..o) ready for linking.# directives before compilation..h).#define: Macro definition for text replacement.#ifdef: Conditional compilation based on defined flags.-c Flag: Compile only, do not link.-D Flag: Define a preprocessor constant.-o Flag: Specify output filename._start: Entry point symbol for the linker.printf: Standard output function (from stdio.h).scanf: Standard input function (from stdio.h).malloc/free are not explicitly detailed, though createList implies heap usage.std:: namespace, templates, or class keyword are not used in these slides.Manually curated from summaries/sep_comp.txt. Use this page as a study aid and cross-check official slides for grading-critical details.