Using ODB with Qt Creator on Linux

From Code Synthesis Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 13:26, 4 September 2012
Boris (Talk | contribs)
(Start a page on using ODB with Qt Creator on Linux)
← Previous diff
Revision as of 14:07, 4 September 2012
Boris (Talk | contribs)
(Add note on using Qt that comes with Qt Creator)
Next diff →
Line 11: Line 11:
5. Open the .pro file (test.pro) and add the following lines at the end of this file: 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.+ # List of header files that should be compiled with the ODB compiler.
- #+ #
- ODB_FILES += employee.hxx+ ODB_FILES += employee.hxx
- +
- # ODB compiler flags.+ # ODB compiler flags.
- #+ #
- ODB_FLAGS = -I/usr/include/qt4 --database pgsql --profile qt --generate-schema --generate-query --generate-session+ ODB_FLAGS = --database pgsql --profile qt --generate-schema --generate-query --generate-session
- +
- # Select the database we are going to use.+ # Select the database we are going to use.
- #+ #
- DEFINES += DATABASE_PGSQL+ DEFINES += DATABASE_PGSQL
- +
- # Suppress unknown pragmas GCC warnings.+ # Suppress unknown pragmas GCC warnings.
- #+ #
- QMAKE_CXXFLAGS_WARN_ON = $$QMAKE_CXXFLAGS_WARN_ON -Wno-unknown-pragmas+ QMAKE_CXXFLAGS_WARN_ON = $$QMAKE_CXXFLAGS_WARN_ON -Wno-unknown-pragmas
- +
- # Link to the ODB runtime libraries.+ # Link to the ODB runtime libraries.
- #+ #
- LIBS += -lodb-pgsql+ LIBS += -lodb-pgsql
- LIBS += -lodb-qt+ LIBS += -lodb-qt
- LIBS += -lodb+ LIBS += -lodb
- +
- # ODB compilation rules. Normally you don't need to change anything here.+ # ODB compilation rules. Normally you don't need to change anything here.
- #+ #
- odb.name = odb ${QMAKE_FILE_IN}+ for(dir, QMAKE_INCDIR_QT) {
- odb.input = ODB_FILES+ ODB_FLAGS += -I$${dir}
- odb.output = ${QMAKE_FILE_BASE}-odb.cxx+ }
- odb.commands = odb $$ODB_FLAGS ${QMAKE_FILE_IN}+
- odb.depends = ${ODB_FILES}+ odb.name = odb ${QMAKE_FILE_IN}
- odb.variable_out = SOURCES+ odb.input = ODB_FILES
- odb.clean = ${QMAKE_FILE_BASE}-odb.cxx ${QMAKE_FILE_BASE}-odb.hxx ${QMAKE_FILE_BASE}-odb.ixx ${QMAKE_FILE_BASE}.sql+ odb.output = ${QMAKE_FILE_BASE}-odb.cxx
- QMAKE_EXTRA_COMPILERS += odb+ odb.commands = odb $$ODB_FLAGS ${QMAKE_FILE_IN}
- + odb.depends = ${ODB_FILES}
- odbh.name = odb ${QMAKE_FILE_IN}+ odb.variable_out = SOURCES
- odbh.input = ODB_FILES+ odb.clean = ${QMAKE_FILE_BASE}-odb.cxx ${QMAKE_FILE_BASE}-odb.hxx ${QMAKE_FILE_BASE}-odb.ixx ${QMAKE_FILE_BASE}.sql
- odbh.output = ${QMAKE_FILE_BASE}-odb.hxx+ QMAKE_EXTRA_COMPILERS += odb
- odbh.commands = @true+
- odbh.CONFIG = no_link+ odbh.name = odb ${QMAKE_FILE_IN}
- odbh.depends = ${QMAKE_FILE_BASE}-odb.cxx+ odbh.input = ODB_FILES
- QMAKE_EXTRA_COMPILERS += odbh+ 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. 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:+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.
- pkg-config --cflags-only-I QtCore+6. Next save the .pro file and build the project.
-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.+If instead of using the system Qt installation you would prefer to use one that comes with Qt Creator, the the above steps need to be altered as follows.
-6. Next save the .pro file and build the project.+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 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.

Revision as of 14:07, 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 = --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.
#
for(dir, QMAKE_INCDIR_QT) {
  ODB_FLAGS += -I$${dir}
}

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 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 one that comes with Qt Creator, the 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 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