Demo-1 <<-BACK            HOME          NEXT-->>  Demo-3
side4linux, a simple integrated development environment!

CLC Demo-2 Link to an internal static library.


Purpose: 

Requirements:

Preamble:

Using this Integrated Development Environment 'side4linux' provides many benefits including the ability to link to internal static libraries and also to link to external dynamic libraries. Libraries hold a batch of useful functions and declarations that may be 'called' from your program to make it more useful. For the new-comer, a static library is one where the source code is compiled into a linkable 'object' file ( 'someLibraryName.a' ) and then linked directly into the program as part of the binary executable. Dynamic libraries are a bit different, usually the 'object' file is linked in only at 'run-time' and resides somewhere else on the system pre-compiled ( for example  /usr/lib or /usr/local/lib  as 'someLibraryName.so' ).

Why the difference between using static and dynamic libraries?

Well, using static libraries for everything would mean that your program would take forever to compile, be very large, slow to load and you may not have the source code to every library, only the linkable 'object' file. So dynamic linking is the preferred option!

What then are the benefits of a statically linked library?

A
statically linked library normally allows the program to be self-contained. This can be of benefit when different versions of the Operating System are supplied with different versions of the dynamic libraries. Your program may run on one version but not on the other because the dynamic libraries may not be backward or forward compatible ( some function that you have called may have been changed to perform differently, it could have been re-named, the calling parameters may have changed or the function could have been removed from the library completely! ).

Why include the static library in the project?

Can static libraries be used in every project?

It depends on the license governing conditions of use e.g. under the GPL you must provide the library in source code form and include a copy of the original version You only have copyright over your changes while the original author's rights remain protected. This may mean that 'commercial' programs only supplied in binary executable form may need to dynamically link to LGPL ( or other ) licensed libraries only and a static library cannot be used unless it is all of your own work. It may be permissible to sell the binary executable program and provide a copy or a link to a copy of the library's source code, this depends on the end user license agreement of the library involved.


Step-1 Using a static library.

But first let us take a tour of the finished product that was prepared earlier.....
  1. Open 'side4linux' and click on 'Project>Open Project' in the Main Menu.
  2. Double click on the 'DEMOS' Product Area,
  3. Double click on the 'SIDEdemos' Product,
  4. In the file dialog double click the 'CLC' folder, this takes you to where 'C' command line projects are kept.
  5. Double click on 'Testlibs', this opens the 'Testlibs' project folder.
  6. Notice that project names are set to be an upper case letter followed by lower case letters (easier on the IDE's Project management routines).
  7. Double click on 'testlibs.prf', this opens the 'Testlibs' project file.
  8. Notice that the left bottom status bar window says 'Project: Testlibs' which is the Project Name
  9. Notice that the next window says 'Ptype: CLC' which is the Project Type.
  10. Click on the 'Open' toolbar button, observe the two files 'main.c', 'main.h'
  11. and double click on 'main.c' to open it.
  12. Click on the 'I-Libs' tab to the left of the Notebook and notice that there is a static library called 'internalExample'.
  13. Notice the 'main.c' file in the 'Notebook' has at line-23,   #include "libinternalExample/hello.h".
  14. Notice the 'main.c' file in the 'Notebook' has at line-27,   sayHello();   the function to call from the static library.
  15. Now click on the 'Build' toolbar button the Output Window should end with  +++ Successful Make command! +++
  16. and you should see something like this in the following screen shot,


From the screen shot above you can see that the 'Output window' has the output from the 'C' compiler and the
'Status bar' says 'Pbuild: OK!'

Now let us run the compiled binary by opening a terminal in the 'src' directory as follows,

Using 'Nautilus' change to the --SIDE/CLC/Testlibs/src directory




If you have loaded the 'Open with terminal' option from Synaptic Program Manager right click on the Nautilus window and left click on
the 'Open with Terminal' option which will open a terminal, then type './testlibs' in the terminal window and press the 'Enter' key.
This should result in the following text to appear on the terminal window (or something similar containing the line '
Hello World!',

db@wks1:~/Data/Projects/SIDE/CLC/Testlibs/src$ ./testlibs

Hello World!
db@wks1:~/Data/Projects/SIDE/CLC/
Testlibs/src$

The './' at the start of the program name means 'to launch the copy of the program in the current folder'

Leave this copy of side4linux open so that you can refer back to this demo example.

Now change to another workspace where you will build a new command line 'C' project.
You will use this new project to continue this Demo.


Step-2 Creating a static library.

Let us start by making a new 'C' command line program called 'trystaticlib'
  1. Open 'side4linux' and click on 'Project>New Project' in the Main Menu.
  2. Select 'Command Line 'C' Program' and click on 'NEXT' in the 'Select New Project Type' Dialog Box.
  3. Select the 'PRODUCTS' Product Area in the Product Selection Dialog and click on 'OK',
  4. Select 'MC-1' from the Product Area selection Combo Box and click on the 'Next' button,
  5. In the New Project dialog enter the name of 'trystaticlib' and click on 'Build'.
  6. Once the Project is 'Built', close and then re-open it to let things settle into place.
  7. 'Project>Close Project'
  8. 'Project>Open Project'
  9. Double click on the 'PRODUCTS' Product Area,
  10. Double click on the 'MC1' Product,
  11. In the file dialog double click the  'CLC' folder, this takes you to where command line 'C' language projects are kept.
  12. Double click on 'Trystaticlib', this opens the 'Trystaticlib' project folder.
  13. Double click on 'trystaticlib.prf', this opens the 'Trystaticlib' project file.
  14. Left click the tabs on the left hand side of the 'Notebook' and notice the file tree results, this is the 'Project File Tree Explorer'
    1. Visual  ( nothing here )
    2. Files    ( main )
    3. D-Libs ( m ) = maths  'libm.so' dynamic library added automatically by the new project process.
    4. I-Libs  ( Internal Static libraries, but nothing here, yet. )
Filename
Contents
Makefile.am
noinst_LIBRARIES = libStatic.a
libStatic_a_SOURCES = hello.c

hello.c
#include <stdio.h>
 
void sayHello(void)
{
  printf("Hello World! \n");
}

hello.h
void sayHello(void);


Hint: Make sure that there is a blank line above and below your source code in case the compiler you are using complains!
         ( a completed copy of this project is stored in -/SIDE/PRODUCTS/MC1/CLC/HelpStaticLib for your reference in case of trouble )
Note: Notice that filenames mentioned here are 'case sensitive' so 'Makefile.am' is not the same as 'makefile.am' !!!!!     

Filename
Contents
main.c

#include <stdarg.h>
#include "libStatic/hello.h"                     // from "libStatic" our example of an internal statically linked library for side4linux

int main(int argc, char *argv[])
{
  sayHello();                                             // calling a
function from our new library
  return(0);
}


db@wks1:~/Data/Projects/SIDE/PRODUCTS/MC1/CLC/Trystaticlib/src$ ./trystaticlib
Hello World!
db@wks1:~/Data/Projects/SIDE/PRODUCTS/MC1/CLC/Trystaticlib/src$

   Note: There is a working copy of this demo in ~~/SIDE/DEMOS/SIDEdemos/CLC/Testlibs


We will provide other demos as 'side4linux' develops to cover 'C' programming and integration into a real world machine controller.