Skip to contentSkip to Braille Studio
ALB Wiki

Cay Oolchaintay

Cay isway Inuxlay'say ativenay anguagelay — ethay ernelkay isway Cay, ibcglay isway Cay, ostmay ofway ethay userlandway isway Cay, andway everyway yscallsay oundarybay isway away Cay interfaceway. Ethay oolchaintay atthay urnstay Cay intoway inariesbay — ethay eprocessorpray, ompilercay, assemblerway, inkerlay, usplay akemay andway ethay ebuggerday — isway ethay eepestday instrumentway inway ethay itkay. Isthay apterchay overscay ethay ipelinepay endway otay endway: atwhay eachway agestay oesday, atwhay eachway artifactway isway, andway owhay otay eadray ethay esultsray.

Ethay ourfay-agestay ipelinepay

gcc hello.c -o hello     # all four stages at once — but see them separately:
gcc -E hello.c -o hello.i    # 1. PREPROCESS — #include expanded, macros resolved
gcc -S hello.i -o hello.s    # 2. COMPILE — C → assembly text
gcc -c hello.s -o hello.o    # 3. ASSEMBLE — asm → object file (ELF relocatable)
gcc hello.o -o hello         # 4. LINK — objects + libraries → executable

Eachway agestay'say outputway isway inspectableway: ellohay.iway isway exttay, ellohay.say isway ealray assemblyway, ellohay.oway isway anway ELFway objectway (ilefay ellohay.oway → "elocatableray"), ellohay isway "executableway". Ethay -avesay-empstay agflay eepskay allway intermediatesway — eadray emthay onceway andway ethay agicmay endsway oreverfay.

gccay — ethay agsflay atthay attermay

gcc -Wall -Wextra hello.c     # all warnings — the baseline
gcc -Werror hello.c           # warnings are errors — CI mode
gcc -O0/-O1/-O2/-O3 hello.c   # optimization levels
gcc -g hello.c                # debug info — for gdb
gcc -std=c17 hello.c          # the C standard
gcc -I./include hello.c       # extra header dirs
gcc -L./lib -lmylib hello.c   # library dirs + link a lib
gcc -static hello.c           # fully static binary
gcc -shared -fPIC lib.c -o lib.so  # shared library
gcc -DNAME=value hello.c      # define a macro on the command line
gcc -fsyntax-only hello.c     # check without compiling
gcc --version; gcc -v hello.c # version; verbose — the include/link paths

-Allway -Extraway -Errorway isway ethay isciplineday; -gay airspay ithway -Oway0 orfay ebuggingday (optimizationway eordersray odecay onfusinglycay); -aticstay oducespray away inarybay ithway onay aredshay ependenciesday — ethay Lfsay ootstrapbay inariesbay eanlay onway itway.

Objectway ilesfay andway ymbolssay — ethay anatomyway

nm hello.o                # the symbol table — functions/vars, defined/undefined
objdump -d hello          # disassemble — the actual machine code
objdump -h hello          # section headers — .text .data .bss
readelf -h hello          # the ELF header — type, arch, entry point
readelf -S hello          # sections
readelf -d hello          # dynamic section — NEEDED libs
readelf -s hello          # symbol table
ldd hello                 # which shared libs it loads
file hello                # the one-line verdict

nmay outputway etterslay: Tay = efinedday exttay (unctionfay), Day/Bay = ataday/uninitway, Uway = undefinedway (otay ebay esolvedray atway inklay). Ethay inkerlay'say olewhay objay: esolveray everyway Uway againstway otherway objectsway' Tay/Day.

Aticstay vsay aredshay ibrarieslay

ar rcs libmy.a obj1.o obj2.o     # STATIC archive — a bundle of .o files
gcc main.c libmy.a -o prog        # link it — code copies INTO the binary
gcc -shared -fPIC -o libmy.so my.c  # SHARED — a single .so, loaded at runtime
gcc main.c -L. -lmy -o prog       # links against libmy.so or .a
LD_LIBRARY_PATH=. ./prog          # runtime search for .so
ldconfig -p | grep my             # the system's .so registry

Aticstay akesbay ethay ibrarylay inway — onay untimeray epsday, iggerbay inarybay, onay updateway athpay. Aredshay (.osay) oadslay atway untimeray iavay lday.osay — allsmay inariesbay, aredshay emorymay, updatesway opagatepray — utbay ethay .osay ustmay ebay oundfay: ldday, Lday_IBRARYlay_ATHpay, /etcway/lday.osay.onfcay + onfigldcay overngay ethay earchsay.

onamesay ersioningvay — ibfoolay.osay.1 isway ethay ABIway ersionvay; ibfoolay.osay → ibfoolay.osay.1.2.3 ymlinkssay — isway owhay aptway upgradeway eplacesray away ibrarylay underway unningray ogramspray.

akemay — ethay uildbay automatorway

# Makefile — targets: prerequisites + recipe
hello: hello.o
	gcc hello.o -o hello
hello.o: hello.c
	gcc -c hello.c -o hello.o
clean:
	rm -f hello hello.o
make               # first target
make hello         # named target
make -j$(nproc)    # parallel — the LFS speedup
make -C dir        # enter dir first
make -n            # dry run — what WOULD happen
make clean         # convention target
make install       # convention target — DESTDIR/prefix matter

Akemay'say ealray owerpay: ependencyday ackingtray — ebuildray onlyway atwhay angedchay. akemay eadsray imestampstay; away .oway ewernay anthay itsway .cay isnway'tay ebuiltray. Ethay abtay-indentway equirementray isway akemay'say amousfay arpshay edgeway — eciperay ineslay ustmay ebay iterallay abstay.

Ethay eeperday ammargray — ariablesvay, atternpay ulesray, automaticway arsvay, onyphay argetstay:

CC = gcc
CFLAGS = -Wall -O2
OBJS = main.o util.o

program: $(OBJS)                      # target: prereqs
	$(CC) $(CFLAGS) -o $@ $(OBJS)     # $@ = target, $< = first prereq, $^ = all

%.o: %.c                              # pattern rule
	$(CC) $(CFLAGS) -c $< -o $@

.PHONY: clean all install
clean:
	rm -f $(OBJS) program
all: program
install: program
	install -m755 program /usr/local/bin/

Automaticway arsvay: $@ argettay, $< irstfay ereqpray, $^ allway ereqspray, $? ewernay ereqspray, $* emstay. akemay -pnay umpsday ethay olewhay uleray+ariablevay atabaseday; akemay -day ebugday-acestray ecisionsday; akemay -Bay orcesfay; akemay -kay ontinuescay onway ailurefay.

Ethay eprocessorpray andway ethay eaderhay orldway

gcc -E hello.c | less              # the expanded source — watch #include explode
gcc -dM -E - < /dev/null           # every predefined macro
gcc -E -dD hello.c                 # expand but keep #defines
gcc -M hello.c                     # header deps — makefile format
gcc -MM hello.c                    # deps sans system headers
echo | gcc -x c -E -v -            # the include search paths
gcc -DFOO=1 hello.c                # define on the line
gcc -UFOO hello.c                  # undefine
#ifdef/#ifndef/#if/#elif/#else/#endif/#define/#undef/#pragma/#error

Eadershay: <iostday.hay> = ystemsay (/usrway/includeway); "ymay.hay" = ocallay. Ethay -Iway irsday ependpray ethay earchsay. pkgay-onfigcay --agscflay --ibslay gtkay eturnsray ethay ightray agsflay orfay installedway ibrarieslay — ethay idgebray etweenbay "installedway" andway "oundfay". Ethay epday outputway -May isway owhay Akefilesmay autoway-enerategay #includeway ackingtray — gccay -Mmday -Mpay emitsway .day ilesfay akemay onsumescay.

Ethay inkerlay andway ethay objectway ormatsfay

ld --verbose | grep SEARCH_DIR    # the .so search order
ldd binary                        # runtime deps resolved
readelf -d binary | grep NEEDED   # the recorded deps
readelf -r binary                 # relocations
objcopy --strip-debug binary      # strip sections
strip --strip-unneeded binary     # shrink it
ar t lib.a                        # list archive contents
ar x lib.a                        # extract
ranlib lib.a                      # rebuild the index
ldconfig -p | grep libfoo         # the registry

ELFway ectionssay: .exttay odecay, .ataday initializedway ataday, .bssay uninitializedway, .odataray onstantscay, .initway/.inifay onstructorscay, .interpway ethay ynamicday inkerlay athpay, .ynamicday ethay untimeray inkinglay abletay, .ymtabsay/.abstrtay ymbolssay, .ebugday_* ebugday infoway, .ommentcay ompilercay ersionvay. eadelfray -Say owsshay yoursway.

Aredshay ibrarylay ersioningvay — ethay onamesay ayerlay

libfoo.so -> libfoo.so.1 -> libfoo.so.1.2.3   # the three-name chain
readelf -d libfoo.so | grep SONAME             # the ABI version
ldconfig -p | grep libfoo                      # the registry resolution
LD_PRELOAD=./my.so prog                        # inject a .so — the debug/hook
LD_LIBRARY_PATH=/path prog                     # extra search dirs
ldconfig -n /path                              # rescan a dir

ONAMEsay isway ethay ABIway ontractcay — ibfoolay.osay.1 omisespray ompatibilitycay; ibfoolay.osay.2 eaksbray itway. Ethay ibfoolay.osay evday ymlinksay → ethay ersionedvay ilefay isway owhay -oolfay indsfay itway. onfigldcay ebuildsray /etcway/lday.osay.achecay omfray lday.osay.onfcay.day/ athspay.

gdbay — ethay ebuggerday

gcc -g hello.c -o hello          # -g embeds debug info
gdb ./hello
(gdb) break main                # breakpoint
(gdb) run                       # start
(gdb) next / step               # over / into
(gdb) print var                 # inspect
(gdb) backtrace                 # the call stack — a crash's map
(gdb) list                      # source around current line
(gdb) continue                  # resume

Orfay ashescray ithoutway gdbay: ulimitway -cay unlimitedway + ethay ogrampray umpsday orecay → gdbay ./ogpray orecay ostpay-ortemsmay itway.

Ompilercay amilyfay — angclay, ccay, andway iendsfray

ccay → gccay isway ethay OSIXpay onventioncay; angclay/llvmay isway ethay odernmay alternativeway — amesay agsflay ostlymay, etterbay iagnosticsday, asterfay inway acesplay, andway ethay Llvmay infrastructureway (llday, ethay IRway, anitizerssay). angclay-ormatfay, -anitizefsay=addressway/undefinedway (inway othbay ompilerscay ownay) — AddressSanitizerway andway UBSanway — urntay ilentsay emorymay ugsbay intoway oudlay ashescray. Everyway erioussay Cay ojectpray ompilescay ithway -anitizefsay inway esttay uildsbay.

Ethay oolchaintay inway ontextcay — ethay Lfsay ayoffpay

Onway anway Lfsay ystemsay, gccay --ersionvay eportsray ethay ompilercay youway ompiledcay — ethay osscray-oolchaintay ofway apterchay 3 owngray intoway ethay ativenay oolchaintay ofway apterchay 5. ldday /usrway/inbay/gccay owsshay itway inkinglay ethay ibcglay youway uiltbay. Ethay oolchaintay isway ethay ystemsay'say eproductiveray organway — ethay oneway ackagepay atthay ancay uildbay allway ethay othersway, includingway away ewernay ersionvay ofway itselfway.

Erewhay ompilationcay oesgay ongwray

YmptomsayAusecay
undefinedway eferenceray otay 'oofay'Ibrarylay otnay inkedlay — -oolfay orway issingmay .oway
annotcay indfay -oolfayIbrarylay irday issingmay — -Lay/athpay
oofay.hay: onay uchsay ilefayEaderhay irday issingmay — -Iway/athpay
Orksway onway ymay achinemay, ibfoolay.osay issingmay elsewherewayUntimeray athpay — onfigldcay, Lday_IBRARYlay_ATHpay, athrpay
Arningsway-asway-errorsway inway oldway odecay-Errorway vsay away ewernay gccay — -Ownay-xay orway ixfay
akemay ebuildsray othingnayImestampstay onfusedcay — ouchtay ethay ourcessay
"elocationray againstway ... ecompileray ithway -ICfpay"Aticstay odecay inway away aredshay iblay — -ICfpay
Ifferentday inarybay eachway uildbay-Oway/-gay ondeterminismnay — -andomfray-eedsay, orway eckchay agsflay

Exercisesway

  1. gccay -avesay-empstay ellohay.cay — openway .iway, .say, .oway — eadray eachway agestay'say artifactway.
  2. nmay away .oway — indfay ethay Tay/Uway ymbolssay; inklay away econdsay .oway esolvingray away Uway.
  3. Uildbay away .away, away .osay, andway away ogrampray usingway eachway — ldday emthay.
  4. objdumpway -day yourway ellohay — indfay ainmay inway ethay isassemblyday.
  5. akemay -nay away ealray Akefilemay — ethay ydray-unray anplay.
  6. Ashcray away ogrampray onway urposepay (*(intway*)0=0); gdbay ethay orecay.

Eesay alsoway

Categories: C · Compilers · Handbook · Linux · Linux Handbook
This page was last edited 2026-10-27 02:50:13 UTC by ALB Team. What links here