09-20-2013 10:16 AM
When creating a new app from scratch, most developers will find themselves writing bits of code they previously wrote in their last project, the one before that and the one before that. Sometimes we have to accept this as the time and effort required to somehow automate the creation of repeated code far outweighs the effort required to simply repeat it. Changing the default app icon for the custom image provided by the customer for example.
However, sometimes there are project tasks which it may well be worthwhile separating into a distinct chunk of work and creating a component which can be reused in future projects at will. One such task we recently encountered was the ability to write debug logs to a device’s file system. The motivation for this is to be able to examine exactly what was going on when a user in the field experienced a bug; the user could simply email the debug file to us developers for us to examine and hopefully find the problem. The platform we were working on at the time was BlackBerry 10; this provides debug output to a console in the development environment, but no ready made way to pipe this to persistent file storage.
Of course, one way of producing a reusable component would be to write the necessary source files which could be copied and pasted into future projects. However, we decided we’d like to learn about creating a standalone library which other developers could simply include in their projects without ever having to see or concern themselves with the source code. There weren’t any up-to-date tutorials available on how to do this so there was a sense of trial and error in the process, but we were eventually successful. These are the required steps:
1 - in Momentics IDE v10.2 (recommended as this removes the manual steps previously required when it comes to including a library in a project), create an empty Cascades project, giving it the name of your library
2 - strip out everything not needed:
a - delete main.qml
b - the main method in main.cpp can be empty, but not deleted
c - delete the ApplicationUI methods (in ,10.2 of the IDE you get an onSystemLanguageChanged handler)
and empty the constructor
d - remove QObject inheritance (we did this to keep things simple)
3 - edit the .pro file:
a - add TEMPLATE = lib
b - add shared to the CONFIG += line for a shared library, or staticlib for a static library
4 - to avoid name clashes with users of the library, rename the class, cpp file, hpp file and #ifndef/#define lines in the header to something other than the default "ApplicationUi"
5 - implement your library. Any functions which will be accessed by library users need the Q_DECL_EXPORT macro in the signature:
Q_DECL_EXPORT int CalcLib::addTwoInts(int a, int b)
return a + b;
and you need to
to access this
6 - right click and export it - this will produce the .so file or .a file, depending on whether it's a shared or static library, in the form lib<PROJ_NAME>.so. (There is also a .so file generated in the form <PROJ_NAME>.so - ignore this one
To use the library in a project:
1 - right click the project
2 - choose configure | add library | external library, then navigate to the .so (lib<PROJ_NAME>.so) file of the library and public header folder of the library's header (create a "lib" folder in the projects folder structure to hold the library and header)
3 - add
where you will be using the library and call the functions as required
We hope you find this useful.
09-21-2013 09:09 PM