Thursday, February 19, 2009

Testing EOF of DataInputStream JAVA

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class TestEOF {
  // Throw exceptions to console:
  public static void main(String[] argsthrows IOException {
    DataInputStream in = new DataInputStream(new BufferedInputStream(
        new FileInputStream("TestEOF.java")));
    while (in.available() != 0)
      System.out.print((charin.readByte());
  }
///:~



Source

Tuesday, January 20, 2009

Shahabinary Alpha 0.002

Hi.

Shahabinary English to Norwegian dictionary :

Shahabinary is a OS free, freeware PC application which is at least runable on Linux & Windows (TESTED). The code is not open source this time (like Shahabinary 0.001) but I will post the source code in near future. Due to some technical difficulties with GUI, the word recognition ability has been disabled but will be functional again in version 0.003. This version enjoys a better GUI and much more comfortable design with significant changes in the code which makes it smaller and easier to compile. The only thing you need to do for running is to unzip the folder and double click on Shahabinary v. 0.0002.jar.

Again I repeat the words and database are not trustable and this is just an exercise in my learning progress. This project the focus is mostly on the technical part and not the dictionary parts of the application.

Click here
to download the latest Shahabinary ...

Enjoy ...

Thursday, January 08, 2009

How to run an exe file from java

If you want something which you do not need to read much about it but it is old :

Runtime.getRuntime().exec( "myprog.exe" );

will spawn an external process (usually a program written in some language other than Java) that runs in parallel with the Java execution. In W95/W98/Me/NT/W2K/XP/W2K3, you must use an explicit *.exe or *.com extension on the parameter. In Vista it will still work even if you leave it off. Be careful to include it, or your code will mysteriously fail on the older operating systems.

It is also best to fully qualify executable names so that the system executable search path is irrelevant, and so you don’t pick up some stray program off the path with the same name.


-----


ATTENTION : Starting with JDK 1.5, exec has been replaced by ProcessBuilder. start. You should likely not be using exec for new programs. However, studying exec is useful to understand old programs that use exec and to understand why ProcessBuilder is designed the way it is. They have many things in common, especially the way command interpreters and internal commands must be handled, and they both use the same Process class. So read about exec first, then enjoy the improved ProcessBuilder.

example :

/ using ProcessBuilder to spawn an process
ProcessBuilder pb = new ProcessBuilder( "dolunch.exe", "firstarg", "secondarg" );

Map env = pb.environment();

// insert set FRUIT=strawberry into environment just for our children
env.put( "FRUIT", "strawberry" );

// remove set LIVERCOURSE=YES from environment just for our children
env.remove( "LIVERCOURSE" );

// modify set WINE=pinot to WINE=pinot blanc
env.put( "WINE", env.get("WINE") + " blanc" );

// set up the working directory.
// Note the method is called "directory" not "setDirectory"
pb.directory( new File( "/lunchDir" ) );

// merge child's error and normal output streams.
// Note it is not called setRedirectErrorStream.
pb.redirectErrorStream( true );

Process p = pb.start();
// From here on it, it behaves just like exec, since you have the
// exact same Process object.
// ...

// You can reuse the ProcessBuilder objects.
// They retain their initialisation.
Process sister = pb.Start();


SOURCE

reqading txt files with UTF8 formats

Well this code helped me reading Persian language characters from a text files.


java.io.FileInputStream fis=new java.io.FileInputStream(
"File Path");
java.io.InputStreamReader isr=new java.io.InputStreamReader(
fis, "UTF8");
java.io.BufferedReader fin=
new java.io.BufferedReader(isr);
String line;
while((line=fin.readLine())!=null){
//Statements
}//end of while

How do I right-align a JTextField?

short answer : add this code to the constructor ...
myTextField.setHorizontalAlignment(SwingConstants.TRAILING);

Tuesday, January 06, 2009

Finally Shahabinary Alpha 0.001 is ready :-)

Finally after a long delay because of my exams the Shahabinary dictionary alpha version 0.001 probably full of non discovered bugs with very little amount of words is ready to download for coders. This dictionary is coded in Java and is a open source free Norwegian to English dictionary. I hope this is my first step toward the next better versions of this dictionary specially from Norwegian to Persian and the other way.

Click HERE to download.

Wish me luck and enjoy...

Shahab.

How to make an iterator for a Hashmap in Java

Iterate through the values of Java HashMap example :


  1. /*
  2. Iterate through the values of Java HashMap example
  3. This Java Example shows how to iterate through the values contained in the
  4. HashMap object.
  5. */
  6. import java.util.Collection;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. public class IterateValuesOfHashMapExample {
  10. public static void main(String[] args) {
  11. //create HashMap object
  12. HashMap hMap = new HashMap();
  13. //add key value pairs to HashMap
  14. hMap.put("1","One");
  15. hMap.put("2","Two");
  16. hMap.put("3","Three");
  17. /*
  18. get Collection of values contained in HashMap using
  19. Collection values() method of HashMap class
  20. */
  21. Collection c = hMap.values();
  22. //obtain an Iterator for Collection
  23. Iterator itr = c.iterator();
  24. //iterate through HashMap values iterator
  25. while(itr.hasNext())
  26. System.out.println(itr.next());
  27. }
  28. }
  29. /*
  30. Output would be
  31. Three
  32. Two
  33. One
  34. */

Saturday, December 20, 2008

Install flash player on ubuntu

Q. How do I install Flash Player for Firefox web browser under Ubuntu Linux?

A. First, close running Firefox.

Open the terminal (shell prompt)

Type the following command to install flash player:

$ sudo apt-get install flashplugin-nonfree

Now flash player should be working. Visit youtube or any other site to view flash content.

Troubleshooting tip

Update: This issue only relevant to older Ubuntu Linux version such as 6.04.

Some people may find voice is not working with newly installed flash player. Type following commands to solve this problem (thanks to macewan.org):
$ sudo apt-get install alsa-oss
$ gksudo gedit /etc/firefox/firefoxrc

Find line that read as FIREFOX_DSP and set to:
FIREFOX_DSP="aoss"

Save and close the file.

Close Firefox and restart it again.


Click here to see the source.


Monday, December 01, 2008

Linux on USB memory stick

Now you can have your Linux on your USB memory stick and go around, boot different computers with your memory stick without installing or changing whatever crap(WINDOWS) on that computer which is not yours or for whatever reason you can not install Linux on it. There is a piece of software development called Unetbootin which makes this dream come true and with this piece of very handy technology, you just do not boot the system BUT makes changes that you can carry it around on your USB stick :-)

The following Linux distributions are supported by Unetbootin:

  • Ubuntu, Kubuntu, Xubuntu
  • Debian
  • Linux Mint
  • opensuse
  • Arch Linux
  • Damn Small Linux
  • Slitaz
  • Puppy Linux
  • FreeBSD
  • NetBSD
  • Fedora
  • PCLinuxOS
  • Gentoo
  • Zenwalk
  • Slax
  • Dreamlinux
  • Elive
  • CentOS
  • Mandriva
  • FaunOS
  • Frugalware Linux
Actually some have their own type like Ubuntu which tutorial for it follows here :

Ubuntu 8.10 offers a way to create a USB drive from the Live CD or an .iso. The question is, how well does this really work. In order to test we created several of these USB drive installs. The first thing to note is that the process is easy to do. Here is a step by step in create a USB Pen Drive disk.

Insert the USB Pen Drive once you have booted the Live CD. Now go to System/Administration and Create USB startup disk in the menu. You will see it has detected the 2 GB San Disk. You can use as small as 1 GB for the disk. You will also need to indicate the location of the Live CD or an .iso so the system can build the disk. Here we used the Live CD.


It provides you with the option to decide how much free space to allow for files that you create. What this does is give you a way to move this Pen Drive from computer to computer and save your files. A mobile Pen Drive.


Installation process is quick.

News source (NORWEGIAN) : http://www.hardware.no/artikler/kjor_linux_fra_minnepinnen/58108
Tutorial Source : http://beginlinux.wordpress.com/2008/11/18/ubuntu-810-usb-drive-review/

Tuesday, November 11, 2008

VLC doesn't show subtitle automaticly under Linux

The thing is this, the avi file can be played by VLC media player and if you have the subtitle file (.srt) in the same folder with the same name beside the avi file then it should be read by VLC media player automaticly. But not always, for example mine under Linux refused to add it to the movie. Here is the way to do it manualy :

You click on open file, you specify the video file, then you click on use subtitle file, and then specify the subtitle file, it does not have to have the same name, as long as you specify the correct path

also, the subtitles file might be wrong, try to open it with a text editor and check its contents.

also try opening another subtitles, just to make sure that the subtitles file is not corrupted/worng.

Source that helped me : http://ubuntuforums.org/archive/index.php/t-803063.html

Monday, November 10, 2008

How to set JAVA_HOME variable

Q : I just need a help to show me how to setup java path on Linux. How can I set JAVA_HOME and PATH variables for every user under my Linux system?

A : ~/.bash_profile is a startup script which generally runs once. This particular file is used for commands which run when the normal user logs in. Common uses for .bash_profile are to set environment variables such as PATH, JAVA_HOME, to create aliases for shell commands, and to set the default permissions for newly created files.

Set JAVA_HOME / PATH for single user

Login to your account and open .bash_profile file
$ vi ~/.bash_profile
Set JAVA_HOME as follows using syntax export JAVA_HOME=. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows:
export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
Set PATH as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Save and close the file. Just logout and login back to see new changes:
$ echo $JAVA_HOME
$ echo $PATH

Tip: Use the following command to find out exact path to which java executable under UNIX / Linux:
$ which java

Please note that the file ~/.bashrc is similar, with the exception that ~/.bash_profile runs only for Bash login shells and .bashrc runs for every new Bash shell.


Set JAVA_HOME / PATH for all user

You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:
# vi /etc/profile
Next setup PATH / JAVA_PATH variables as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin

Save and close the file.


Source : http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/

Friday, November 07, 2008

Schematic Diagrams, DC circuits

This is a source of emf (electromotive force), with a voltage of , measured in units of volts, V. The most common source you will see will be a battery.

This is a resistance, measured in units ohms ohms, . Most often it will be a resistor. However, wires connecting parts of a circuit are really not resistance-free. We can model this real-life case by putting a 'resistor' in the circuit which has the same resistance as the wires would have. Thus the wires can still be considered resistance-free since their resistance is represented by an additional resistor.

This is a capacitor, with capacitance C measured in units of farads, F. The perfect capacitor will have no resistance (or inductance; see next), and we will assume that this symbol represents such an ideal capacitor. If a capacitor is not ideal, that is, it has some resistance (or inductance) associated with it, then we can model this by putting a 'resistor' (or inductor) in parallel with the capacitor. This resistor (or inductor) will have the same resistance (or inductance) as the non-ideal capacitor.

This is an inductor, with inductance L, measured in units of henrys, H. To model a non-ideal inductor in a circuit (an inductor with an associated resistance or capacitance) we can put a resistor in series or a capacitor in parallel with the inductor. Giving these the same values as the inductor will represent an imperfect inductor.

--------------------------------------------------------------

1. Ohm's Law deals with the relationship between voltage and current in an ideal conductor. This relationship states that:

The potential difference (voltage) across an ideal conductor is proportional to the current through it.

The constant of proportionality is called the "resistance", R.

Ohm's Law is given by:

    V = I R
where V is the potential difference between two points which include a resistance R. I is the current flowing through the resistance. For biological work, it is often preferable to use the conductance, g = 1/R; In this form Ohm's Law is:
    I = g V
2. Material that obeys Ohm's Law is called "ohmic" or "linear" because the potential difference across it varies linearly with the current.

3. Ohm's Law can be used to solve simple circuits. A complete circuit is one which is a closed loop. It contains at least one source of voltage (thus providing an increase of potential energy), and at least one potential drop i.e., a place where potential energy decreases. The sum of the voltages around a complete circuit is zero.

4. An increase of potential energy in a circuit causes a charge to move from a lower to a higher potential (ie. voltage). Note the difference between potential energy and potential.

Because of the electrostatic force, which tries to move a positive charge from a higher to a lower potential, there must be another 'force' to move charge from a lower potential to a higher inside the battery. This so-called force is called the electromotive force, or emf. The SI unit for the emf is a volt (and thus this is not really a force, despite its name). We will use a script E, the symbol , to represent the emf.

A decrease of potential energy can occur by various means. For example, heat lost in a circuit due to some electrical resistance could be one source of energy drop.

Because energy is conserved, the potential difference across an emf must be equal to the potential difference across the rest of the circuit. That is, Ohm's Law will be satisfied:

    = I R
5. Here is a nice simulated experiment on Ohm's Law for you to test your understanding of this concept. Use the "back" button to return to this place.

Source : http://www.physics.uoguelph.ca/tutorials/ohm/

Thursday, October 30, 2008

IRC chat, ubuntu

Xchat is a nice gui irc(Internet Relay Chat) client and may already be on your system. If not, you can get it with :

sudo apt-get install xchat

A text-based alternative irc client is irssi(homepage http://irssi.org/). You can get irssi with :

sudo apt-get install irssi

IRC is a good way to get help. The main Ubuntu support channel is #ubuntu on the irc.freenode.net irc network. There are other Ubuntu channels as well, including #ubuntu-offtopic (for social chat), #ubuntu+1 (for the dev ubuntu release), #ubuntu-effects (for beryp/compiz/xgl in ubuntu) and lots of Ubuntu channels for different languages.

Tuesday, October 28, 2008

Shahabinary Dictionary, My Project

Project name : Shahabinary Dictionary
Description : Norwegian - English Dictionary with spell-checking
Language : Java
Program Type : Open Source
Programming environment : Eclipse
supported O.S. : Linux / Windows


Comments, My Type :

I have a on going project which is a Norwegian to English dictionary with spell checker. The program is supposed to get the Norwegian word and then search in its database to find it and show the different meanings of one word within numbers that shows importancy.
The program is supposed to be intelligent enough to find out if a word has been entered with wrong spelling(misssing one character, replacing two characters, ...) and try to recover the mistake by giving a list of possible existing words from its database. My words database, right now and in the upcoming first version is not good at all and inholds around 4000 words. The Beta version will be available soon ...

Thursday, October 23, 2008

Change Login background, Ubuntu

GDM is controlled by themes..

see System -> Administration -> Login Window

you will find a lot of themes for login here :

http://art.gnome.org/themes/gdm_greeter/

Friday, October 17, 2008

Dividing C programs into files and run them with makefile

well¸ with my experience of large scale C programs(a Mobile Operative System that can send SMS), I would really recommend you to divide your program into files because without it you will just go officially nuts ...

How to Divide a Program between Several Files

Where a function is spread over several files, each file will contain one or more functions. One file will include main while the others will contain functions which are called by others. These other files can be treated as a library of functions.

Programmers usually start designing a program by dividing the problem into easily managed sections. Each of these sections might be implemented as one or more functions. All functions from each section will usually live in a single file.

Where objects are implemented as data structures, it is usual to to keep all functions which access that object in the same file. The advantages of this are;

*The object can easily be re-used in other programs.
*All related functions are stored together.
*Later changes to the object require only one file to be modified.

Where the file contains the definition of an object, or functions which return values, there is a further restriction on calling these functions from another file. Unless functions in another file are told about the object or function definitions, they will be unable to compile them correctly.

The best solution to this problem is to write a header file for each of the C files. This will have the same name as the C file, but ending in .h. The header file contains definitions of all the functions used in the C file.

Whenever a function in another file calls a function from our C file, it can define the function by making a #include of the appropriate .h file.
Organisation of Data in each File

Any file must have its data organised in a certain order. This will typically be:

1.A preamble consisting of #defined constants, #included header files and typedefs of important datatypes.
2.Declaration of global and external variables. Global variables may also be initialised here.
3.One or more functions.

The order of items is important, since every object must be defined before it can be used. Functions which return values must be defined before they are called. This definition might be one of the following:

*Where the function is defined and called in the same file, a full declaration of the function can be placed ahead of any call to the function.
*If the function is called from a file where it is not defined, a prototype should appear before the call to the function.

A function defined as

float find_max(float a, float b, float c) { /* etc ... ... */

would have a prototype of

float find_max(float a, float b, float c);

The prototype may occur among the global variables at the start of the source file. Alternatively it may be declared in a header file which is read in using a #include.

It is important to remember that all C objects should be declared before use.
Compiling Multi-File Programs

This process is rather more involved than compiling a single file program. Imagine a program in three files prog.c, containing main(), func1.c and func2.c. The simplest method of compilation (to produce a runnable file called a.out) would be

cc prog.c func1.c func2.c

If we wanted to call the runnable file prog we would have to type

cc prog.c func1.c func2.c -o prog

In these examples, each of the .c files is compiled, and then they are automatically linked together using a program called the loader ld.
Separate Compilation

We can also compile each C file separately using the cc -c option. This produces an object file with the same name, but ending in .o. After this, the object files are linked using the linker. This would require the four following commands for our current example.

cc -c prog.c cc -c func1.c cc -c func2.c ld prog.o func1.o func2.o -o prog

Each file is compiled before the object files are linked to give a runnable file.

The advantage of separate compilation is that only files which have been edited since the last compilation need to be re-compiled when re-building the program. For very large programs this can save a lot of time.

The make utility is designed to automate this re-building process. It checks the times of modification of files, and selectively re-compiles as required. It is an excellent tool for maintaining multi-file programs. Its use is recommended when building multi-file programs.
Using make with Multi-File Programs

We have already used make to build single file programs. It was really designed to help build large multi-file programs. Its use will be described here.

Make knows about `dependencies' in program building. For example;

*

We can get prog.o by running cc -c prog.c.
*

This need only be done if prog.c changed more recently than prog.o.

make is usually used with a configuration file called Makefile which describes the structure of the program. This includes the name of the runnable file, and the object files to be linked to create it. Here is a sample Makefile for our current example

# Sample Makefile for prog
#
# prog is built from prog.c func1.c func2.c
#
# Object files (Ending in .o,
# these are compiled from .c files by make)
OBJS = prog.o func1.o func2.o
# Prog is generated from the object files
prog: $(OBJS)
$(CC) $(CFLAGS) -o prog $(OBJS)
# ^^^ This space must be a TAB.
# Above line is an instruction to link object files

This looks cluttered, but ignore the comments (lines starting with #) andthere are just 3 lines.

When make is run, Makefile is searched for a list of dependencies. The compiler is involved to create .o files where needed. The link statement is then used to create the runnable file.

make re-builds the whole program with a minimum of re-compilation, and ensures that all parts of the program are up to date. It has many other features, some of which are very complicated.

For a full description of all of these features, look at the manual page for make by typing

man make

source

Wednesday, October 08, 2008

Makefile in Linux

Why Use a Makefile?

In any project you can have any number of source files. Now imagine having to recompile even the simplest of programs consisting of two source files, binarytree.c and mainprog.c. You will need to type the following command:

gcc -o outprogname binarytree.c mainprog.c

Now, since that mainprog.c files takes ages to compile, you don't want to recompile it everytime, the same goes for the binarytree.c file. Now imagine you had a hundred source files (think big, and then some more).

A Makefile makes this simple by allowing us to specify rules (consisting of dependencies and commands) to build our project.
4.2.2 A Simple Makefile Example

The following is a very simple, but complete Makefile:

outprogname : binarytree.o mainprog.o
gcc -o outprogname binarytree.o mainprog.o

binarytree.o : binarytree.c
gcc -c binarytree.c

mainprog.o : mainprog.c
gcc -c mainprog.c

Please note that the indented lines begins with a tab character.

In the case where both files needs to be compiled, make will issue three commands - yes this can be wasteful - namely:

gcc -c binaryproc.c
gcc -c binarytree.c
gcc -o outprogname binarytree.o mainprog.o

It is clear that these are the indented lines. You could have executed all of these commands by hand, if you were so inclined (programmers are known to be lazy), but typing make is easier. Now going on to dissect the above, a Makefile consists of a set of rules. Each rule consists of dependencies and commands. The lines with the colons in are dependencies and the lines that are tabbed in is commands. For each dependency, the commands required to bring the target - the file(s) before the colon - up to date follows it one the next line, indented with a tab character. The file(s) to the the right of the colon is the files on which the target relies, these are almost always used as input to the commands on the lines following the dependency.

It should be clear that the two rules at the bottom of the given Makefile produces .o files, created from the .c files (the -c flag tells gcc to compile the source file, but not link it). The topmost rule takes the two object files and links them together to create the final executable.
4.2.3 How Does make Determine Which Commands to Execute?

There are two reasons why make would decide a file needs to be made:

1. The file does not exist
2. The file is outdated

The first case is simple, the file does not exist - nuff said. The second case should be reasonably clear as well. Considering the simple Makefile above, edit binarytree.c. You would now like to compile outprogname. It relies on binarytree.o and mainprog.o. binarytree.o in turn relies on binarytree.c and mainprog.o on mainprog.c. You've just edited binarytree.c, so as you probably expect, binarytree.o is outdated and as a result so is outprogname. Make then goes ahead and first builds binarytree.o and then outprogname using the commands given.

A file is considered outdated if and only if:

* It is listed on the left of a colon
* A file on the right of that same colon is newer than that file

File "a" is considered newer than file "b" if its last modified time is more recent than file "b"'s.
4.2.4 Make Variables

Suppose you would like to use another C compiler, say cc. Now you can either replace gcc right through with cc, or you could have been smart and used variables. A make variable, as in any programming language, is simply a place holder for another value. You can create such a variable in a Makefile with a line such as:

cc=gcc

Actually, variables in make work very much like in bash scripting. So to actually replace the value of cc (which evaluates to gcc) just use ${cc}. Same people prefer using () instead of {}, but it doesn't matter which one you choose. Now let's asume that we would also like to be able to specify different levels of optimization as well as those debugging flags Derick has mentioned. We would need to specify the -O flags to the compile commands and the -g flag to all of them. Ok, so let's create two more variables: The first, called cflags, contains the flags we would like to pass to the compiler, and the second, called ldflags, contains the options we would like to pass to the linking command. The resulting Makefile will look something like:

cc=gcc
cflags=-O3 -g
ldflags=-g

outprogname : binarytree.o mainprog.o
${cc} ${ldflags} -o outprogname binarytree.o mainprog.o

binarytree.o : binarytree.c
${cc} ${cflags} -c binarytree.c

mainprog.o : mainprog.c
${cc} ${cflags} -c mainprog.c

4.2.5 Built-in Make Variables

Now I'm really lazy, and I mean that. So I'm aiming at reducing the size of my Makefiles even more (the previous step actually enlarged it, but it made mass adjustments easier - similar to constants in large programs). Also, I would like to keep the amount of debugging I've got to do in my Makefiles to a minimum. For these, and probably many others, make has a few built-in variables. I list a few of the useful ones, along with an explanation below:
Variable Description
$@ This will always expand to the current target.
$< The name of the first prerequisite. This is the first item listed after the colon. (binarytree.o in the first rule above)
$? The names of all the prerequisites that are newer than the target. In the above example this will evaluate to binarytree.o for the first rule.
$^ The names of all the prerequisites, with spaces between them. No duplicates
$+ Like $^, but with duplicates. Items are listed in the order they were specified in the rule.
4.2.6 Pattern Matching in Makefiles

As far as I know, this is a GNU extension, so if you plan to create very portable Makefiles, don't use it. In this case I suggest you use tools such as autoproject, automake, autoconf to build your Makefiles for you (Sorry M$ fans, but the configure scripts created by these tools doesn't work on Windows AFAIK).

As I mentioned above, I would like to get my Makefiles to be as short and compact as possible. Now, considering the variables mentioned above, take a look at:

binarytree.o : binarytree.c
${cc} ${cflags} -c $<

mainprog.o : mainprog.c
${cc} ${cflags} -c $<

Now I see a lot of repetition here. The only parts that differ here are the bases of the filesnames. Luckily, there is a way of compacting these two rules into one -- it's called pattern matching and has saved me quite a bit of time in my short lifetime. To use a pattern, you use % where you would like to actually match anything into it. For example, the above could be written as:

%.o : %.c
${cc} ${cflags} -c $<

Now that looks a lot simpler. In general, I have the following rule in all my Makefiles:

%.o : %.c Makefile
${cc} ${cflags} -c $<

Which is identical to the above, except that it forces a complete recompile each time I modify the Makefile. I do this so that if I change my Makefile, changes will take effect. For example, so I drop the -O3 flag from the cflags variable. Normally this would indicate that you would like the project to be recompiled without optimation

Also, it must be mentioned that it becomes possible to have two rules for the same target. I'm still not sure what make does in this case, but I suspect that both sets of commands gets executed.
4.2.7 Auto Dependencies

This is bordering on really advanced and I'm not always 100 % sure whether this works (according to the info pages it should, but it sometimes seems that the makefile.dep file doesn't get reloaded). Use with caution (even though you would definitely have the same problem without it).

In order to keep things simple, I often have a config.h file lying around somewhere (this is also a convention in the autoproject set of tools). Now if you change it you want all the files that includes it to be recompiled. Oh bugger, now which ones are those, since that other header file also includes it. Now, what if we we're to list every source file with all it's header files in a rule, and then all the header files with all the header files they include etc...

This will lead us back to where we began and it will probably be easiest to compile by hand again.

But as Derick mentioned, gcc has a -M switch, which will output exactly what we need. There are two variants of this switch: -M, which will list all header files included, including system header files, and -MM, which will only list header files included with the #include "..." directive and not those with the #include <...>
directive.

At this point in time, it is probably a good idea to mention that a rule does not need to have commands associated with it. The following is therefor a totally valid rule:

file.c : somefile.h anotherfile.h

As a matter of fact, this is excactly the type of rules that gcc -MM outputs. So we add The following to our Makefile

makefile.dep : *.[Cch]
for i in *.[Cc]; do gcc -MM "$${i}"; done > $@
include makefile.dep

Note the double $, this is because make uses $ as variable expansion, in this case we would like a single $ to be part of the command, and as in C/C++ with \, we simply put a double $ sign. For those not familiar with bash scripting, this command will execute a for loop, not unlike a for loop in programming, but using variable substitutions (as is available in PHP and Perl). The [Cch] is not unlike a ? in wildcard expansion, except that it will only match the characters C, c and h. This is called a glob expression. Strictly speaking * is also a glob expression (man 7 glob). Thus the command

gcc -MM "$i"

will be executed for every .c and .C file, substituting $i for each file. Then we capture the combined output from these commands into the file makefile.dep.

The next line tells make to include makefile.dep as part of the current Makefile. This is where the problem kicks in. Make first loads and parses the entire Makefile, thus including makefile.dep, before actually processing any rules. Now, I suppose you can see that when makefile.dep gets updated, it is already included. According to the info pages, this should cause make to "abort" and restart, causing the updated makefile.dep to be loaded. I sometimes suspect this is not the case, but it is still better than not having this in at all.
4.2.8 Concluding on Makefiles

That should give you a great head start on Makefiles. This is by no means a complete guide, even though it covers practically everything I have ever used. There are other nice tricks like

glfont.o : override cflags+=$(shell freetype-config --cflags)

Which will cause the command freetype-config --cflags to be executed and it's output made part of the cflags variable whilst compiling glfont.o. Also the += specifies that it should be appended to the existing cflags variable instead of merely discarding the existing value as would happen with =. The override keyword also has special meaning in this context.

So where can you find more information on this, and other features of GNU make?

info make

should tell you all you need to know. Enjoy Makefiles -- they truly make your life easier.

to see the original Source, click here

Sunday, October 05, 2008

Burning ISO files on Linux

You think you need to worry about a lot of copyrights and money and installing softwares like windows to burn an ISO file on a CD/DVD. Well if you have Linux you are wrong. Follow the tutorial and in 10 seconds you start burning your ISO file.

How to burn iso files on Ubuntu Linux.

1. Download the file.
2.Right click on the file and click on Write to Disc...
3.In Write To Disc screen you choose your CD Burner and Write speed and when you are done just click on Write
Source

Tuesday, September 23, 2008

C Manual Pages in Ubuntu

To get a manual over C statements in Ubuntu, you have to run these commands in the terminal :

sudo apt-get install build-essential gcc-3.3-doc c-cpp-reference
apt-cache search gcc|grep doc
sudo apt-get install manpages-posix
sudo apt-get install manpages-posix-dev