Embedded Components Tutorial - Page 3
Converting aKtion to a Shared Library
The first real step to converting an existing application to an embedded component is to turn it into a shared library. An important thing to keep in mind is that this will not change the way you use the program currently. There will still be a standalone executable called aktion that you can run. The only difference at this point is that the aktion executable won't have all of its code compiled in -- it will load it all dynamically.
A second important thing to note is that this does not require any code changes. The only changes necessary are are a few simple lines in the Makefile. To be specific, the file to modify isn't Makefile -- it is Makefile.am. This is because aKtion, like all good KDE programs, uses the automake/autoconf system to automate Makefile creation. In this system, Makefile.in and Makefile are both created based on Makefile.am
Here are the lines from the Makefile that you will need to change.OLD: aktion-1.99/src/Makefile.am
Those lines basically say "to create the binary program 'aktion', use the source files specified with aktion_SOURCES, the link flags specified with aktion_LDFLAGS, and the libraries specified with aktion_LDADD"
As mentioned earlier, you need to change this so that the Makefile generates a shared library instead of one standalone executable.
Here is the result of the replacement:NEW: aktion-1.99/src/Makefile.am
Line by Line Analysis
What is happening in the above lines? Here is what each group means:
- Specify the outputs
bin_PROGRAMS = aktion lib_LTLIBRARIES = libaktion.la
These two lines detail what are the outputs. Specifically, they are an executable (specified with 'bin_PROGRAMS') called aktion and a shared library (specified with 'lib_LTLIBRARIES') called libaktion.la.
All Makefile variables after these will depend on those names. For instance, all variables that pertain to the executable will be in the form 'aktion_SOMEVARIABLE' and all variables that apply to the shared library will look like 'libaktion_la_SOMEVARIABLE'
- Convert former executable variables to shared library variables
libaktion_la_SOURCES = main.cpp aktionConf.cpp capture.cpp \ aktionVm.cpp kxanim.cpp principal.cpp libaktion_la_LDFLAGS = $(all_libraries) -version-info 1:0:0 -module libaktion_la_LIBADD = $(LIB_KFILE) $(LIBVM) -lkparts
You may notice that there are only three changes to the variables, here.
First: They are all preceded with libaktion_la_ instead of aktion_.
Reason: Everything is a shared library, now.
Second: The LDFLAGS variable has the additional '-version-info 1:0:0 -module'
Reason: Those are standard flags that are needed to create the shared library. You shouldn't have to change them.
Third: The LDADD variable is now called LIBADD
Reason: Same reason as the first -- this is a shared library now
- Create a standalone executable
aktion_LDFLAGS = $(KDE_RPATH) aktion_SOURCES = main.cpp aktion_LDADD = libaktion.la
Since all of the code is now in a shared library, the standalone executable now needs only a main() function (found in main.cpp in aKtion) and a pointer to the shared library.
That's it. If you build and install this, you'll notice that it works exactly the same as before. The only differences you should see is that the 'aktion' file is much smaller than before and there is an additional library in $KDEDIR/lib
If your own application uses a standard automake like the one above, then converting it to a shared library uses exactly the same steps (and code). There is nothing specific to aKtion in this procedure.
[ Edit ]