Below are some basic flags and examples to help you compile your programs:
The following command compiles test.c and produces an executable named a.out
$ gcc test.c
If we want to specify the name of the executable generated we provide the -o flag. The following command will create an executable called test
$ gcc test.c -o test
To compile a single file into an object module we can use the -c flag. The following will create test.o
$ gcc -c test.c
When dealing with multiple files it is best to use makefiles. However, it is possible to also use the command line.
Step 1. Compile all of the object modules:
$ gcc -c p1.c
$ gcc -c p2.c
...
$ gcc -c pn.c
Step 2. Compile all of the object files into an executable called my_project (the *.o tells gcc to look for all .o files in the directory):
$ gcc -o my_project *.o
Static libraries are created using the ar (archive) tool. The tool has a few flags that you can use to create the library.
c -> creates a library with a given name
q -> adds a file at the end of the library
r -> replaces an existing member of the library with a new file
t -> prints a table of archive contents
s -> effectively acts the same as running ranlib on the library
Step 1. Create all of the object modules
$ gcc -c p1.c
$ gcc -c p2.c
...
$ gcc -c pn.c
Step 2. Combine the object modules into archive using ar, the cs flag combo will create a library named libMyLibraryName.a using all of the .o files in the directory and then creates an object file index. Alternatively you could name the .o files individually in the command. Note: your library name should always begin with 'lib'.
$ ar cs libMyLibraryName.a *.o
Step 3. Using the static library. To include the library in your executable use the -static flag when running gcc along with -L to identify where the library is located, and -l followed by the library name (less the lib prefix).
$ gcc -static my_c_file.c -o my_executable -L. -lMyLibraryName
Dynamic libraries allow you to have only one version of the library which is referenced by all running processes.
Step 1. Compile all of your object modules using the -fPIC flag
$ gcc -fPIC -c p1.c
$ gcc -fPIC -c p2.c
...
$ gcc -fPIC -c pn.c
Step 2. Use the -shared flag in gcc to create a shared library named libMySharedLibrary.so
$ gcc -shared *.o -o libMySharedLibrary.so
Step 3. Compile your program using the shared library
$ gcc my_c_file.c -o my_executable -L. -lMySharedLibrary
-O0, -O1, -O2, -O3 -> apply varying levels of optimization to your code during compilation -O3 being the highest
-g -> compile the code so that you can run gcc on it
-shared -> creates a shared object library with the name specified following -o
-o -> specifies the name of the output for gcc
-c -> compiles c source code into an object module. See above for usage.
-static -> tells gcc to use the static version of libraries, by default it will look for shared object libraries
-L -> tells gcc where to look for libraries. Usage would be -L</some/path/here>
-l -> lowercase l tells gcc the name of a library to include. See above for usage.
-fPIC -> tells gcc to create an object module with position independent code. You need to include this flag when creating object modules for a dynamic library
-S -> tells gcc to output the assembly. Usage would be $ gcc -S some_c_file.c -o some_asm_file.s
-s -> tells gcc to strip the symbol table from the output file