2015年4月10日 星期五

Hello ninja and GYP

My English is not good to describe what is ninja and GYP, so I just list all steps that building a 'hello world' with these tools on Ubuntu.

1. Install ninja
    sudo apt-get install ninja

2. Install GYP
    sudo apt-get install gyp

3. Create a C file
    vi hello.c

with following content
#include <stdio.h>

int main(void)

{

    printf("Hello, world!\n");

    return 0;

}

4. Create GYP file for generating build.ninja or Makefile
    vi hello.gyp

with following content
{

    'targets': [

    {

        'target_name': 'hello',

            'type': 'executable',

            'sources': [

                'hello.c'

            ],

    },

    ],

}

5. Generate build.ninja file
    gyp hello.gyp --depth=. --generator-output=release -f ninja

    for generate Makefile
    gyp hello.gyp --depth=. --generator-output=release

6. Build with ninja
    ninja -C ./release/out/Default/ all

7. Execute
    ./release/out/Default/hello

8. You'll see output
Hello, world!



Reference:
http://erikge.com/articles/HelloGyp/

2015年3月10日 星期二

Add hard disk to Virtualbox

Add new SCSI hard disk to your setting and start Virtual Machine

Check disk partition
# sudo fdisk -l
Disk /dev/sdb: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders, total 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdb doesn't contain a valid partition table

New hd is mount at /dev/sdb, it need to create new partition by fdisk
# sudo fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x68da59e5.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-33554431, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-33554431, default 33554431):
Using default value 33554431
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Check partition again
# sudo fdisk -l
Disk /dev/sdb: 17.2 GB, 17179869184 bytes
171 heads, 2 sectors/track, 98112 cylinders, total 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x68da59e5
Device Boot Start End Blocks Id System
/dev/sdb1 2048 33554431 16776192 83 Linux

Format disk
# sudo mkfs -t ext4 /dev/sdb1
mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1048576 inodes, 4194048 blocks
209702 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
128 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Mount disk
# sudo mkdir /data
# sudo mount /dev/sdb1 /data

Set auto mount and reboot
# sudo vi /etc/fstab
/dev/sdb1 /data auto defaults,auto,noatime,nodiratime 0 0


2015年2月5日 星期四

Different between const int * , int const * and int * const

* Question: 
What different between const int * , int const * , int * const ?

* Answer: 
  • int*                                                           - pointer to int
  • const int * == int const *                     - pointer to const int
  • int * const - const pointer to int
  • const int * const == int const * const - const pointer to const int

  • int **                       - pointer to pointer to int
  • int ** const             - a const pointer to a pointer to an int
  • int * const *           - a pointer to a const pointer to an int
  • int const **            - a pointer to a pointer to a const int
  • int * const * const - a const pointer to a const pointer to an int


* Reference: 

http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const

2015年2月4日 星期三

Difference between std::thread and pthread

* Question: 

What are the differences between std::thread and pthread


* Answer: 
The std::thread library is implemented on top of pthreads in an environment supporting pthreads 
std::thread provides portability across different platforms like Windows, MacOS, and Linux, but may not be complete on all platforms yet.
The C++11 std::thread class unfortunately doesn't work reliably (yet) on every platform, even if C++11 seems available.

* Reference: 
http://stackoverflow.com/questions/13134186/c11-stdthreads-vs-posix-threads

2015年2月3日 星期二

Difference between size_t and std::size_t


* Question: 

What are the differences between size_t and std::size_t



* Answer: 
C's size_t and C++'s std::size_t are both same.
In C, it's defined in <stddef.h> and in C++, its defined in <cstddef> whose contents are the same 
as C header (see the quotation below). Its defined as unsigned integer type of the result of the 
sizeof operator.

* Reference: 
http://stackoverflow.com/questions/5813700/difference-between-size-t-and-stdsize-t