Qore Programming Language  1.7.2
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
DLLEXPORT void qore_init(qore_license_t license=QL_GPL, const char *default_encoding=0, bool show_module_errors=false, int init_options=QLO_NONE)
initializes the Qore library
@ QL_GPL
code to be used under the GPL license
Definition: common.h:86

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

DLLEXPORT void qore_cleanup()
frees all memory allocated by the library

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;
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48

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
// ... rest of code here
}
#define PO_DEFAULT
no parse options set by default
Definition: Restrictions.h:107
safely manages QoreProgram objects; note the the destructor will block until all background threads i...
Definition: QoreProgram.h:904

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
// 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
DLLEXPORT void handleWarnings()
calls ExceptionSink::defaultWarningHandler() on all exceptions still present in the object and then d...
DLLEXPORT void handleExceptions()
calls ExceptionSink::defaultExceptionHandler() on all exceptions still present in the object and then...

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