Using ODB with Qt Creator on Linux

From Code Synthesis Wiki

(Difference between revisions)
Jump to: navigation, search

Boris (Talk | contribs)
(Start a page on using ODB with Qt Creator on Linux)
Next diff →

Revision as of 13:26, 4 September 2012

Below are the step-by-step instructions for building the 'qt' ODB example with Qt Creator on Linux. You can use the same approach for other examples or for your own application. The following steps assume you are using your system Qt libraries (i.e., the one that are installed into /usr or /usr/local, normally from packages that come with your Linux distribution). This is the easiest way to get started. At the end of this page there is some additional information on how to instead use ODB with Qt libraries that come with Qt Creator.

1. The first step is to build and install ODB using the standard UNIX Installation Instructions. Alternatively, you can check if your distribution has pre-built packages for ODB. Specifically, you will need the ODB compiler, the common ODB runtime library (libodb), and the database-specific ODB runtime library (libodb-<database>). You will also most likely want to install the Qt ODB profile library (libodb-qt).

2. Once this is done, start Qt Creator and create a new Qt console project (for your own applications you can use other project types). Specifically, select File->New File or Project. Then select Other Project and Qt Console Application. Click Choose. In the next screen, enter the project name (we will use 'test') and select its location. Click Next. In the next screen for the Create Build Configuration field select the For One Qt Version One Debug and One Release option. Uncheck the Use Shadow Building (you can enable it later if desired). In the Qt Version field select the System Qt. Click Next and on the next screen click Finish.

3. In the Edit tab, delete the generated main.cpp file from the project.

4. From the 'qt' example in the odb-examples package, copy the following files to the project's directory: driver.cxx, employee.hxx, and database.hxx. Add these files to the project.

5. Open the .pro file (test.pro) and add the following lines at the end of this file:

 # List of header files that should be compiled with the ODB compiler.
 #
 ODB_FILES += employee.hxx
 
 # ODB compiler flags.
 #
 ODB_FLAGS = -I/usr/include/qt4 --database pgsql --profile qt --generate-schema --generate-query --generate-session
 
 # Select the database we are going to use.
 #
 DEFINES += DATABASE_PGSQL
 
 # Suppress unknown pragmas GCC warnings.
 #
 QMAKE_CXXFLAGS_WARN_ON = $$QMAKE_CXXFLAGS_WARN_ON -Wno-unknown-pragmas
 
 # Link to the ODB runtime libraries.
 #
 LIBS += -lodb-pgsql
 LIBS += -lodb-qt
 LIBS += -lodb
 
 # ODB compilation rules. Normally you don't need to change anything here.
 #
 odb.name = odb ${QMAKE_FILE_IN}
 odb.input = ODB_FILES
 odb.output = ${QMAKE_FILE_BASE}-odb.cxx
 odb.commands = odb $$ODB_FLAGS ${QMAKE_FILE_IN}
 odb.depends = ${ODB_FILES}
 odb.variable_out = SOURCES
 odb.clean = ${QMAKE_FILE_BASE}-odb.cxx ${QMAKE_FILE_BASE}-odb.hxx ${QMAKE_FILE_BASE}-odb.ixx ${QMAKE_FILE_BASE}.sql
 QMAKE_EXTRA_COMPILERS += odb
 
 odbh.name = odb ${QMAKE_FILE_IN}
 odbh.input = ODB_FILES
 odbh.output = ${QMAKE_FILE_BASE}-odb.hxx
 odbh.commands = @true
 odbh.CONFIG = no_link
 odbh.depends = ${QMAKE_FILE_BASE}-odb.cxx
 QMAKE_EXTRA_COMPILERS += odbh


The above fragment assumes that we are using PostgreSQL. If you would like to use a different database, then you will need to make three changes: First, you will need change the --database option value in the ODB_FLAGS variable. Second, you will need to change the define added to the DEFINES variable. And, finally, you will need to change the name of the database-specific runtime library (libodb-pgsql) that is added to the LIBS variable.

Note also the -I/usr/include/qt4 option in ODB_FLAGS. This (or similar) option is necessary if Qt headers on your system are installed in a place other than directly in /usr/include or /usr/local/include. For example, on Debian/Ubuntu Qt headers are installed into the /usr/include/qt4 sub-directory. To find out which options, if any, you need to add, run the following command:

 pkg-config --cflags-only-I QtCore

Finally, the addition of the define to the DEFINES variable is only necessary if you are using the database.hxx file from one of the examples. If, instead, you create the desired database instance directly, then you don't need this line.

6. Next save the .pro file and build the project.

Personal tools