Author Archives: xibaiyu

Make Mac Terminal Colorful

If you just want the “ls” result colorful to tell different file type, just add “alias ls=’ls -G'” to your .bashrc file  and then execute ” source ~/.bashrc”

I post this because I found so many long articles when googling mac terminal colorful,  but I just want to make ls and grep results are easy to recognize.

Set vim with python support

Python is perhaps the most the productive programming   language for widely use ( Happy Chinese Snake Year).  Now the vim plugins could be written with python.

For version or platform reason, sometimes the vim could not get the right version for  python lib.

I got a importError for python when run vim with a plugin as:

File “<string>”, line 2, in <module>
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”, line 94, in <module>
import httplib
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”, line 79, in <module>
import mimetools
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py”, line 6, in <module>
import tempfile
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py”, line 34, in <module>
from random import Random as _Random
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py”, line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom

This could happen on my iMac.  For macvim,  to solve this is just (assume there is hombrew installed):

brew unlink python

brew unlink macvim

brew remove macvim

brew install -v –force macvim

brew link macvim

brew link python

For the vim, the funny thing is : the vim coming with mac did not support python well ( /etc/bin/vim), if you install vim using brew( /etc/local/bin/vim),  then this one could work with python.  My solution is just modify the PATH, by edit the file /etc/paths.   Place /etc/local/bin  ahead of /etc/bin/,  Then when running vim, the available one could work with python very well.

 

Hello World From Vim

Hello word! from Vim

Let me say Hello world! from vim.

I think I have to summarieze my vim configure experience. Let me start from vimRepress

I use gvim7.3 under windows

  • Downloand VimRepress from http://www.vim.org/scripts/script.php?script_id=3510
  • Configure .vimrc file by adding:
    let VIMPRESS =[{'username':'your name',  
                    \'password':'your password',
                    \'blog_url':'http://your.blog.com'},
                    ... "you can add more blogs by repeat the above format      '
                    }]
    

I did not creat file .vimpressrc it still work well

BTW, it is cool to write blog from vim, but does it look like another emacs? It will definitely be another emacs when vim is also able to cook coffee. Then there will be a new war between two “quasi-operating-systerm”.

Tagged ,

compiler return value optimization(RVO)

class A

{

     A(){cout <<” in constructor”<<endl;};

     A(A&a){cout<<”in the copy constructor”<<endl;};

};

int main()

{

   A a = A();

   return 0;

}

From all the textbook about C++, this program should generate output as follow:

in constructor

in the copy constructor

but both G++ and MS Vs200* will generate only”in constructor”.

The reason lies in the “Return Value Optimization”(RVO).

Use “-fno-elide-constructors” opinion for g++, then the desired output will be shown. Similarly configure the MS VS 200*, then desired output will be there.

(C++ tips) Somethings about new malloc and continuous memory

Recently, there is a chance for me to  go back through the topic about stack and heap for C++ language.

I think I need keep this experience.

My story is this:  some one declared a large array under Linux. No error during compiling  but the classic  “segment fault” appears once the programing is running.

This often happens under Linux with gcc/g++, not so often on MS Windows. I think it depends on the different memory management strategies.

I try to declare large array, and apply for large memory by new and malloc, then found something interesting:

for my computer environment, the following three programs end up “segment fault”:

declare large array, I try from array[1*] , here ’*’ will be replaced by some number of zeros. This method keeps well until the zeros number comes 8, like the following code:

//main.c

#include <stdio.h>

int main()

{

     double array[100000000]// it is 8 zeros,

     return 0;

}

using malloc function (the idea is similar with above ), the malloc fails at 10 zeros:

//main.c

#include <stdio.h>

#include <stdlib.h>

int main()

{

   double * ptr = (double *) malloc( 10000000000*sizeof(double));

     return 0;

}

finally, the new method, it fails on the same  condition as the malloc condition:

//main.cpp

#include<stdio.h>

int main()

{

    double * ptr = new double[10000000000];

    return 0;

}

Now let’s make some conclusion about new, malloc, stack and heap.

Array cannot apply for the memory large as malloc/new, because array declaring creates variable on stack , and new and malloc apply memory from heap. Then array cannot get memory as large as malloc/new did.

In addition: stack address is always continuous, but heap may not. (If confused , come back the Operation System textbook to go through the memory management chapter.) It is the continuous requirement that makes the stack variable, i.e. array could not apply space as large as malloc/new.

Another thing I want to say is that because new will call the constructor, while malloc dose not. Then for some condition or some system, the performance of new and malloc  may be different from each other.

(Just for Fun)

Eclipse(Indigo) with CDT no output on Console

Once finishing the configuration Eclipse and CDT, an “hello world” program is common, but sometimes there are no output values on console.

1) try fflush(stdout) after printf().

2) If there is still nothing on console of eclipse. Then perhaps the eclipse could not find the MinGW(in my case), then we set the path manually :

Run –> Run Configurations –> C/C++ Application  / PROJECT DEBUG(your project debug ),

edit the “environment” tab, add a new one named” PATH”, value is the $MINGW/bin

then re-run the program.

Code Block tips: auto exit console window

when using Code::Block, sometimes the we need console program auto-exit the console window without asking for an"Enter" key.

Set the console as:

Project -> Properties -> Build targets

uncheck the choice "Pause when execution"

Then the program could will never trouble you by asking for the "Enter" key.

for Code::Blocks 10.05,

Just for Fun , just as a backup.

PS, the more IDE I used, the more I like my vim with its powerful plugins.

Install OpenCV 2.31 for visual studio 2008

The OpenCV latest version is  2.31 for now (Sep 13, 2011).  There are many new modifications for this improvement. 

 

Also there are some problems for running this version.

Now, the official website of OpenCV does not contain manual for OpenCV 2.31 and VS2008. (At current time Sep 13 2011).

 

Download the latest version of OpenCV 2.31 from http://opencv.willowgarage.com/wiki/.

Extract all the file using the file “OpenCV-2.3.1-win-superpack.exe”

We need the CMake to configure and VS to compile the source files.

Let’s use “$opencv_home” to stand for the directory where the OpenCV files extracted.

Then we could employ Cmake to specify the directory where the binary file are generated, in my case, it is “$opencv_home/build”.

We could generate debug and release version respectively:

$opencv_home/build/install/debug/

$opencv_home/build/install/release/

Under debug/release directory, there will be three folders:

include/,  bin/, lib/,

“include” and “lib” file could be added into the VS2008 project following:

Tools –> Options –>Projects and Solutions –> VC ++ Directories

*.lib file should be added following:

Project –> Properties –> Linker –> input –> Additional Dependencies

The “bin” directory contains the dll files, and we should make sure the running program could localize necessary DLL. There are many methods:

  1. copy these dll files into $system or $windows/systems directory
  2. add the dll directory path to the system environment variable “$PATH”
  3. copy all these dll files into the current project executing path.

 

 

 

 

To make the content auto resize

<html>
<head>
<style type="text/css">
body{
background:#999;
text-align:center;
color: #333;
font-family:arial,verdana,sans-serif;
}
#header{
width:776px;
margin-right: auto;
margin-left: auto;
padding: 0px;
background: #EEE;
height:60px;
text-align:left;
} #contain{
margin-right: auto;
margin-left: auto;
width: 776px;
} #mainbg{
width:776px;
padding: 0px;
background: #60A179;
float: left;
}
#right{
float: right;
margin: 2px 0px 2px 0px;
padding:0px;
width: 574px;
background: #ccd2de;
text-align:left;
}
#left{
float: left;
margin: 2px 2px 0px 0px;
padding: 0px;
background: #F2F3F7;
width: 200px;
text-align:left;
}
#footer{
clear:both;
width:776px;
margin-right: auto;
margin-left: auto;
padding: 0px;
background: #EEE;
height:60px;}
.text{margin:0px;padding:20px;}
</style>
</head>
<body>
<div id="header">header</div>
<div id="contain">
<div id="mainbg">
<div id="right">
<div
class="text">right<p>1</p><p>1</p><p>1</p><p>1</p><p>1</p></div>
</div>
<div id="left">
<div class="text">left</div>
</div>
</div>
</div>
<div id="footer">footer</div>
</body>
</html>

This is a sample for resize the html content using CSS.

The Main effort lies in two:

  1. <body> “text-align:center;”, could ensure the resizing in IE,
  2. For others like Firefox, we should add

margin-right: auto;
margin-left: auto;

in the <header> to guarantee the result.

Tips:  I never pay any attention on the Html or CSS, thinking them “too simple, sometimes naive”, but recently found them interesting.

Install OpenCV in Ubuntu( for the case missing files)

Well, there are already many articles for installing OpenCV 2.* in Ubuntu, I think I cannot write this better than them, for example, http://www.samontab.com/web/2010/04/installing-opencv-2-1-in-ubuntu/ 

But during “ make” there is always mistakes, saying miss some file,  there is a solution for this. This occurs about 80%.

there are two files to modify:

$opencv_path/modules/highgui/src/cap.cpp

about 174 lines, 

replace the

#if define (HAVE_CAMV4L) || defined (HAVE_CAMV4L2)

with

#if define (HAVE_CAMV4L)

modify the file $opencv_path/moddules/highgui/src/cap_v4l.cpp

about 217 lines

replace the line

#include <linux/videodev.h>

by

#ifdef HAVE_CAMV4L

#include<linux/videodev.h>

#endif

after these modification, we could “ make “ successfully.  then make install,  and using the opencv