GNU Toolchain, including GCC, is included in all Unixes. It is the standard compiler for most Unix-like operating systems. Open a Terminal, and enter “gcc –version”. If gcc is not installed, the system will prompt you to install gcc.
Below is the Hello-world C program hello.c:
// Compile-only with -c option
o: specifies the output executable filename.
-Wall: prints “all” Warning messages.
-g: generates additional symbolic debuggging information for use with gdb debugger.
The options are:
-c: Compile into object file “Hello.o”. By default, the object file has the same name as the source file with extension of “.o” (there is no need to specify -o option). No linking with other object files or libraries.
Linking is performed when the input file are object files “.o” (instead of source file “.cpp” or “.c”). GCC uses a separate linker program (called ld) to perform the linking.
GCC compiles a C/C++ program into executable in 4 steps as shown in the above diagram. For example, a “gcc -o hello hello.c” is carried out as follows:
Pre-processing: via the GNU C Preprocessor (cpp), which includes the headers (#include) and expands the macros (#define).
> cpp hello.c > hello.i
The resultant intermediate file “hello.i” contains the expanded source code.
Compilation: The compiler compiles the pre-processed source code into assembly code for a specific processor.
> gcc -S hello.i
The -S option specifies to produce assembly code, instead of object code. The resultant assembly file is “hello.s”. Assembly: The assembler (as.exe) converts the assembly code into machine code in the object file “hello.o”.
> as -o hello.o hello.s
Linker: Finally, the linker (ld.exe) links the object code with the library code to produce an executable file “hello.exe”.
> ld -o hello.exe hello.o ...libraries...
Suppose that your program has two source files: file1.cpp, file2.cpp. You could compile all of them in a single command:
You need to use g++ to compile C++ program, as follows. We use the -o option to specify the output file name.
Linking result in a single executable out of each object code from several source files. Interpreted program on the other hand interprets each line of the input file and executes it as code. This way the program does not need to be compiled, and any changes will be seen the next time the interpreter runs the code.
So, just create the object files for the functions. You should see object files fn1.o and fn2.o
Bundle the object files into an archive called a library (static.a)
View the object files and the functions in the library.
output:
Just want to view the object files in the library?
output:
When you try to compile main.c without linking it to the library, you will get the following errors:
So, you need to link the library. Now, you can find the “execute” as an executable.
If the library is not in the current path, you need to specify the Library path and the library:
(Note: -lstatic not -llibstatic)
The fn1 and fn2 are defined by symbol type T: Normal Code Section
Create position Independent Code (fPIC) objects; -wall: warning
Compile the code:
You are linking to the dynamic library i.e. the executable looks for the objects in the shared library on the fly. If you run the executable, you will get the error. Also, you can check the dependencies for the executable.
So, you need to make the library accessible. You can set LD_LIBRARY_PATH variable in the shell (interactively) or in .bashrc file.
Or, include the path in the /etc/ld.so.conf file (only admin)
Execute now.
Conver the static library “static.a” to Shared Object “shared.so”
When compiling the program, the compiler needs the header files to compile the source codes; the linker needs the libraries to resolve external references from other object files or libraries. The compiler and linker will not find the headers/libraries unless you set the appropriate options, which is not obvious for first-time user.
For each of the headers used in your source (via #include directives), the compiler searches the so-called include-paths for these headers. The include-paths are specified via -Idir option (or environment variable CPATH). Since the header’s filename is known (e.g., iostream.h, stdio.h), the compiler only needs the directories.
The linker searches the so-called library-paths for libraries needed to link the program into an executable. The library-path is specified via -Ldir option (uppercase ‘L’ followed by the directory path) (or environment variable LIBRARY_PATH). In addition, you also have to specify the library name. In Unixes, the library libxxx.a is specified via -lxxx option (lowercase letter ‘l’, without the prefix “lib” and “.a” extension). In Windows, provide the full name such as -lxxx.lib. The linker needs to know both the directories as well as the library names. Hence, two options need to be specified.
Try list the default include-paths in your system used by the “GNU C Preprocessor” via “cpp -v”:
A library is a collection of pre-compiled object files that can be linked into your programs via the linker. Examples are the system functions such as printf() and sqrt().
There are two types of external libraries: static library and shared library.
Because of the advantage of dynamic linking, GCC, by default, links to the shared library if it is available.
You can list the contents of a library via “nm filename”.
The utility “ldd” examines an executable and displays a list of the shared libraries that it needs. For example,
The utility “nm” lists symbol table of object files. For example,
[1] Yolinux.com Tutorial: http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
[2] Configure Script: https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Developer_Guide/cmd-autotools-config.html
[3] GNU Manual: http://www.gnu.org/software/make/manual/make.html#toc_Top
[4] Computer Science from Bottom up - https://www.bottomupcs.com/index.xhtml