Tuesday 26 June 2012

CMake

CMake is a sophisticated, cross-platform, open-source build system developed by Kitware in 2001. CMake is the name used to refer to the entire family of tools: CMake, CTest, CDash, and CPack. These tools are used to…
  • CMake – An intelligent build system used to build simple, to very elaborate projects.
  • CTest, CDash – These are used to test and debug your projects.
  • CPack – A packaging tool that finalizes your project for distribution.
 To:
http://mathnathan.com/2010/07/11/getting-started-with-cmake/

CMake simplifies the potentially monstrous build system into a few easy to write files. It is targeted towards C and C++ and is usable with various compiler/OS support. At the risk of over simplifying things, CMake looks at your project and sees a ‘file system’. You tell it where all your files are at, what you want it to do with them, and it does it. In a nut shell it’s really that easy. More elaborately however, CMake is wonderful for it’s design to support complex directory hierarchies. When you use CMake it will locate include files, libraries, executables, other optional build directives, then link everything for you. No more messy Makefiles.
CMake basically requires one thing, a CMakeLists.txt file. We’ll start with a simple example.
Here is a very simple program I wrote in C called fib.c which lists the Fibonacci Sequence up to a given value. That value is passed to the main function when it is called.
# include "stdio.h"
# include "stdlib.h"

int main( int argc, char** argv) {
 if ( argc > 2 || argc == 1 ) {
  printf("Fibonacci takes one postitive integer greater\
                         than two as it's argument\n");
  return EXIT_SUCCESS;
 }
 int a, b;
 a = 0;
 b = 1;
 printf( "%d", b );
 for( int i = 0; i + a <= atof( argv[1] ); b = i ) {
  i = a + b;
  a = b;
  printf( ", %d", i );
 }
 printf("\n");
 return EXIT_SUCCESS;
}
So let's say you'd like to compile this and give it a go. We could just compile it directly with one line...
        gcc -std=c99 fib.c
Notice we need a -std flag. We set it to c99 because loop initial declarations are only allowed in c99 mode. Alternatively you could just change that in your gcc compiler to use by default. Another option would be to use a Makefile..... Nah... Or we could use CMake. I declare the winner to be CMake.

Download

Let's make sure we have CMake on our systems, I'm using Ubuntu 10.04.
      sudo apt-get install cmake cmake-curses-gui
The cmake-curses-gui provides a somewhat-helpful-gui (a lot like the Windows version) if that's what you're into. There is pretty much no setup required here, we just need to make our CMakeLists.txt file in the main directory of our project, then run CMake from our build folder.

Setup Project

fib is the name of the folder/main directory, where the fib.c file is, as well as the soon to be disclosed CMakeLists.txt file. It's a good programming practice to do all of your builds in a build folder. Not doing so clutters your project folder with a bunch of confusing files. You'll see this after we run cmake. So let's set this up.
       mkdir /where/you/want/your/project/fib
        cd fib
        mkdir build
So this is what your project file system looks like.
        fib
       /
    build
Now use your favorite text editor (should be vim ;-) ) to create your own fib.c file, as well as a CMakeLists.txt file in your fib directory.

CMake Syntax

Here is the CMakeLists.txt file used to compile the fib.c program.
#Specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.6 C)
#Name your project here
project(fibonacci)

#Sends the -std=c99 flag to the gcc compiler
add_definitions(-std=c99)
#This tells CMake to fib.c and name it fibonacci
add_executable(fibonacci fib.c)
The basic syntax of CMake consists of three things; comments, commands, and white spaces. A comment is made by beginning a line with the '#' character
       #Chocolate Curry is my favorite
And a command is constructed with the command name, opening parenthesis, whitespace separated arguments, and a closing parenthesis.
        command(arg1 agr2 agr3 ...)
The basic data type in CMake is a string. You can even create lists of strings using the set command.
        set(VAR a b c)
This creates variable VAR which is composed of three strings a, b, and c. Referencing a variable should look familiar to most of you. ${VAR} is the correct syntax to refer to the list of strings VAR at a later time. For instance these three command calls are the same.
        command(${VAR})
        command(a b c)
        command(a;b;c)
You can also separate strings with semicolons. Placing quotations around anything will allow it to be read as a string. For instance, the following command
        command("${VAR}")
will read in "${VAR}" not "a b c". CMake syntax also provides some useful flow control options. First is the if else logic structure.
        # some_command will be called if the variable's value is not:
        # empty, 0, N, NO, OFF, FALSE, NOTFOUND, or -NOTFOUND.
        if(var)
           some_command(...)
        endif(var) 
They also provide looping commands, foreach and while.
        set(VAR a b c)
        # loop over a, b,c with the variable f
        foreach(f ${VAR})
            message(${f})
        endforeach(f) 
Simple function and macro constructions are also supported.
        # define a macro hello
        macro(hello MESSAGE)
            message(${MESSAGE})
        endmacro(hello)
        # call the macro with the string "hello world"
        hello("hello world")
        # define a function hello
        function(hello MESSAGE)
            message(${MESSAGE})
        endfunction(hello)
Lastly CMake also supports all of your common Regular Expressions
  • ^ Matches at beginning of a line or string
  • $ Matches at end of a line or string
  • . Matches any single character other than a newline
  • [ ] Matches any character(s) inside the brackets
  • [^ ] Matches any character(s) not inside the brackets
  • [-] Matches any character in range on either side of a dash
  • * Matches preceding pattern zero or more times
  • + Matches preceding pattern one or more times
  • ? Matches preceding pattern zero or once only
  • () Saves a matched expression and uses it in a later replacement
For a more detailed description of CMake syntax go here.

Building The Project

So back to the CMakeLists.txt file to build our example
#Specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.6 C)
#Name your project here
project(fibonacci)

#Sends the -std=c99 flag to the gcc compiler
add_definitions(-std=c99)
#This tells CMake to fib.c and name it fibonacci
add_executable(fibonacci fib.c)
CMake is case insensitive, thus it does not distinguish between command() and COMMAND(). I prefer all lowercase because it is faster to type. so here is the breakdown of the CMakeLists.txt
  • cmake_minimum_required(VERSION 2.6 C) - This signifies the version of CMake that you're using and allows you to pass the language into documentation.
  • project(fibonacci) - This allows you to name your project and you can later on refer to the project as a whole using this string.
  • add_definitions(-std=c99) - This command sends arguments to the compiler, which CMake will choose for you unless otherwise told so.
  • add_executable(fibonacci fib.c) - This does the magic. Here CMake takes fib.c, and using all of the libraries it was handed (none in this simple example) compiles it into the output file named fibonacci.
Beautiful, we're almost done. Now we want to compile things. Once run for the first time CMake will configure itself to your system, and place the binaries (your output files) into the directory you called it from. This is where we cd into the build folder, and run
        cmake ..
The ' .. ' argument is the same as the one used in BASH, it tells CMake to execute from the CMakeLists.txt in the previous directory. This call just produced your Makefile, so let's finish it off with the last command.
        make
Now if you ls into the build folder you'll see all kinds of stuff.
  • CMakeCache.txt - Think of this as a configuration file. It has many advanced settings to optimize the build process for large projects. (Such as building the Linux Kernel! ;-) )
  • CMakeFiles (directory) - This has a lot of the stuff CMake uses under the hood to build your project. I recommend you poke around through there and take a look at stuff...
  • cmake_install.cmake - Your install file. We didn't use one in this program.
  • fibonacci - Hooray it's our program!
  • Makefile - remember your last command 'make'? This is the Makefile produced by Cmake, this is really what it all came down to.

Monday 25 June 2012

Make

1. 组成部分:
注释,依赖,命令,(循环依赖),宏定义,宏定义外置;
2. Example:
http://www.opussoftware.com/tutorial/TutMakefile.htm
# This makefile compiles the project listed in the PROJ macro
#
PROJ = project   # the name of the project
OBJS = main.obj io.obj  # list of object files 
# Configuration:
#
MODEL = s   # memory model
CC = bcc   # name of compiler 
# Compiler-dependent section
#
%if $(CC) == bcc  # if compiler is bcc
  CFLAGS = –m$(MODEL)  # $(CFLAGS) is –ms
  LDSTART = c0$(MODEL)  # the start-up object file
  LDLIBS = c$(MODEL)  # the library
  LDFLAGS = /Lf:\bc\lib  # f:\bc\lib is library directory
%elif $(CC) == cl  # else if compiler is cl
  CFLAGS = –A$(MODEL,UC) # $(CFLAGS) is –AS
  LDSTART =   # no special start-up
  LDLIBS =   # no special library
  LDFLAGS = /Lf:\c6\lib; # f:\c6\lib is library directory
%else    # else
% abort Unsupported CC==$(CC) # compiler is not supported
%endif    # endif 
# The project to be built
#
$(PROJ).exe : $(OBJS)
 tlink $(LDSTART) $(OBJS), $(.TARGET),, $(LDLIBS) $(LDFLAGS) 
$(OBJS) : incl.h 

Thursday 21 June 2012

Show the List of Installed Packages on Ubuntu or Debian


Show the List of Installed Packages on Ubuntu or Debian

While working on the instructions for compiling MonoDevelop from source, I relied heavily on the dpkg and apt-cache commands to tell me what was already installed vs what packages were available in the repository. After completing that article it occurred to me that I should explain how to show what packages are currently installed… so here we are.
The command we need to use is dpkg –get-selections, which will give us a list of all the currently installed packages.
$ dpkg --get-selections
adduser                                         install
alsa-base                                       install
alsa-utils                                      install
apache2                                         install
apache2-mpm-prefork                             install
apache2-utils                                   install
apache2.2-common                                install
apt                                             install
apt-utils                                       install
The full list can be long and unwieldy, so it’s much easier to filter through grep to get results for the exact package you need. For instance, I wanted to see which php packages I had already installed through apt-get:
dpkg --get-selections | grep php
libapache2-mod-php5                             install
php-db                                          install
php-pear                                        install
php-sqlite3                                     install
php5                                            install
php5-cli                                        install
php5-common                                     install
php5-gd                                         install
php5-memcache                                   install
php5-mysql                                      install
php5-sqlite                                     install
php5-sqlite3                                    install
php5-xsl                                        install
For extra credit, you can find the locations of the files within a package from the list by using the dpkg -L command, such as:
dpkg -L php5-gd
/.
/usr
/usr/lib
/usr/lib/php5
/usr/lib/php5/20060613
/usr/lib/php5/20060613/gd.so
/usr/share
/usr/share/doc
/etc
/etc/php5
/etc/php5/conf.d
/etc/php5/conf.d/gd.ini
/usr/share/doc/php5-gd
Now I can take a look at the gd.ini file and change some settings around…

Wednesday 20 June 2012

How to read code

1. Target: In order to read Code DynaMIT.
2. Tools: Source Insight
3. Understand: Wiki
4. Target Today: Understand the work flow and answer questions related with performance

Sunday 17 June 2012

Design and Implementation of Large-Scale Distributed Agent-based Transportation Simulation

0. Introduction
1. Agent-based Structure
2. Road Network Partitioning
3. Boundary Processing
4. Communication and Synchronization
    Rules:
    (1) Based on MPI&Cluster
    (2) Group Discussion > one-by-one
    (3) Proxy and Buffer
    (4) Off-line Storage & Communication
5. Experiments and Analysis
6. Conclusion

Thursday 14 June 2012

关于男士护肤

今天发现自己的皮肤很不好;
身体的皮肤开始长一些小红豆,
面部的痘痘逐渐退去,留下了一些小坑;
有可能已经无法根除,只能让事情不要变的更坏!
(1)皮肤分层:表皮&真皮
          表皮抵抗,真皮再生;
(2)油性皮肤
(3)对付小坑
在临床上,有经验的专业医师通常会外用维甲酸、曲酸、水杨酸等制剂来淡化色素沉着,如果是比较浅的小坑,几个月后就基本上能恢复正常肤色。但对于明显的小坑,就必须到皮肤外科或美容外科进行皮肤磨削术了。这种手术主要是对表皮和真皮乳头浅层进行削磨,使深而明显的小坑基本变平,达到治疗目的。另外,皮肤磨削术还可用于水痘等引起的瘢痕。

Saturday 2 June 2012

男士服装学习

第一个:佐丹奴(GIORDANO
休闲,中等价格
比较喜欢,裤子休闲,价格在20-50新币左右
第二个:G2000 (U2)
都市生活,活力干练
中等价格

第三个:ESPRIT (埃斯普利特)
心态而非年龄,追求品位,中高档价位
款式设计风格并非适合我;

第四个:JACK&JONES 杰克琼斯
流行度,中等价位
很好,可以看看新加坡有没有;

另外,需要认识一些奢侈品品牌,知道自己的位置:
Hugo Boss,GianFranco Ferre, W&LT (无性别主义),GUCCI (奢侈品), Gabbana,  

comme des garcons (日本品牌),PRADA (意大利时尚), Armani, Christian Dior (古典).