The Tools for Compiling

Whenever you build an application from source code, you need the right set of tools and libraries. OpenLDAP is no exception. Thankfully, OpenLDAP is a little lighter on requirements than some server applications out there.

Compiling is done on the command line, so you will need to open a terminal or otherwise gain access to the shell.

You will need the standard tool chain for working with C and C++ applications; a C compiler, a linker, and a make program. Fortunately, all of these come as standard with almost every Linux distribution available. You can test your system for the appropriate tools using the which command, which will tell you where the tools are located on your filesystem (assuming they are in one of the directories listed in your $PATH environment variable).

Here's a quick example of how you can check to see where the tools are and what the current version of each tool is. My system is Ubuntu Linux 6.06. Version numbers on your own system may vary. That's okay. OpenLDAP should compile on all modern Linux distributions, and probably on all modern UNIX distributions as well.

In each case I used the which tool to know where the tool was located. The programs I checked were gcc, ld, and make—the compiler, linker, and make program (respectively). As long as some path is returned, this indicates that the tool is installed. If no command is found, which returns without any output. Thus, if I searched for a fake command, blah, the output would look like this:

Thus, if you run which for any of the programs (gcc, ld, or make), and get no output, it indicates that you do not have the required tool.

After the which command in the given example, I ran each command with the --version flag (with two dashes before version) to tell me which version was installed. The --version flag is a GNU standard, but non-GNU programs (such as other versions of make or cc) may not support it.

The next thing to do is set a couple of environment variables that will provide some basic settings for the given tools. While there are many options that you can provide to your tools through environment variables, here we will just provide the basics for building OpenLDAP.

One way to set environment variables is with the export command. When you use the export command, the environment variables will be stored for the duration of your shell session (in other words, until you exit from the shell or close the terminal window). Here we will set the necessary environment variables using export:

The first export sets the $CC environment variable to gcc. The make program will use this to determine which compiler to use. (If you are using the cc compiler instead, then adjust the example to point to cc instead of gcc). Note that when you set an environment variable, you do not use the dollar sign ($) before the variable name. When you reference the variable however, you will need to include the dollar sign.

The second line sets the $CFLAGS variable. The $CFLAGS variables are the options that get passed to the compiler during compilation. In this case, we are passing it the option -O2 (that's a captial letter O, not a zero). This tells the compiler to use level 2 optimization when compiling the code.

The $PATH environment variable should also be set. However, by using the which command to see where our tools were, we have already verified that the necessary directories (that is, the directories that contain our tools) are specified in the $PATH variable.

If you are dealing with a non-standard system or non-standard builds of any of the libraries, or if you are interested in passing some other options to the build tools, you may also have to use some additional environment variables. You can use $CPPFLAGS to pass options to the C preprocessor (cpp, part of GCC). Likewise, you can pass the linker (ld) options with the $LDFLAGS variable. Finally, if you have libraries (compiled modules of code used by other applications) that are stored in non-standard places, you can use the $LIBS variable to specify the location of these libraries. If you need to use the variables you should consult the documentation for the tools and libraries.

At any point you can check your environment variables with some simple commands. The env command (executed with no arguments) will list all of the environment variables currently defined, as well as their values. You can also check an individual environment variable with the echo command. Simply type echo, followed by the name of the environment variable to display the value of that environment variable:

In this example echo $PATH shows the list of directories the shell searches to find programs. As you may recall the which command, when run, printed out the location of the specified tool. To find tools it searched each of the directories specified in the $PATH variable.

At this point we are ready to move on to the next step: installing the necessary dependencies.

Dependencies are packages that OpenLDAP will require to compile and to run. Installing these dependencies will vary from platform to platform (and from Linux distro to Linux distro). Here, I will use the Debian tools (included in Ubuntu Linux) to install packages.

OpenLDAP requires standard C libraries, regular expression libraries, and the Berkeley DB 4.2 (or later) libraries. These are almost always included in modern Linux distributions. In addition to the libraries, the header files are also required. Often these are stored in separate packages (usually called DEV packages). To install, for example, the Berkeley DB 4.2 development package in Ubuntu, you can execute the following command from the command line:

This will fetch and install the required package.

There are various other packages that are useful to install, and we will need them in order to build all of the features that we use in this book. You will need to install:

If you are interested in storing your directory in a relational database engine, such as MySQL or Oracle, you might also want to install iODBC2 (for database backend support).

All of these packages are common on modern Linux system. Make sure that the packages are installed, and that the DEV (or -dev) add-ons for each of these is installed as well. In Ubuntu 6.06 these can be installed with one (rather long) command:

Other distributions may use different installers, and even different package names, but you should have no problem finding them based on the bullet list of names that have been provided.

At this point you have all the tools and requirements necessary for building OpenLDAP. Now we move onward to the actual compiling process.