Compiling and Linking

Before we delve into the various functions that you can use for interacting with MySQL, let's spend a few moments studying the structure of the sample code that we will create to demonstrate them. We will pay attention to compiler options and the means of linking to the library and include what our programs require.

Our code will consist of the following source files:

  • main.c, the main program that we will use to demonstrate the API

  • common.c, in which we will define common functions, such as for connecting to a MySQL server

  • common.h, a header file that contains prototypes for those common functions

  • The MySQL header files and client library

  • stdio.h, the library of standard input/output routines

You will need to ensure that the MySQL header file, mysql.h, and the MySQL client library are available on your system. On a Unix system, the header file will normally be found (depending on where you installed MySQL) in /usr/local/mysql/include/mysql/ or /usr/local/include/mysql/. Similarly, the client library will be in /usr/local/mysql/lib/mysql/ or /usr/local/lib/mysql/. When building your application, you will need to include the correct paths to these directories.

In the examples, we will assume that you're using gcc as your compiler (this assumes that you have a Unix-type system), although if you are familiar with C on a Windows system you should still be able to compile, with minor adjustments.

We'll also assume that you are using make, which means that you can place complex compile options and paths to linked files in a file called Makefile, which resides in the same directory as your C source code. Create a Makefile like this (although you may have to modify the paths to the MySQL files to suit your system):

CC = gcc
INCLUDES = -I/usr/local/mysql/include/mysql
LIBS = -L/usr/local/mysql/lib/mysql -lmysqlclient -lm
all: myapp
main.o: main.c common.h
    $(CC) -c $(INCLUDES) main.c
common.o: common.c common.h
    $(CC) -c $(INCLUDES) common.c
myapp: main.o common.o
    $(CC) -o myapp main.o common.o $(LIBS)
clean:
    rm -f myapp main.o common.o

To run the compiler, you just need to type a single word at the command prompt:

$ make
					

Here's a sample output from make:

gcc -c -I/usr/local/mysql/include/mysql main.c
gcc -c -I/usr/local/mysql/include/mysql common.c
gcc -o myapp main.o common.o -L/usr/local/mysql/lib/mysql -lmysqlclient -lm

These lines are what you would have typed if you hadn't used make! gcc compiles the files, creating main.o and common.o, and the linking process creates the executable binary myapp.

Note

You may get a compile error on certain systems if the mysql library isn't found in your library path.

To fix this, you can edit the /etc/ld.so.conf file (if you are root), add the path to the library (such as /usr/local/mysql/lib/mysql), and run ldconfig to reload it. Alternatively, set the $LD_LIBRARY_PATH to the mysql library path. Then run make to compile again.


If you run make and get output free of errors such as the preceding output, you will be able to run the executable like this:

$ ./myapp
					

or if it's in your $PATH variable for your system, just this:

						$ myapp
					

We'll develop myapp throughout this lesson, so what it does and what it produces as output will evolve according to the functions in main.c at the time. The functionality will include connecting to a database and running various types of queries, getting more complex as the lesson progresses.

To clean up your directory of old compiled files (in fact to delete them) but leave your source files alone, you may want to run the following:

$ make clean
					

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.247