Showing posts with label yet another insignificant Programming Notes. Show all posts
Showing posts with label yet another insignificant Programming Notes. Show all posts

Thursday, May 23, 2013

Notes on GCC and Make

http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
yet another insignificant Programming Notes
by ehchua
---

1.  GCC (GNU Compiler Collection)

1.1  A Brief History and Introduction to GCC



The original GNU C Compiler (GCC) is developed by Richard Stallman, the founder of the GNU Project.
  1. GNU Compiler Collection (GCC): a compiler suit that supports many languages, such as C/C++, Objective-C and Java.
  2. GNU Make: an automation tool for compiling and building applications.
  3. GNU Binutils: a suit of binary utility tools, including linker and assembler.
  4. GNU Debugger (GDB).
  5. GNU Autotools: A build system including Autoconf, Autoheader, Automake and Libtool.
  6. GNU Bison: a parser generator (similar to lex and yacc).

1.2  Installing GCC

MinGW GCC
MinGW (Minimalist GNU for Windows) is a software port of the GNU Compiler Collection (GCC) and GNU Binutils for use in Windows. It also included MSYS (Minimal System), which is basically a Bourne shell (bash).
Cygwin GCC
Cygwin is a Unix-like environment and command-line interface for Microsoft Windows. Cygwin is huge and includes most of the Unix tools and utilities. It also included the commonly-used Bash shell.

1.3  Getting Started

To compile the hello.c:
> gcc hello.c
  // Compile and link source file hello.c into executable a.exe
The default output executable is called "a.exe".
To run the program:
// Under CMD Shell
> a
// Under Bash or Bourne Shell - include the current path (./)
$ ./a
NOTES (for Bash Shell, Bourne Shell and Unixes):
  • In Bash or Bourne shell, the default PATH does not include the current working directory. Hence, you may need to include the current path (./) in the command. (Windows include the current directory in the PATH automatically; whereas Unixes do not - you need to include the current directory explicitly in the PATH.)
  • In some Unixes, the output file could be "a.out" or simply "a". Furthermore, you may need to assign executable file-mode (x) to the executable file "a.out", via command "chmod a+x filename" (add executable file-mode "+x" to all users "a+x").
To specify the output filename, use -o option:
> gcc -o hello.exe hello.c
  // Compile and link source file hello.c into executable hello.exe
> hello
  // Execute hello.exe under CMD shell
$ ./hello
  // Execute hello.exe under Bash or Bourne shell, specifying the current path (./)
NOTE for Unixes: In Unixes, you may omit the .exe file extension, and simply name the output executable as hello. You need to assign executable file mode via command "chmod a+x hello".
> gcc -o hello hello.c

FOR C PLUS PLUS
> g++ -o hello.exe hello.cpp
   // Compile and link source hello.cpp into executable hello.exe
> hello
   // Execute under CMD shell
$ ./hello
   // Execute under Bash or Bourne shell, specifying the current path (./)

More GCC Compiler Options
A few commonly-used GCC compiler options are:
$ g++ -Wall -g -o Hello.exe Hello.cpp
  • -o: specifies the output executable filename.
  • -Wall: prints "all" warning messages.
  • -g: generates additional symbolic debugging information for use with gdb debugger.
Compile and Link Separately
// Compile-only with -c option
> g++ -c Hello.cpp
// Link object file(s) into an executable
> g++ -o Hello.exe Hello.o
The options are:
  • -c: compile into object file "Hello.o". 
  • -o: Linking is performed when the input file are object files ".o" 
Compile and Link Multiple Source Files
Suppose that your program has two source files: file1.cpp, file2.cpp. You could compile all of them in a single command:
> g++ -o myprog.exe file1.cpp file2.cpp 
However, we usually compile each of the source files separately into object file, and link them together in the later stage. In this case, changes in one file does not require re-compilation of the other files.
> g++ -c file1.cpp
> g++ -c file2.cpp
> g++ -o myprog.exe file1.o file2.o
Compile into a Shared Library
To compile and link C/C++ program into a shared libary (".dll" in Windows, ".so" in Unixes), use -shared option. Read "Java Native Interface" for example.

1.4  GCC Compilation Process

GCC compiles a C/C++ program into executable in 4 steps as shown in the above diagram. For example, a "gcc -o hello.exe hello.c" is carried out as follows:
  1. Preprocessing: via the GNU C Preprocessor (cpp.exe), which includes the headers (#include) and expands the macros (#define).
    > cpp hello.c > hello.i
    The resultant intermediate file "hello.i" contains the expanded source code.
  2. Compilation: The compiler compiles the preprocessed source code into assembly code for a specific processor.
    > gcc -S hello.i
    The -S option specifies to produce assembly code, instead of object code. The resultant assembly file is "hello.s".
  3. Assembly: The assembler (as.exe) converts the assembly code into machine code in the object file "hello.o".
    > as -o hello.o hello.s
  4. Linker: Finally, the linker (ld.exe) links the object code with the library code to produce an executable file "hello.exe".
    > ld -o hello.exe hello.o ...libraries...
Verbose Mode (-v)
You can see the detailed compilation process by enabling -v (verbose) option. For example,
> gcc -v hello.c -o hello.exe
Defining Macro (-D)
You can use the -Dname option to define a macro, or -Dname=value to define a macro with a value. The value should be enclosed in double quotes if it contains spaces.

1.5  Headers (.h), Static Libraries (.lib, .a) and Shared Library (.dll, .so)

Static Library vs. Shared Library
A library is a collection of pre-compiled object files that can be linked into your programs via the linker. Examples are the system functions such asprintf() and sqrt().
There are two types of external libraries: static library and shared library.
  1. A static library has file extension of ".a" (archive file) in Unixes or ".lib" (library) in Windows. When your program is linked against a static library, the machine code of external functions used in your program is copied into the executable. A static library can be created via thearchive program "ar.exe".
  2. A shared library has file extension of ".so" (shared objects) in Unixes or ".dll" (dynamic link library) in Windows. When your program is linked against a shared library, only a small table is created in the executable. Before the executable starts running, the operating system loads the machine code needed for the external functions - a process known as dynamic linking. Dynamic linking makes executable files smaller and saves disk space, because one copy of a library can be shared between multiple programs. Furthermore, most operating systems allows one single copy of a shared library in memory to be used by all running programs, thus, saving memory. The shared library codes can be upgraded without the need to recompile your program.
Because of the advantage of dynamic linking, GCC, by default, links to the shared library if it is available.
You can list the contents of a library via "nm filename".
Compiler and Linker Searching for Header Files and Libraries (-I, -L and -l)
When compiling the program, the compiler needs the header files to compile the source codes; the linker needs the libraries to resolve external references from other object files or libraries. The compiler and linker will not find the headers/libraries unless you set the appropriate options, which is not obvious for first-time user.
For each of the headers used in your source (via #include directives), the compiler searches the so-called include-paths for these headers. The include-paths are specified via -Idir option (or environment variable CPATH). Since the header's filename is known (e.g., iostream.h, stdio.h), the compiler only needs the directories.
The linker searches the so-called library-paths for libraries needed to link the program into an executable. The library-path is specified via -Ldiroption (uppercase 'L' followed by the directory path) (or environment variable LIBRARY_PATH). In addition, you also have to specify the library name. In Unixes, the library libxxx.a is specified via -lxxx option (lowercase letter 'l', without the prefix "lib" and ".a" extension). In Windows, provide the full name such as -lxxx.lib. The linker needs to know both the directories as well as the library names. Hence, two options need to be specified.
Default Include-paths, Library-paths and Libraries
Try list the default include-paths in your system used by the "GNU C Preprocessor" via "cpp -v":
> cpp -v
......
#include "..." search starts here:
#include <...> search starts here:
 d:\mingw\bin\../lib/gcc/mingw32/4.6.2/include             // d:\mingw\lib\gcc\mingw32\4.6.2\include
 d:\mingw\bin\../lib/gcc/mingw32/4.6.2/../../../../include // d:\mingw\include
 d:\mingw\bin\../lib/gcc/mingw32/4.6.2/include-fixed       // d:\mingw\lib\gcc\mingw32\4.6.2\include-fixed
Eclipse CDT: In Eclipse CDT, you can set the include paths, library paths and libraries by right-click on the project ⇒ Properties ⇒ C/C++ General ⇒ Paths and Symbols ⇒ Under tabs "Includes", "Library Paths" and "Libraries". The settings are applicable to the selected project only.

1.6  GCC Environment Variables

GCC uses the following environment variables:
  • PATH: For searching the executables and run-time shared libraries (.dll, .so).
  • CPATH: For searching the include-paths for headers. It is searched after paths specified in -I<dir> options. C_INCLUDE_PATH andCPLUS_INCLUDE_PATH can be used to specify C and C++ headers if the particular language was indicated in preprocessing.
  • LIBRARY_PATH: For searching library-paths for link libraries. It is searched after paths specified in -L<dir> options.

"file" Utility - Determine File Type
> file hello.o
hello.o: 80386 COFF executable not stripped - version 30821
 
> file hello.exe
hello.exe: PE32 executable (console) Intel 80386, for MS Windows
"nm" Utility - List Symbol Table of Object Files
The utility "nm" lists symbol table of object files. 
> nm hello.o
00000000 b .bss
00000000 d .data
00000000 r .eh_frame
00000000 r .rdata
00000000 t .text
         U ___main
00000000 T _main
         U _printf
         U _puts
 
> nm hello.exe | grep printf
00406120 I __imp__printf
0040612c I __imp__vfprintf
00401b28 T _printf
00401b38 T _vfprintf
"nm" is commonly-used to check if a particular function is defined in an object file. A 'T' in the second column indicates a function that is defined, while a 'U' indicates a function which is undefined and should be resolved by the linker.
"ldd" Utility - List Dynamic-Link Libraries
The utility "ldd" examines an executable and displays a list of the shared libraries that it needs. For example,
> ldd hello.exe
ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77bd0000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x77600000)
KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x75fa0000)
msvcrt.dll => /cygdrive/c/Windows/system32/msvcrt.dll (0x763f0000)

2.  GNU Make

The "make" utility automation "make" uses a so-called makefile, which contains rules on how to build the executables.
You can issue "make --help" to list the command-line options; or "man make" to display the man pages.
---

Create the following file named "makefile" (without any file extension), which contains rules to build the executable, and save in the same directory as the source file. Use "tab" to indent the command (NOT spaces).
all: hello.exe

hello.exe: hello.o
  gcc -o hello.exe hello.o

hello.o: hello.c
  gcc -c hello.c
     
clean:
  rm hello.o hello.exe
Run the "make" utility as follows:
> make
gcc -c hello.c
gcc -o hello.exe hello.o
Running make by default starts the target "all" in the makefile. A makefile consists of a set of rules. A rule consists of 3 parts: a target, a list of pre-requsites and a command, as follows:
target: pre-req-1 pre-req-2 ...
 command   <--------------------------inside the makefile
The target and pre-requsites are separated by a colon (:). The command must be preceded by a tab (NOT spaces).
When make is asked to evaluate a rule, it begins by finding the files in the prerequisites. If any of the prerequisites has an associated rule, make attempts to update those first.
In the above example, the rule "all" has a pre-requsite "hello.exe". make cannot find the file "hello.exe", so it looks for a rule to create it. The rule "hello.exe" has a pre-requsite "hello.o". Again, it does not exist, so make looks for a rule to create it. The rule "hello.o" has a pre-requsite "hello.c". make checks that "hello.c" exists and it is newer than the target (which does not exist). It runs the command "gcc -c hello.c". The rule "hello.exe" then run its command "gcc -o hello.exe hello.o". Finally, the rule "all" does nothing.
More importantly, if the pre-requsite is not newer than than target, the command will not be run. In other words, the command will be run only if the target is out-dated compared with its pre-requsites. (increase the efficiency) 
For example, if we re-run the make command:
> make
make: Nothing to be done for `all'.
You can also specify the target to be made in the make command. For example, the target "clean" removes the "hello.o" and "hello.exe". You can then run the make without target, which is the same as "make all".
> make clean  <--------------------------NachOS 
rm hello.o hello.exe
 
> make
gcc -c hello.c
gcc -o hello.exe hello.o
Try modifying the "hello.c" and run make.
NOTES:
  • If the command is not preceded by a tab, you get an error message "makefile:4: *** missing separator. Stop."
  • If there is no makefile in the current directory, you get an error message "make: *** No targets specified and no makefile found. Stop."
  • The makefile can be named "makefile", "Makefile" or "GNUMakefile", without file extension.



done


Software Notes Windows, Office, Firefox, Dreamweaver, others Tips and Tricks


source: 
http://www.ntu.edu.sg/home/ehchua/programming/howto/SoftwareNotes.html
yet another insignificant Programming Notes
by ehchua

1.  Windows

Startup Programs and Services: run "msconfig".

Remove File Associations: Launch Register Editor (regedit), look for key "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts". Find and delete the association.
Windows' Registry: CCleaner 

2.  Word

Selecting Columns of Texts: Hold the "alt" key, you can use mouse to select columns of texts. (ALT+Normal Selection)
Format Painter: One of the most useful feature for formatting your word documents. Suppose that you wish to apply the same format of a paragraph or a character to another part of the document. Select the texts, push "Format Painter" , and paste over the texts to be formatted. (Copy the text mark ups)
Table Formula: You can use formula such as sum for WinWord's table just like excel. Under "Table Tools" ⇒ Layout ⇒ Data ⇒ Fomula. T

5.  Dreamweaver

Creating a Code Snippet:
  1. Show the "Snippets" panel: From "Window" menu ⇒ "Snippets" (or "Snippets" tab on the right panel).
  2. Create a folder for your snippets: right-click on "Name" panel ⇒ New Folder ⇒ MySnippets.
  3. Create your snippet: Let us create a snippet to underline selected texts by wrapping a style <span class="underline"> and </span> around the selected texts. Right-click on "Name" panel ⇒ "New Snippet" ⇒ In "Name" field, enter a name, e.g., underline ⇒ In "Snippet Type", you may choose "Wrap selection" (for applying markup to selected texts) or "Insert block" (for inserting codes/texts). For our example, we select "Wrap selection" ⇒ In "Insert Before", enter <span class="underline> ⇒ In "Insert After", enter </span> ⇒ OK. Move the snippet to your snippet folder MySnippets.
  4. Assign keyboard shortcut to your snippet: Right-click on the snippet name ⇒ "Edit Keyboard Shortcuts" ⇒ In "Commands", choose "Snippet" ⇒ Press the desired Key ⇒ Click "Change" to override the existing keyboard shortcut.
Customizing Keyword Shortcuts: Use "Edit" ⇒ "Keyboard Shortcuts".
Example: To remove "ctrl-3" for "Format, Paragraph Format, Heading 3": "Edit" ⇒ "Keyboard Shortcuts" ⇒ "Menu Commands" ⇒ "Format" ⇒ "Heading 3" ⇒ Select the shortcut key ⇒ Click "-" to remove.
Display Orphaned Files and Broken Files: From "Site" menu ⇒ "Advanced" ⇒ "Recreate Site Cache" ⇒ "Check Links Sitewide" ⇒ In the output, choose "Show Orphaned Files" to display the orphaned files; choose "Show Broken Links" to display broken links.
Creating a Local Site and Synchronize with the Server:
  1. From "Site" menu ⇒ "New Site..." ⇒ Choose the "Advanced" tab.
  2. Fill in the "local Info"
  3. Fill in the "Remote Info" (the server)
  4. You can also use menu "Manage Site..." to configure a Dreamweaver site.
  5. To transfer local site to the server, From "Files" ⇒ "Connect to remote host" ⇒ "Synchronize".
Looking for Tips
I use dreamweaver to write this web site. BUT, I am certainly not a dreamweaver power user. I am still looking for answers for these questions. Let me know if you have a solution.
  • How to do on-the-fly spell and grammar check, with errors highlighted while typing, just like Word?
  • How to compress and optimal images for web display to save the bandwidth, just like Powerpoint? I am also looking for a lightweight image editor that can do the simple and basic image operations such as cropping. Any suggestion?
  • What is the easier way to replace '>', '<', '"' and '&', with their escape sequences?

Notes on Generic Types in Java

source: http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGeneric.html
yet another insignificant Programming Notes
by ehchua


In generics, instead of pass arguments, we pass type information (inside the angle brackets <>).
The primary usage of generics is to abstract over types when working with collections (Read "The Collection Framework" if necessary).


For example, the class ArrayList is designed (by the class designer) to take a generics type <E> as follows:
public class ArrayList<E> implements List<E> .... {
   // Constructor
   public ArraList() { ...... }

   // Public methods
   public boolean add(E e) { ...... }
   public void add(int index, E element) { ...... }
   public boolean addAll(int index, Collection<? extends E> c)
   public abstract E get(int index) { ...... }
   public E remove(int index)
   .......
}

downcast problem - not type-safe
.Suppose, for example, you wish to define an ArrayList of String. In theadd(Object) operation, the String will be upcasted implicitly into Object by the compiler. During retrieval, however, it is the programmer's responsibility to downcast the Object back to an String explicitly. If you inadvertently added in a non-String object. the compiler cannot detect the error, but the downcasting will fail at runtime 

2.1  Generics Classes

JDK 1.5 introduces the so-called generics to resolve this problem. Generics allow you to abstract over types. You can design a class with a generic type, and provide the specific type information during the instantiation. The compiler is able to perform the necessary type checking during compile time and ensure that no type-casting error occurs at runtime. This is known as type-safety.

(avoid using Object and Downcasting)
Take a look at the declaration of interface java.util.List<E>:
public interface List<E> extends Collection<E> {
   boolean add(E o);
   void add(int index, E element);
   boolean addAll(Collection<? extends E> c);
   boolean containsAll(Collection<?> c);
   ......
}
<E> is called the formal "type" parameter, which can be used for passing "type" parameters during the actual instantiation.

The mechanism is similar to passing arguments to parameters
Formal Type Parameter Naming Convection (just Naming Convention) 
Use an uppercase single-character for formal type parameter. For example,
  • <E> for an element of a collection;
  • <T> for type;
  • <K, V> for key and value.
  • <N> for number
  • S,U,V, etc. for 2nd, 3rd, 4th type parameters
Example of Generic Class
In this example, a class called GenericBox, which takes a generic type parameter E, holds a conetent of type E. The constructor, getter and setter work on the parameterized type E. The toString() reveals the actual type of the content.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class GenericBox<E> {
   // Private variable
   private E content;
 
   // Constructor
   public GenericBox(E content) {
      this.content = content;
   }
 
   public E getContent() {
      return content;
   }
 
   public void setContent(E content) {
      this.content = content;
   }
 
   public String toString() {
      return content + " (" + content.getClass() + ")";
   }
}


In fact, the compiler replaces (compilers job) all reference to parameterized type E with Object, performs the type check, and insert the required downcast operators. For example, the GenericBox is compiled as follows (which is compatible with codes without generics): 
(Object + Downcasting + Checking are done by compiler)
Continue with our "type-safe" ArrayList...
Let's return to the MyArrayList example. With the use of generics, we can rewrite our program as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// A dynamically allocated array with generics
public class MyGenericArrayList<E> {
   private int size;     // number of elements
   private Object[] elements;
   
   public MyGenericArrayList() {  // constructor
      elements = new Object[10];  // allocate initial capacity of 10
      size = 0;
   }
   
   public void add(E e) {
      if (size < elements.length) {
         elements[size] = e;
      } else {
         // allocate a larger array and add the element, omitted
      }
      ++size;
   }
   
   public E get(int index) {
      if (index >= size)
         throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
      return (E)elements[index];
   }
   
   public int size() { return size; }
}


Behind the scene, generics are implemented by the Java compiler as a front-end conversion called erasure, which translates or rewrites code that uses generics into non-generic code (to ensure backward compatibility). This conversion erases all generic type information.

2.2  Generic Methods

Methods can be defined with generic types as well (similar to generic class). For example,
public static <E> void ArrayToArrayList(E[] a, ArrayList<E> lst) {
   for (E e : a) lst.add(e);
}
A generic method can declare formal type parameters (e.g. <E>, <K,V>) preceding the return type. The formal type parameters can then be used asplaceholders for return type, method's parameters and local variables within a generic method, for proper type-checking by compiler.

out: lst

2.3  Wildcards

Consider the following lines of codes:
ArrayList<Object> lst = new ArrayList<String>();
It causes a compilation error "incompatible types", as ArrayList<String> is not an ArrayList<Object>.
This error is against our intuition on polymorphism, as we often assign a subclass instance to a superclass reference.
Consider these two statements:
List<String> strLst = new ArrayList<String>();   // 1
List<Object> objLst = strList;                   // 2 - Compilation Error
Line 2 generates a compilation error. But if line 2 succeeds and some arbitrary objects are added into objLst, strLst will get "corrupted" and no longer contains only Strings. (objLst and strLst have the same reference.)
Because of the above, suppose we want to write a method called printList(List<.>) to print the elements of a List. If we define the method asprintList(List<Object> lst), then it can only accept an argument of List<object>, but not List<String> or List<Integer>. For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
public class TestGenericWildcard {
   
   public static void printList(List<Object> lst) {  // accept List of Objects only, 
                                                     // not List of subclasses of object
      for (Object o : lst) System.out.println(o);
   }
   
   public static void main(String[] args) {
      List<Object> objLst = new ArrayList<Object>();
      objLst.add(new Integer(55));
      printList(objLst);   // matches
   
      List<String> strLst = new ArrayList<String>();
      strLst.add("one");
      printList(strLst);  // compilation error
   }
}


Unbounded Wildcard <?>
To resolve this problem, a wildcard (?) is provided in generics, which stands for any unknown type. For example, we can rewrite our printList() as follows to accept a List of any unknown type.
public static void printList(List<?> lst) {
  for (Object o : lst) System.out.println(o);
}
Upperbound Wildcard <? extends type>
The wildcard <? extends type> stands for type and its sub-type. For example,
public static void printList(List<? extends Number> lst) {
  for (Object o : lst) System.out.println(o);
}
List<? extends Number> accepts List of Number and any subtype of Number, e.g., List<Integer> and List<Double>.
Clearly, <?> can be interpreted as <? extends Object>, which is applicable to all Java classes.
Another example,
// List<Number> lst = new ArrayList<Integer>();  // Compilation Error
List<? extends Number> lst = new ArrayList<Integer>();
Lowerbound Wildcard <? super type>
The wildcard <? super type> matches type, as well as its super-type. In other words, it specifies the lower bound.


2.4  Bounded Generics (Generics with Upperbound) 

A bounded parameter type is a generic type that specifies a bound for the generic, in the form of <T extends ClassUpperBound>, e.g., <T extends Number> accepts Number and its subclasses (such as Integer and Double).
Example
The method add() takes a type parameter <T extends Number>, which accepts Number and its subclasses (such as Integer and Double).
1
2
3
4
5
6
7
8
9
10
11
public class MyMath {
   public static <T extends Number> double add(T first, T second) {
      return first.doubleValue() + second.doubleValue();
   }
 
   public static void main(String[] args) {
      System.out.println(add(55, 66));     // int -> Integer
      System.out.println(add(5.5f, 6.6f)); // float -> Float
      System.out.println(add(5.5, 6.6));   // double -> Double
   }
}


END