All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Embedding Qore Code

# Embedding Qore Code

## Initializing and Closing the Qore Library

The first step is to initialize the library. To do this, call qore_init() as follows (note that this function can only be called once and must be called before any other functionality of the Qore library is used):

// qore_init() called using the library under the GPL license, appropriate for a GPL program
// qore_init() optionally takes several other arguments as well - initializes the openssl and libxml2 libraries as well
qore_init(QL_GPL);

When your program terminates, you should call qore_cleanup() as follows:

qore_cleanup();

## Parsing and Running Qore Code

First you should declare a variable of type ExceptionSink to capture and manage Qore exceptions (declare an additional variable if you want to capture and manage warnings when parsing Qore code):

ExceptionSink xsink, wsink;

Then you can use the QoreProgramHelper class to manage QoreProgram objects. The QoreProgramHelper class contains a QoreProgram object and calls QoreProgram::waitForTerminationAndDeref() in the destructor. The constructor takes a pointer to an ExceptionSink object, so make sure that the ExceptionSink object has at least as long a scope as the QoreProgramHelper object, as follows:

ExceptionSink xsink, wsink;
{
// creates and manages a QoreProgram object
QoreProgramHelper pgm(PO_DEFAULT, xsink);
// ... rest of code here
}

Then the QoreProgramHelper object can be used like a QoreProgram object. For example, to parse a file named "test.q" and run it, do the following:

ExceptionSink xsink, wsink;
{
// creates and manages a QoreProgram object
QoreProgramHelper pgm(PO_DEFAULT, xsink);
// parses a file with all warnings enabled
pgm->parseFile("test.q", &xsink, &wsink, QP_WARN_ALL);
// display any warnings immediately
wsink.handleWarnings();
// execute program if there were no parse exceptions
if (!xsink)
pgm->run(&xsink);
}
// display any exceptions on stdout
xsink.handleExceptions();

There are many functions for parsing and running Qore code; see the QoreProgram class documentation for more information.