Using ODB with Qt Creator on Linux

From Code Synthesis Wiki

Jump to: navigation, search

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 = --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.
#
 
# Add the Qt headers directory to the ODB include directory list.
#
ODB_FLAGS += -I$$[QT_INSTALL_HEADERS]

# Newer versions of QtCreator do builds in a separate directory. As a
# result, we need to append the source directory to ODB files.
#
for(dir, ODB_FILES) {
  ODB_PWD_FILES += $$PWD/$${dir}
}

odb.name = odb ${QMAKE_FILE_IN}
odb.input = ODB_PWD_FILES
odb.output = ${QMAKE_FILE_BASE}-odb.cxx
odb.commands = odb $$ODB_FLAGS ${QMAKE_FILE_IN}
odb.depends = $$ODB_PWD_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_PWD_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 that 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.

If instead of using the system Qt installation you would prefer to use the one that comes with Qt Creator, then the above steps need to be altered as follows:

1. Install the ODB compiler, libodb, and libodb-<database> as before. However, for libodb-qt, we need to build it with the Qt libraries that come with Qt Creator. We also need to install libodb-qt into the same directory as Qt Creator's Qt libraries. Assuming that Qt Creator is in /opt/qtsdk and we are using Qt 4.8.1, the following commands can be used to build and install libodb-qt:

 ./configure CPPFLAGS=-I/opt/qtsdk/Desktop/Qt/4.8.1/gcc/include LDFLAGS=-L/opt/qtsdk/Desktop/Qt/4.8.1/gcc/lib --prefix /opt/qtsdk/Desktop/Qt/4.8.1/gcc
 make
 sudo make install

2. The only change in this step is to select the Qt version that come with Qt Creator instead of the system Qt.

3-6. No changes needed.

Personal tools