Using ODB with Eclipse CDT

From Code Synthesis Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 14:32, 11 July 2011
Boris (Talk | contribs)
(Add instructions on using ODB with Eclipse)
← Previous diff
Current revision
Boris (Talk | contribs)
(Add instructions on using ODB with Eclipse)

Current revision

Below are the step-by-step instructions for building the hello ODB example with Eclipse CDT. You can use the same approach for other examples or for your own application. Here we assume that the ODB compiler is installed in /opt/odb and that the ODB runtime libraries (libodb and libodb-<db>, where <db> stands for the database system that you are using) were built and installed in /opt/odb/include (header files) and /opt/odb/lib (library files). For more information on how to build the ODB runtime libraries refer to the ODB documentation.

1. Create a directory for the hello example and copy the driver.cxx, database.hxx, and person.hxx files into it. Do not copy the makefile.

2. In Eclipse, select "File"->"New"->"C++ Project". Type the directory name from step 1 as project name so that the Location field points to the directory created in step 1. Click "Next" then "Finish". At this stage Eclipse will try to build driver.cxx which will result in compile errors. This is expected since we haven't generated the database conversion code from the person.hxx header file yet.

3. In Project Explorer right-click on person.hxx and select "Properties". Select "C/C++ Build"->"Settings", then select the "Build Steps" tab, and change Configuration to "All". Change "Disable Custom Build Step" to "Apply Custom Build Step Overriding Other Tools". Leave the "Additional Input Files" field blank. For the output file names, list all the generated C++ files and the database schema file, if any, with the '../' path, separated by semicolon. In our case it will be:

../person-odb.hxx;../person-odb.cxx;../person-odb.ixx;../person.sql

In the "Command" field put the ODB compiler command line that is used to compile the schema. Also add '--output-dir ..' and use the '../' path for the schema file. In our case it will be:

/opt/odb/bin/odb --database <db> --generate-query --generate-schema --output-dir .. ../person.hxx

Here again <db> stands for the database system that you are using. In the "Description" field put something like "odb person.hxx" or "compiling person.hxx". Click "Ok" to close the dialog.

4. In Project Explorer right-click on the project name and select "Properties". Select "C/C++ General"->"Paths and Symbols", select "GNU C++", select the "Includes" tab, and change Configuration to "All". Add the path for the ODB runtime library headers, /opt/odb/include in our case. On this step, you may also need to a path to the database client library headers. Change to the "Library Paths" tab and add the path to the runtime library files, /opt/odb/lib in our case. Here, again, you may also need to a path to the database client library files. Click "Apply".

Change to "C/C++ Build"->"Settings"->"GCC C++ Linker"->"Libraries". Add 'odb' and 'odb-<db>' to the "Libraries list". Click "Ok" to close the dialog.

5. Now Eclipse knows how to build the database conversion code from the person.hxx header. Unfortunately, Eclipse is not smart enough to figure out that some files included in driver.cxx are those generated C++ files and it could have successfully compiled driver.cxx if only it compiled the schema first.

So when we don't have the generated C++ files, we need to "bootstrap" the project by explicitly compiling them. Once the files are generated, Eclipse will figure out all the dependencies and everything will work as expected (that is, if we modify person.hxx, Eclipse will know to first recompile person.hxx header and only then recompile driver.cxx).

The easiest way to do this bootstrapping is to add a special target that will trigger the schema compilation. Go to "Project"->"Make Targets"->"Create". In "Target Name" type something like Bootstrap. Then uncheck the "Same as the target name" box and enter the name of the generated file with the '../' path, one file per header is sufficient. In our case it will be ../person-odb.hxx. Click "Ok" to close the dialog.

6. Now we can bootstrap the project by doing Project->Make Targets->Build and selecting the Bootstrap target. After that we can continue with normal builds.

If you try to run or debug the resulting program, you may get an error saying the the libodb.so or libodb-<db>.so library is not found. There are several ways to resolve this problem, listed from more general to more specific:

1. Add the /opt/odb/lib path to the /etc/ld.so.conf file. This will allow you and other users on this computer to run programs linked to the ODB runtime libraries without any extra actions either from a terminal or from Eclipse.

2. Add the /opt/odb/lib path to the LD_LIBRARY_PATH variable in your .bash_login or similar (and re-login):

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/odb/lib

This will allow you to run programs linked to the ODB runtime libraries without any extra actions either from a terminal or from Eclipse.

3. Add the /opt/odb/lib path to the LD_LIBRARY_PATH variable in a terminal:

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/odb/lib

This will allow you to run programs linked to the ODB runtime libraries from this terminal and from Eclipse if you start it from this terminal.

4. In Eclipse in Project Explorer right-click on the project name and select "Properties". Select "Run/Debug Settings" and either edit an existing configuration or create a new one. Select the "Environment" tab and click "New". Enter LD_LIBRARY_PATH in the "Name" field and /opt/odb/lib in the "Value" field. Click "Ok" to close all the dialogs. This will allow you to run/debug this project from the Eclipse IDE only.

Personal tools