Penguin
Blame: CompilingHowto
EditPageHistoryDiffInfoLikePages
Annotated edit history of CompilingHowto version 5, including all changes. View license author blame.
Rev Author # Line
5 AristotlePagaltzis 1 !!! How compiling works under [Linux] (and Unices in general)
1 PerryLorier 2
5 AristotlePagaltzis 3 Note this is the longwinded approach to compiling a [C] program. gcc(1) is smart enough to do most of these steps for you automagically. :)
1 PerryLorier 4
5 AristotlePagaltzis 5 # cpp(1) takes a <tt>.c</tt> and some <tt>.h</tt> files, preprocess the source and generates an <tt>.i</tt> file.
1 PerryLorier 6
5 AristotlePagaltzis 7 This file is the ultimate SourceCode, with all the <tt>#include</tt>s expanded out and all the <tt>#define</tt>s replaced.
1 PerryLorier 8
5 AristotlePagaltzis 9 # gcc(1)then takes the <tt>.i</tt> file and generates a <tt>.s</tt> file (assembler source).
1 PerryLorier 10
5 AristotlePagaltzis 11 # as(1) then takes the <tt>.s</tt> file and generates a <tt>.o</tt> file (object file).
1 PerryLorier 12
5 AristotlePagaltzis 13 These are fragments of MachineCode with unresolved symbols. This means that the addresses of various variables and subroutines are not yet known, and any [CPU] instructions that refer to these unknown addresses must be filled in.
1 PerryLorier 14
5 AristotlePagaltzis 15 # ld(1) then takes the <tt>.o</tt> file(s) and links it/them with any libraries, resolves the symbols, and generates an executable or <tt>.a</tt> library.
2 PerryLorier 16
5 AristotlePagaltzis 17 <tt>a.out</tt> is the default name given to a program if none was specified with the <tt>-o</tt> switch. The reason for this is that it used to be the assembler output (before seperate linking was used), and the assembler was called <tt>a</tt>, hence <tt>a</tt>'s <tt>.out</tt> file.
18
19 <tt>.a</tt> files are libraries of <tt>.o</tt> files. They are kinda like TarBall~s of <tt>.o</tt> files. ar(1) is the tool to manage them. If you run ranlib(1) over such an archive it will create an index of all the symbols, making your compiles faster. I believe [GNU] ar(1) keeps the symbol table up to date so ranlib(1) isn't required, but I could be wrong.
20
21 # strip(1) can then optionally remove any unneeded information in the executable (such as debugging information) to reduce its size.
22
23 Alternatively, <tt>gcc foo.c baz.c -o baz</tt> will sort the entire thing out for you. :)
24
25 !! Libraries
2 PerryLorier 26
27 libtool(1) is a program to manage libraries in a CrossPlatform manner under Unix.
28
5 AristotlePagaltzis 29 Instead of making a BinaryExecutable, you can make a SharedLibrary by compiling with the flags <tt>-shared</tt> and <tt>-fPIC</tt>. The latter is optional; it means to create PositionIndependentCode. If you don't use it then when the library is loaded into memory ld.so(8) will relocate the symbols for you which will write to the memory used by the library, and thusly will cause that library not to be shared between processes due to CopyOnWrite. I don't know why you wouldn't want to use PositionIndependentCode if your platform supports it, so use it. :)
30
31 <verbatim>
2 PerryLorier 32 gcc foo.c -shared -fPIC -c -o foo.o
5 AristotlePagaltzis 33 </verbatim>
2 PerryLorier 34
5 AristotlePagaltzis 35 You can open dynamically loaded modules using dlopen(3). You can probably link against these, although I've never bothered figuring out how.
36
37 If you want to make a library that is statically compiled into a program then compile it into a .a file called <tt>lib''thenameofyourlibrary''.a</tt> and put it in some directory. Then when compiling your main program use <tt>-L/path/to/the/libraries</tt> to make the compiler search that directory and put <tt>-l''thenameofyourlibrary''</tt> on the command line. Eg.:
38
39 <verbatim>
2 PerryLorier 40 gcc foo.c -c -o foo.o
41 ar rcs libs/libfoo.a foo.o
42 ranlib libs/libfoo.a
43 gcc baz.c -Llibs/ -lfoo -o baz
5 AristotlePagaltzis 44 </verbatim>
45
46 !! See also
2 PerryLorier 47
5 AristotlePagaltzis 48 * MakefileHowto
1 PerryLorier 49
50 ----
5 AristotlePagaltzis 51 CategoryHowto