Compiling and linking

suggest change

A C implementation translates source files and combines them with required libraries to produce a program. The standard describes translation conceptually; an implementation does not have to expose each phase as a separate tool or file.

A typical native toolchain performs these tasks:

  1. Preprocesses directives such as #include, #define, and conditional compilation.
  2. Translates C into assembly or another intermediate representation.
  3. Produces object code.
  4. Links object files and libraries into an executable or library.

With GCC or Clang, compile one source file without linking:

cc -std=c23 -Wall -Wextra -pedantic -c source.c

This commonly creates source.o on Unix-like systems. File names and options are implementation-specific.

Compile and link a complete single-file program:

cc -std=c23 -Wall -Wextra -pedantic source.c -o program

Headers usually provide declarations and macros; including a header does not copy a library function's compiled implementation into the source file. The linker resolves external references against object files and libraries.

Warnings identify many suspicious constructs but cannot prove that a program is correct. Use an appropriate language mode, enable a strong warning set, and investigate every diagnostic. Add sanitizers, static analysis, and tests where the toolchain and deployment environment permit them.

The command cc often invokes the system's default C compiler. GCC, Clang, and Microsoft Visual C also provide their own driver commands and options; consult the selected toolchain's documentation instead of assuming that flags are portable.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents