Archive for cpp

GCC

Environment Variables

 
export C_INCLUDE_PATH=/opt/gdbm-1.8.3/include
export CPLUS_INCLUDE_PATH=/opt/gdbm-1.8.3/include
export LIBRARY_PATH=/opt/gdbm-1.8.3/lib
 

Include Path Search order

* Command line options (-I) from left to right
* Env variables (C_INCLUDE_PATH, CPLUS_INCLUDE_PATH)
* Standard default directories
- /usr/local/include
- /usr/include

Library Path Search order

* Command line options (-L) from left to right
* Env variables (LIBRARY_PATH)
* Standard default directories
- /usr/local/lib
- /usr/lib

Extended Search Path

* Multiple -I or -L entries. For example, -I. -I/opt/gdbm-1.8.3/include -I/net/include -L. -L/opt/gdbm-1.8.3/lib -L/net/lib .....
* Use colon as separator in env variables. For example,
C_INCLUDE_PATH=.:/opt/gdbm-1.8.3/include:/net/include
LIBRARY_PATH=.:/opt/gdbm-1.8.3/lib:/net/lib

Shared Library Path

LD_LIBRARY_PATH=/opt/gdbm-1.8.3/lib:/opt/gtk-1.4/lib
export LD_LIBRARY_PATH

ANSI/ISO Compliance

# Use -ansi
gcc -Wall -ansi pi.c

# Use -pedantic AND -ansi for strict ANSI/ISO compliance
gcc -Wall -ansi -pedantic gnuarray.c

Other Standards

-std=c89 or -std=iso9899:1990
-std=iso9899:199409
-std=c99 or -std=iso9899:1999

Recommended warning options

gcc -ansi -pedantic -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings

C Preprocessor (CPP)

* Expands macros before source files are compiled.
* Preprocess without compiling: -save-temps

 
# Use -E options
gcc -E hello.c
 
# Use -save-temps options
gcc -c -save-temps hello.c
 

Define macros

 
#ifdef TEST
  printf ("Test mode\n");
#endif
 

* Compile with gcc -Wall -DTEST dtest.c -o dtest will print "Test mode".
* Define in source files or header files

#define TEST "Hello, World!"

* Print predefined macros:

cpp -dM /dev/null

* Macro with values

 
printf ("Value of NUM is %d\n", NUM);
gcc -Wall -DNUM=100 dtestval.c
 

Debugging Core Dump

 
# Compile with -g option
gcc -Wall -g null.c -o null
 
# Set core dump file limit
ulimit -c unlimited
 
# Use gdb
gdb null core
(gdb) backtrace
(gdb) break main
(gdb) run
(gdb) step
(gdb) set variable p = malloc(sizeof(int))
(gdb) set variable *p = 255
(gdb) step
(gdb) step
(gdb) finish
(gdb) continue
 

Profiling

* Compile and link with -pg option

 
gcc -Wall -c -pg collatz.c
gcc -Wall -pg collatz.o
 

* Execute

 
./a.out
 

* Profile

 
gprof a.out
 

Compile C++

 
g++ -Wall hello.cc -o hello
 
# Compile for a specific CPU. Not portable!
g++ -Wall -march=pentium4 hello.cc -o hello
 

Debug/Kill a Hanged Program

 
# Compile with -g option
gcc -Wall -g loop.c
 
# Run it
./a.out
 
# Find pid
ps -x
 
# Attach gdb
gdb a.out
(gdb) attach 27189
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb)
 

Create a Library with GNU Archiver

 
# Compile. No link (-c)
gcc -Wall -c hello_fn.c
gcc -Wall -c hello_fn.c
 
# Archive. Create and replace (cr)
ar cr libhello.a hello_fn.o bye_fn.o
 
# List content
ar t libhello.a
 
# Use lib
gcc -Wall hellolib.c libhello.a -o hellolib
# or -L. (include current dir) -lhello (shortcut library link option)
gcc -L. hellolib.c -lhello -o hellolib
 

C++ Standard Library Template

* libstdc++

Providing Own Template

* Follow "Inclusion Compilation Model": place template definition in header files.

How Compiler Works

Overview

* Preprocessing: expands macros

 
cpp hello.c > hello.i
 

* Compilation: source code to assembly code

 
gcc -Wall -S hello.i
 

* Assembly: assembly code to machine code

 
as hello.s -o hello.o
 

* Linking: create final executable

 
ld
  -dynamic-linker \
  /lib/ld-linux.so.2 \
  /usr/lib/crt1.o \
  /usr/lib/crti.o \
  /usr/lib/gcc-lib/i686/3.3.1/crtbegin.o \
  -L/usr/lib/gcc-lib/i686/3.3.1 \
  hello.o \
  -lgcc \
  -lgcc_eh \
  -lc \
  -lgcc \
  -lgcc_eh \
  /usr/lib/gcc-lib/i686/3.3.1/crtend.o \
  /usr/lib/crtn.o
 

Examine Compiled Files

* file

$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped

Examine Symbol Table

* nm
- T: defined
- U: undefined

Finding Dynamically Linked Libraries

* ldd

 ldd hello
        linux-gate.so.1 =>  (0x00362000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00169000)
        libm.so.6 => /lib/libm.so.6 (0x0077c000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00101000)
        libc.so.6 => /lib/libc.so.6 (0x0063a000)
        /lib/ld-linux.so.2 (0x0061d000)

References

* http://www.network-theory.co.uk/gcc/intro/

DIB

Overview

  • Device independent bitmap
    • Can be moved from device to device (hence device independent)
  • Normally transported in
    • metafiles
    • bmp files
    • clipboard (CF_DIB data format)
  • Contains
    • bits
    • header 
      • describes the format of the bits
        • color format (color table)
        • size of the bitmap
  • Supports
    • 1-bit, 4-bit, 8-bit
      • pixels are defined by indexes into color table
    • 24-bit
      • 1 byte each for red, green, and blue
  • DIB functions
    • GetDIBits
    • SetDIBits
    • CreateDIBBitmap
    • SetDIBitsToDevice
    • StretchDIBits
    • CreateDIBPatternBrush

DIB Header

  • Consists two adjoioning parts
    • header
    • color table
  • Combined into the BITMAPINFO structure
    • biSize
    • biWidth
    • biHeight
    • biPlanes
      • Should always be 1
    • biBitCount
      • Color resolution in bits per pixel.
      • Allowed are 1, 4, 8, and 24
    • biCompression
      • Type of compression
      • Can be
        • BI_RGB
        • BI_RLE4
        • BI_RLE8
    • biSizeImage
      • Size of the bitmap proper in bytes
    •  biXPelsPerMeter
    • biYPelsPerMeter
    • biClrUsed
    • biClrImportant 

OS2 Style DIB

  • A DIB file has
    • A file header

      // 14 bytes since each WORD is 2 bytes and DWORD is 4
      typedef struct tagBITMAPFILEHEADER // bmfh
      {
        WORD bfType ; // signature word "BM" or 0x4D42
        DWORD bfSize ; // entire size of file
        WORD bfReserved1 ; // must be zero
        WORD bfReserved2 ; // must be zero
        DWORD bfOffsetBits ; // offset in file of DIB pixel bits
      }
      BITMAPFILEHEADER, * PBITMAPFILEHEADER ;
    • An information header

      typedef struct tagBITMAPCOREHEADER // bmch
      {
        DWORD bcSize ; // size of the structure = 12
        WORD bcWidth ; // width of image in pixels
        WORD bcHeight ; // height of image in pixels
        WORD bcPlanes ; // = 1
        WORD bcBitCount ; // bits per pixel (1, 4, 8, or 24)
      }
      BITMAPCOREHEADER, * PBITMAPCOREHEADER ;// Also defined in WINGDI.H
      typedef struct tagBITMAPCOREINFO // bmci
      {
        BITMAPCOREHEADER bmciHeader ; // core-header structure
        RGBTRIPLE bmciColors[1] ; // color table array
      // Always 2, 16, or 256 RGBTRIPLE structures depending on bits per pixel
      // Allocate PBITMAPCOREINFO for an 8-bit DIB
      // pbmci = malloc (sizeof (BITMAPCOREINFO) + 255 * sizeof (RGBTRIPLE)) ;
      }
      BITMAPCOREINFO, * PBITMAPCOREINFO ;
    • An RGB color table (but not always. not with 24 bit/pixel DIB)
      • An array of 3 byte RGBTRIPLE structures

        typedef struct tagRGBTRIPLE // rgbt
        {
          BYTE rgbtBlue ; // blue level
          BYTE rgbtGreen ; // green level
          BYTE rgbtRed ; // red level
        }
        RGBTRIPLE ;
    • The bitmap pixel bits
  • A in memory DIB has exactly as DIB file minus file header

http://support.microsoft.com/kb/q81498/

Component Object Model (COM)

History

  • VB cusotm controls
    • Dlls that expose properties and events
  • VBX (VB extenstion) (1991) 
    • Based on 16 bit architecture
    • Allow VB to access components written in C and C++
  • OLE1.0 (1991)
    • Deal with compound documents
    • Not popular
  • OLE2.0 (1993)
    • Synonym to OLE
    • Built on COM

Architecture

  • Binary standard to allow coomponents to interact with each other as objects
    • Not a programming language
  • Exposes functionalities via interfaces

OLE and ActiveX

Overview 

  • ActiveX must
    • Communicate with its container using IUnknown COM interface 
    • Be able to create its own registries
  • All OLEs are ActiveX but not the other way around