side4linux, a simple integrated development
environment!
CLC
Demo-4
Allow
the entry of 'include'
search directories in CLC command line 'C'
projects.
Purpose:
- Provides basic introduction to adding
search paths to find 'include' files such as 'glib.h'.
- The sample command line program will
output the value of a text string 'Hello World!' using the glib library
'glib-2.0'
- You will be shown how to,
- call a function
called 'g_print()' which will output to the command-line the string
'Hello World',
- link the external dynamic
library
'glib-2.0' into the project,
- add the 'Include" search path
which the compiler will use to retreive include files such as 'glib.h',
- compile the project into the
binary executable program,
- run the program to test it,
- remove an include search path,
- run 'ldd' to detect which
libraries are dynamically linked to the program.
Requirements:
Step-1 Call a 'glib' function
called 'g_print()'.
- 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 'includes' 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 'Includes', this opens
the 'Includes' project folder.
- Double click on 'includes.prf',
this opens the 'Includes'
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. )
- INCLUDES
(
nothing here )
- 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
|
#include <stdio.h>
#include <stdarg.h>
#include
<glib.h>
// this is added so that we can link to the external dynamic library
'glib-2.0' to use it's 'g_print()' function.
int main(int argc, char *argv[])
{ // output 'Hello World' using the 'g_print()' function
g_print("\nHello World!\n); //
modified existing 'printf()' function.
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/DEMOS/CLC/Includes
for your reference in case of trouble )
Note: Notice that files and filenames mentioned here are 'case sensitive' !!!!!
Step-2
Add in the dynamic
library to link to ( glib )
HINT: In this and the next step just copy and paste the entries
instead of trying to do it manually to prevent errors!
- From the Main Menue click on 'Project>Libraries>Add Library>Add
Dynamic' and enter the library glib-2.0
into the Dialog
- and click on 'OK',
the IDE will now rebuild the project,
- You still have to add the 'Include
search paths' which we will do next..
Step-3
Add in the 'Include' search path for ( glib )
- Click on 'Project>Includes>Add
Include Directory' and add /usr/lib/glib-2.0/include
- and click on 'OK' once
only.
- The next dialog comes up which asks wether you wish to
rebuild the project, click on cancel
as we have another path to add.
- Click on 'Project>Includes>Add
Include Directory' and add /usr/include/glib-2.0
- The
next dialog comes up which asks wether you wish to rebuild the project, click on 'OK',
the IDE will now rebuild the project,
Step-4
Result of building and running
the program,
On building the program notice
in the 'Output Window'
the lines
libtool:
link: gcc -Wall -g -g -O2 -o includes main.o
/usr/lib/libglib-2.0.so -lm
It shows that we are linking to the 'glib' dynamic library at runtime.
On running the program we get the following or something
similar ( don't forget the './' dot forward slash before the program
name. ),
db@wks1:~/Data/Projects/SIDE/DEMOS/SIDEdemos/CLC/Includes/src$
./includes
Hello World!
Step-5
Running the 'ldd' Program
Trying to determine which libraries are
dynamically linked to 'includes'.
The result is,
db@wks1:~/Data/Projects/SIDE/DEMOS/SIDEdemos/CLC/Includes/src$
ldd
./includes
linux-gate.so.1 => (0xb8067000)
libglib-2.0.so.0 => /usr/lib/libglib-2.0.so.0
(0xb7f96000)
libm.so.6 => /lib/tls/i686/cmov/libm.so.6
(0xb7f70000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6
(0xb7e0c000)
libpcre.so.3 => /lib/libpcre.so.3 (0xb7dda000)
/lib/ld-linux.so.2 (0xb8068000)
The result shows that we are
dynamically linking to,
- 'libc' (
default option for all commandline 'C'
programs ),
- the maths library 'libm',
- our newly added library 'libglib-2.0' and
- 'libpcre' which
glib has added by itself!
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.
NOTE: If you strike trouble then look
in ~~/SIDE/DEMOS/SIDEdemos/CLC/Includes
We will provide other demos as
'side4linux' develops to
cover 'C' programming and integration into a real world machine
controller.