side4linux, a simple integrated development
environment!
CLC
Demo-3
Link to an external dynamic
library.
Purpose:
- Provides basic introduction of linking
to an external dynamic
library.
- The sample command line program will
output the trigonometric sine of an angle using the maths library
'libm.so'
- You will be shown how to,
- create a function
called 'printSin()' which will output to the command-line the sine of
the angle 45 degrees ( 'The Trig. Sine of 45 degrees is 0.7071 ).
- link the external library
'libm.so' library
into the project.
- compile the project into the
binary executable program.
- run the program to test it.
- remove a dynamically linked
library.
- run 'ldd' to detect which
libraries are dynamically linked to the program.
Requirements:
Step-1 Create a function
called 'printSin()'.
- Open 'side4linux'
and click on 'Project>New Project'
in
the Main Menu.
- Select 'Command Line 'C'
Program' and click on 'NEXT'
in the 'Select New Project Type'
Dialog Box.
- Select the 'PRODUCTS'
Product Area in the Product Selection Dialog and click on 'OK',
- Select 'MC-1'
from the Product Area selection Combo Box and click on
the 'Next' button,
- In
the New Project dialog enter the name of 'trydynamiclib' and click on 'Build'.
- Once the Project is 'Built',
close and then re-open it to let
things settle into place.
- 'Project>Close
Project'
- 'Project>Open Project'
- Double click on the 'PRODUCTS'
Product Area,
- Double click on the 'MC1' Product,
- In
the file dialog double click the 'CLC'
folder, this takes you
to where command line 'C' language projects are kept.
- Double
click on 'Trydynamiclib', this opens
the 'Trydynamiclib' project folder.
- Double click on 'trydynamiclib.prf',
this opens the 'Trydynamiclib'
project file.
- 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'
- Visual (
nothing here )
- Files
( main )
- D-Libs ( m ) =
maths 'libm.so' dynamic
library added automatically by the new project process.
- I-Libs ( Internal
Static libraries, but nothing here, yet. )
- Click on the 'TXT'
Toolbutton and open 'main.c'
and edit it to be the same as follows, then click on the 'Build'
Toolbutton,
Filename
|
Contents
|
main.c
|
#define DegreeToRadian 57.295779506
#include <stdio.h>
#include <stdarg.h>
#include
<math.h>
// this is added so that we can link to the external maths library
'libm.so'
int main(int argc, char *argv[])
{ // calculating the trig. sine of the angle 45 degrees
double answer = 0.0, radianAngle = 0.0;
radianAngle = 45/DegreeToRadian; // need radians because
this is what the sin() function expects.
answer = sin(radianAngle);
printf("\nThe Trig. Sine of 45 degrees is %4.4lf\n\n ",answer);
return(0);
}
|
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/Testdynamic
for your reference in case of trouble )
Note: Notice that files and filenames mentioned here are 'case sensitive' !!!!!
Steps-2/3/4
Result of building and running
the program,
- On building the program notice in the 'Output Window'
the line gcc -Wall -g -g -O2 -o trydynamic
main.o -lm
- It shows that we are linking to the 'libm.so' dynamic library at runtime.
- On running the program we get the following or something
similar,
db@wks1:~/Data/Projects/SIDE/DEMOS/SIDEdemos/CLC/Trydynamic/src$
./trydynamic
The Trig. Sine of 45 degrees is 0.7071
db@wks1:~/Data/Projects/SIDE/DEMOS/SIDEdemos/CLC/Trydynamic/src$
Step-5
Removing the 'libm' library from the
Project,
- Left click on the 'D-Libs'
tag of the 'Project File Tree
Explorer',
- Left click on the 'm'
library in the 'Project
File Tree Explorer' to
be removed,
- From the main menue, click on 'Project>Libraries>Remove
Library>Remove Dynamic',
- Click on 'OK'
in the the acceptance dialog,
- Click on 'OK'
in the rebuild dialog to rebuild the project file,
- Click on the 'Build'
Toolbutton to rebuild the program without the removed library.
- Result of building the program
without 'libm',
- On building the program notice in the 'Output Window' the error
messages,
main.o: In function `main':
/home/db/Data/Projects/SIDE/DEMOS/SIDEdemos/CLC/Trydynamic/src/main.c:33:
undefined reference to `sin'
collect2: ld returned 1 exit status
make[3]: *** [trydynamic] Error 1
- Let us now add back the 'libm' library to the 'Trydynamic'
project,
- In the side4linux IDE, click on 'Project>Libraries>Add Library>Add
Dynamic'
- In the dialog box that opens type in 'm' ( note leave off the 'lib' as it is presumed to already
exist! ).
- Click on 'OK'
in the dialog and click on 'OK'
in the next dialog to re-build the project.
- Click on the 'Build'
Toolbutton and notice that the build process completes without error!
- Click on the 'D-Libs' tag in the 'Project File Tree
Explorer'
and see that 'm' is there.
Step-6
Running the 'ldd' Program
- Trying to determine which libraries are
dynamically linked to 'trydynamic'. The result is,
db@wks1:~/Data/Projects/SIDE/DEMOS/SIDEdemos/CLC/Trydynamic/src$
ldd ./trydynamic
linux-gate.so.1 =>
(0xffffe000)
libm.so.6 =>
/lib/tls/i686/cmov/libm.so.6 (0xb7f63000)
libc.so.6 =>
/lib/tls/i686/cmov/libc.so.6 (0xb7e19000)
/lib/ld-linux.so.2
(0xb7f9a000)
db@wks1:~/Data/Projects/SIDE/DEMOS/SIDEdemos/CLC/Trydynamic/src$
- The result shows that we are
dynamically linking to 'libc'
( default option for all commandline 'C' programs ) and also
to the library 'libm'.
Completion
of Demo-3,
- If you have got this far then congratulations!
- Close the newly created project 'Project>Close Project'.
- Note that it will be stored in the 'CLC' section of the 'MC-1' Product.
- Look for other useful dynamic libraries e.g. 'dir
/lib/*.so' or 'dir
/usr/lib/*.so'
- Look in your Distrbution's 'Package Manager' for useful
libraries to use.
- Open the 'Bdesign3d' example project in the
SIDEdemos/GNOMECGL
folder and check out what libraries it has.
We will provide other demos as
'side4linux' develops to
cover 'C' programming and integration into a real world machine
controller.