Using XSD with Eclipse CDT
From Code Synthesis Wiki
Revision as of 14:30, 3 February 2010
Below are the step-by-step instruction for building the example/cxx/tree/hello/ example with Eclipse CDT. You can use the same approach for other examples or for your own application. Here we assume XSD is installed in /opt/xsd and that the Xerces-C++ library was built and installed in /opt/xerces-c. If you haven't built Xerces-C++ yet you can try the following:
$ ./configure --prefix /opt/xerces-c $ make $ sudo make install
1. Create a directory for the hello example and copy driver.cxx, hello.xml, and hello.xsd 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 C++ files from the schema yet.
3. In Project Explorer right-click on hello.xsd 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 with the '../' path, separated by semicolon. In our case it will be:
../hello.hxx;../hello.cxx;../hello.ixx
In the "Command" field put the XSD 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/xsd/bin/xsd cxx-tree --generate-inline --output-dir .. ../hello.xsd
In the "Description" field put something like "xsd hello.xsd" or "compiling hello.xsd". 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 libxsd library headers, /opt/xsd/libxsd in our case. Then add the path for the Xerces-C++ headers, /opt/xerces-c/include in our case. Change to the "Library Paths" tab and add the path to the libxerces-c.so/libxerces-c.a libraries, /opt/xerces-c/lib in our case. Click "Apply".
Change to "C/C++ Build"->"Settings"->"GCC C++ Linker"->"Libraries". Add 'xerces-c' to the "Libraries list". Click "Ok" to close the dialog.
5. Now Eclipse knows how to build the generated C++ files from the schema. 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 hello.xsd, Eclipse will know to first recompile hello.xsd 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 schema is sufficient. In our case it will be ../hello.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 libxerces-c.so library is not found. There are several ways to resolve this problem, listed from more general to more specific:
1. Add the /opt/xerces-c/lib path to the /etc/ld.so.conf file. This will allow you and other users on this computer to run programs linked to Xerces-C++ without any extra actions either from a terminal or from Eclipse.
2. Add the /opt/xerces-c/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/xerces-c/lib
This will allow you to run programs linked to Xerces-C++ without any extra actions either from a terminal or from Eclipse.
3. Add the /opt/xerces-c/lib path to the LD_LIBRARY_PATH variable in a terminal:
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/xerces-c/lib
This will allow you to run programs linked to Xerces-C++ 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/xerces-c/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.