4.1 Building MySQL Connector/C++ from Source on Unix, Solaris, and Mac OS X

  1. Change location to the top-level directory of the source distribution:

    shell> cd /path/to/mysql-connector-cpp
    
  2. Run CMake to build a Makefile:

    shell> cmake .
    -- Check for working C compiler: /usr/local/bin/gcc
    -- Check for working C compiler: /usr/local/bin/gcc -- works
    [...]
    -- Generating done
    -- Build files have been written to: /path/to/mysql-connector-cpp/
    

    On non-Windows systems, CMake first checks to see if the CMake variable MYSQL_CONFIG_EXECUTABLE is set. If it is not found, CMake tries to locate mysql_config in the default locations.

    If you have any problems with the configuration process, check the troubleshooting instructions given later.

  3. Use make to build the libraries. First make sure you have a clean build:

    shell> make clean
    

    Then build the connector:

    shell> make
    [  1%] Building CXX object »
    driver/CMakeFiles/mysqlcppconn.dir/mysql_connection.o
    [  3%] Building CXX object »
    driver/CMakeFiles/mysqlcppconn.dir/mysql_constructed_resultset.o
    [...]
    [100%] Building CXX object examples/CMakeFiles/statement.dir/statement.o
    Linking CXX executable statement
    

    If all goes well, you will find the MySQL Connector/C++ library in the driver directory.

  4. Install the header and library files:

    shell> make install
    

    Unless you have changed the location in the configuration step, make install copies the header files to the directory /usr/local/include. The header files copied are mysql_connection.h and mysql_driver.h.

    Again, unless you have specified otherwise, make install copies the library files to /usr/local/lib. The files copied are the dynamic library libmysqlcppconn.so, and the static library libmysqlcppconn-static.a. The extension of the dynamic library might be different on your system (for example, .dylib on Mac OS X).

If you encounter any errors, please first carry out these checks:

  1. CMake options: MySQL installation path, debug version and more

    In case of configuration or compilation problems, check the list of CMake options:

    shell> cmake -L
    [...]
    CMAKE_BACKWARDS_COMPATIBILITY:STRING=2.4
    CMAKE_BUILD_TYPE:STRING=
    CMAKE_INSTALL_PREFIX:PATH=/usr/local
    EXECUTABLE_OUTPUT_PATH:PATH=
    LIBRARY_OUTPUT_PATH:PATH=
    MYSQLCPPCONN_GCOV_ENABLE:BOOL=0
    MYSQLCPPCONN_TRACE_ENABLE:BOOL=0
    MYSQL_CONFIG_EXECUTABLE:FILEPATH=/usr/bin/mysql_config
    

    For example, if your MySQL Server installation path is not /usr/local/mysql and you want to build a debug version of the MySQL Connector/C++, use this command:

    shell> cmake \
      -D CMAKE_BUILD_TYPE:STRING=Debug \
      -D MYSQL_CONFIG_EXECUTABLE=/path/to/my/mysql/server/bin/mysql_config .
    
  2. Verify your settings with cmake -L:

    shell> cmake -L
    [...]
    CMAKE_BACKWARDS_COMPATIBILITY:STRING=2.4
    CMAKE_BUILD_TYPE:STRING=
    CMAKE_INSTALL_PREFIX:PATH=/usr/local
    EXECUTABLE_OUTPUT_PATH:PATH=
    LIBRARY_OUTPUT_PATH:PATH=
    MYSQLCPPCONN_GCOV_ENABLE:BOOL=0
    MYSQLCPPCONN_TRACE_ENABLE:BOOL=0
    MYSQL_CONFIG_EXECUTABLE=/path/to/my/mysql/server/bin/mysql_config
    

    Proceed by executing a make clean command followed by a make command, as described previously.

Once you have installed MySQL Connector/C++, you can carry out a quick test to check the installation. To do this, compile and run one of the example programs, such as examples/standalone_example.cpp. This example is discussed in more detail later, but for now, you can use it to test whether the connector has been correctly installed. This procedure assumes that you have a working MySQL Server to which you can connect. It also assumes header and library locations of /usrlocal/include and /usr/local/lib, respectively; adjust these as necessary for your system.

  1. Compile the example program. To do this, change location to the examples directory and enter this command:

    shell> g++ -o test_install \
      -I/usr/local/include -I/usr/local/include/cppconn \
      -Wl,-Bdynamic -lmysqlcppconn standalone_example.cpp
    
  2. Make sure the dynamic library which is used in this case can be found at runtime:

    shell> export LD_LIBRARY_PATH=/usr/local/lib
    
  3. Now run the program to test your installation, substituting the appropriate host, user, password, and database names for your system:

    shell> ./test_install localhost root password database
    

    You should see output similar to the following:

    Connector/C++ standalone program example...
    
    ... running 'SELECT 'Welcome to Connector/C++' AS _message'
    ... MySQL replies: Welcome to Connector/C++
    ... say it again, MySQL
    ....MySQL replies: Welcome to Connector/C++
    
    ... find more at http://www.mysql.com
    

    If you see any errors, take note of them and go through the troubleshooting procedures discussed earlier.