On
Linux, MacOS X, or other UNIX-like systems, object files normally have extension ".o", and you make them with
the "-c" flag. So this will create "foo.o" from "foo.cpp":g++ foo.cpp -c |
On Windows, object files have
extension ".obj", and you make them with the "/c" flag. You compile C++ with the "cl" program, and the "/TP /GR /EHsc" flags (that's C++, with dynamic_cast, and throw respectively). So this
command creates "foo.obj" from "foo.cpp":cl /TP /GR /EHsc foo.cpp /c |
On UNIX, use gcc to compile plain C (not C++).gcc foo.c -c |
On
Windows, use the "/TC" flag to compile C.cl /TC foo.c /c |
On UNIX, use "elf32" format (or elf64 on a 64-bit OS)yasm -f elf32 foo.S -o foo.o |
On
Windows, use "win32" format (or win64 on a 64-bit OS)
yasm -f win32 foo.S -o foo.obj |
On
Linux static library files have extension ".a", and you make them with
the "ar cr" tool. So this will create "foo.a" from "foo.o" and
"bar.o":ar cr foo.a foo.o Some machines, like MacOS X, require you to run "ranlib foo.a" after this. Also, "foo.a" remembers several strange things like the order you added the files, and "ar cr" won't ever *remove* .o files from your .a; so it's a good idea to remove your .a's before running "ar cr"... |
On
Windows, static library files have extension ".lib", and you make them
with the "link /lib" tool. So this creates "foo.obj" from "foo.c":link /lib /out:foo.lib foo.obj |
On
Linux executables have no filename extension. You specify the
executable name with the "-o" flag. You can also build
executables yourself with "ld", but it's trickier, especially for
C++. You MUST list all the needed libraries on the command line.g++ -o bar bar.o foo.a |
On
Windows, executables are named ".exe". You can either
list the libraries you need on the command line, or else cl /o bar.exe bar.obj foo.lib |
Name |
Examples | For |
Does |
Needed when |
-c |
Compiler |
Compile only, don't link. Makes an object file (.o or .obj) from a source code file. |
Compiling big programs with lots
of pieces because you can leave most code compiled as .obj files.
Also useful prior to building libraries. |
|
-Dmacro=value |
-DUserID=17 -DMAXCRAP=99 | Compiler |
Sets a macro (just like #define) from the compiler command line. |
Setting up configuration values,
paths, etc. Another alternative is to write a "config.h" file
somewhere that sets the same macros; "config.h" can make the compiler
command lines a lot more intelligible! |
-Ipath |
-Ilibfoo/include -I. | Compiler | Adds a new directory to the "include path"; the list of places the compiler looks for #included files. |
Compiling code that uses header
files in some other directory. (Subtle: #include "foo.h" works
automatically if foo.h is in the current directory; but #include
<foo.h> only works if you specify -I. to add the path to the
header file. Also consider using something like #include
"libfoo/include/foo.h") |
-Lpath |
-Llibfoo/lib -L. | Linker |
Adds a new directory to the "library path"; the list of directories the compiler looks for libraries (.a or .lib files) inside. |
Linking with almost any library other than the builtin system libraries. |
-lname |
-lfoo | Linker |
Looks for a file named "libname.a" in all the known library directories. UNIX-only. | Linking almost any library on UNIX. |
extern malloc"call" is a 5-byte instruction starting with 0xe8, followed by the address of malloc. But what is the address of malloc? We won't know until we're linked together with malloc! So the assembler puts this into the object file:
call malloc
8: e8 fc ff ff ff call 9The assembler puts a bogus value into the slot where malloc's address needs to go (a "relocation"). The assembler notes the relocation's address, and what symbol it needs to point to, in the "relocation table" of the object file--that's what the "R_386_PC32" is: a Relocation for 386 Program-Counter relative 32-bit offset. At link time, when the linker finds the malloc routine, and puts it into memory somewhere, the linker will overwrite the bogus 0xfc 0xff 0xff 0xff bytes with the actual address of malloc.
9: R_386_PC32 malloc
objdump -hdrC foo.oor on Windows using the Microsoft tool dumpbin:
dumpbin /disasm foo.obj(both objdump and dumpbin have zillions of additional parameters and options.)
section ".text"before writing assembler instructions, or
section ".rodata"before defining read-only strings or tables.