22.5.4. MySQL Connector/C++ Building Linux applications with NetBeans

This section describes how you can build MySQL Connector/C++ applications for Linux using the NetBeans IDE.

Figure 22.76. The NetBeans IDE

The NetBeans IDE
Note

To avoid potential crashes the build configuration of MySQL Connector/C++ should match the build configuration of the application using it. For example, do not use the release build of MySQL Connector/C++ with a debug build of the client application.

  1. The first step of building your application is to create a new project. Select File, New Project. Choose a C/C++ Application and click Next.

  2. Give the project a name and click Finish. A new project is created.

  3. In the Projects tab, right-click Source Files and select New, then Main C++ File....

  4. Change the filename, or simply select the defaults and click Finish to add the new file to the project.

  5. You now need to add some working code to you main source file. Explore your MySQL Connector/C++ installation and navigate to the examples directory.

  6. Select a suitable example such as standalone_example_docs1.cpp. Copy all the code in this file, and use it to replace the code in your existing main source file. Amend the code to reflect the connection properties required for your test database. You now have a working example that will access a MySQL database using MySQL Connector/C++.

  7. You will notice that at this point NetBeans is showing some errors in the source code. This is because you need to direct NetBeans to the necessary header files that need to be included. Select File, Project Properties from the main menu.

  8. In the Categories: tree view panel, navigate to Build, C++ Compiler.

  9. In the General panel, select Include Directories.

  10. Click the ... button.

  11. Click Add and then navigate to the directory where the MySQL Connector/C++ header files are located. This will be /usr/local/include unless you have installed the files to a different location. Click Select. Click OK.

    Figure 22.77. Setting the Header Include Directory

    Setting the header include
              directory
  12. Click OK again to close the Project Properties dialog.

At this point you have created a NetBeans project, containing a single C++ source file. You have also ensured that the necessary include files are accessible. Before continuing, you need to decide whether your project is to use the MySQL Connector/C++ static or dynamic library. The project settings are slightly different in each case, as you need to link against a different library.

Using the static library

If you intend to use the static library you will need to link against two library files, libmysqlcppconn-static.a and libmysqlclient.a. The locations of the files will depend on your setup, but typically the former will be found in /usr/local/lib and the latter in /usr/lib. Note the file libmysqlclient.a is not part of MySQL Connector/C++, but is the MySQL Client Library file distributed with MySQL Server. Remember, the MySQL Client Library is an optional component as part of the MySQL Server installation process. Note the MySQL Client Library is also available as part of the new MySQL Connector/C distribution.

  1. The first step is to set the project to link the necessary library files. Select File, Project Properties from the main menu.

  2. In the Categores: tree view navigate to Linker.

  3. In the General panel select Additional Library Directories. Click the ... button.

  4. Select and add the /usr/lib and /usr/local/lib directories.

  5. In the same panel add the two library files required for static linking as discussed earlier. The properties panel should then look similar to the following screenshot:

    Figure 22.78. Setting the Static Library Directories and File Names

    Setting the static library directories
              and file names
  6. Click OK to close the Project Properties dialog.

Using the dynamic library

If you require your application to use the MySQL Connector/C++ dynamic library, you simply need to link your project with a single library file, libmysqlcppconn.so. The location of this file will depend on how you configured your installation of MySQL Connector/C++, but will typically be /usr/local/lib.

  1. The first step is to set the project to link the necessary library file. Select File, Project Properties from the main menu.

  2. In the Categories: tree view navigate to Linker.

  3. In the General panel select Additional Library Directories. Click the ... button.

  4. Select and add the /usr/local/lib directories.

  5. In the same panel add the library file required for static linking as discussed earlier. The properties panel should then look similar to the following screenshot:

    Figure 22.79. Setting the Dynamic Library Directory and File Name

    Setting the dynamic library directory
              and file name
  6. Click OK to close the Project Properties dialog.

Having configured your project you can build it by selecting Run, Build Main Project from the main menu. You can then run the project using Run, Run Main Project.

On running the application you should see a screen similar to the following (this is actually the static version of the application shown):

Figure 22.80. The Example Application Running

The example application running
Note

Note the above settings and procedures were carried out for the default Debug configuration. If you want to create a Release configuration you will need to select that configuration before setting the Project Properties.

Copyright © 2010-2024 Platon Technologies, s.r.o.           Home | Man pages | tLDP | Documents | Utilities | About
Design by styleshout