LevSelector.com New York
home > Building Projects on Unix

Building Projects on Unix (page under construction - still you may find it very useful)
On This Page Other Pages

- intro
- gcc usage
- search paths
- environment
- linking

- gmake

Intro ------------------------------

Building here is a process of preprocessing, compiling, assemblying and linking.

Big software project may depend on hundreds of source files and libraries. Many developers can work in parallel on different pieces of it. Then all these pieces are combined together during the build process. The GNU project provides a set of standard tools which allows to write/build software to run on different unix-s. It also makes the build process simplier (standard steps: configure, make build, make test, make install). Here are some standard tools/files:

For C and C++ projects, the common file type are:

Note: on Windows you can use Cygwin (or MSYS) to emulate unix build environment.

Note: The user working on a project can generate the ./configure file by running shell script (autogen.sh) which would run a series of commands - something like this: aclocal; autoconf; autoheader; automake

gcc usage --------------------

cc -E hello.c; view hello.i - does only preprocessing and shows the result
cc -S hello.c ; view hello.s - generates assembler code
cc -g -c hello.s; view hello.o - generates object code
dis -L -t .data hello.o >hello.dis - disassemble the object file into a text file
cc -o hello hello.c - does all steps (pre-processing, compiling, assembling, linking) - and generates output executable "hello"

Here are some common compiler options:

-c - compile only (do not link)
-S - stop after making assempler code
-E - stop after preprocessing stage
-o file - define the output file
-v - verbouse (print commands executed on STDERR, also print compiler version, etc.)
-l - to specify libraries to link ( -lxyz means to link libxyz.a)
-L - to specify directories to search for libraries
-I - to specify directories to search for include files
-B/some/path - prefix to find executables, libraries, include files, and data files of the compiler itself. Default is /usr/lib/gcc/, /usr/local/lib/gcc-lib/, and $PATH
-static - tells compiler to include shared libraries into the executable (thus making it larger) - even though the libraries are available (usually in /lib) and can be linked dynamically during execution (on systems that support dynamic linking)
-shared - produce a shared object which can then be linked with other objects to form an executable. Only a few systems support this option.
-symbolic - bind references to global symbols when building a shared object, warn about any unresolved references - only if supported.
-On, where n=0,1,2,or 3 - sets level of optimization
-b machine - to specify the type of machine for which to compile
-V version - to specify which version of compiler to run
-g Produce debugging information in the operating system's native format
there are many more options (100s) - see man pages for the compiler.

Search Paths --------------------

To see gcc default search paths run gcc -print-search-dirs. It will show where gcc is installed, and list of directories where it looks for its binaries or libraries. The search path for headers is different than that for libraries. For headers gcc will look into "include" subdirectories, for example:

/usr/include/
/usr/include/c++/ - for C++

On normal Unix system gcc will look for header files in directories similar to these:

/usr/local/include/
/usr/libexec/gcc/sometarget/someversion/include/
/usr/lib/gcc/sometarget/someversion/include/
/usr/sometarget/include/
/usr/include/

(where, for example, sometarget/someversion = i386-redhat-linux/3.4.6)

The search for libraries themselves may be like this:

/usr/lib/gcc/sometarget/someversion/
/usr/sometarget/lib/sometarget/someversion/
/usr/sometarget/lib/
/usr/lib/sometarget/someversion/
/usr/lib/
/lib/sometarget/someversion/
/lib/

Note: main library for C is libc.a, for C++ - libstdc++.a

Note: you an use the following gcc options:

Read more details here: http://gcc.gnu.org/onlinedocs/gcc-4.3.0//cpp/Search-Path.html#Search-Path

Note: you can use GCC with a number of different C libraries. Here are some examples:

Environment --------------------

GCC_INCLUDE_DIR, LD_LIBRARY_PATH, LD_PRELOAD, etc.

David Barr "Why LD_LIBRARY_PATH is bad":
   http://xahlee.org/UnixResource_dir/_/ldpath.html

"LD_LIBRARY_PATH is one of those insidious things that once it gets set globally for a user, things tend to happen which cause people to rely on it being set. Eventually when LD_LIBRARY_PATH needs to be changed or removed, mass breakage will occur!"

Linking --------------------

Static linking - include libraries into the executable itself. Advantage - no dependencies on external libraries. Disadvantage - executable gets larger, all utilities/extensions will be also larger.

Dynamic linking - using external shared libraries. This is the default linking option. Advantage - smaller executables. Disadvantage - we need to make sure that executable will find the libraries and that these libraries has the right version to match the executables.

The search for shared libraries can be influenced by a number of factors:

/lib/ld.so - shared library loader