Search

7/31/2006

GCC重要的選項或旗標(FLAGS)

GCC重要的選項或旗標(FLAGS)

一些常用到的編譯旗標,如下
-c: 只建立obj檔,留待後面才來連結(link)
-S: 只建立assemblely檔。
-g: 建立一些除錯資訊給可執行檔,這樣debug工具ddd,gdb才能除錯。
-o: 把建立的二位元檔給另外名字,因為可執行檔最後內定名字是a.out。
-D: 條件編譯,搭配#ifdef #define用。如果有defined才編譯
-W: 編譯時出錯時,顯示錯誤訊息的條件。
-L: 給連結時要用到的函式庫的搜尋目錄。
-I: 標頭檔.h的搜尋目錄。
-l: 正常連結只會在libc這個函數庫,其他函數庫需要用這個指定連結。
-O1 -O2 -O3: 最佳化,會根據CPU的架構編出好的程式碼,需要多一點編譯時間。
-O2 是不錯的選擇。

像這樣的例子
$ gcc -c -o test.o test.c
$ gcc -S -o test.s test.c
$ gcc -g -o test.o test.c
$ gcc -D_SOLARIS_ -o test test.c
$ gcc -Wall -L./lib/ -I./include/ -o foo.o foo.c
$ gcc -Wall -L../lib -I../include -lX11 -lXext -lm -o xprog xprog.c

W用all表示所有的編譯warning都要秀出來。

Static, Shared, Dynamic 函式庫撰寫

Static,Shared, Dynamic 函式庫撰寫
Program Library HOWTO
三個檔案分別為: libhello.c libhello.h demo_use.c
最簡單的作法就是 gcc demo_use.c libhello.c -o result
不過這邊講的是要做成library, 所以以下

[static library]
GCC 的Static Library 副檔名通常為.a。用ar將很多object file 放進一個archive就是static library了。
用法: ar rcs libutil.a util_file.o util_net.o util_math.o
把util_file.o util_net.o util_math.o 產生一個static library叫作libutil.a

step1: 把.c檔都先弄成.o(object)的二進位檔
gcc -c -o libhello.o libhello.c //產生一個libhello.o
gcc -c -o demo_use.o demo_use.c //產生一個demo_use.o

step2:將obj檔弄成static libary的.a檔
ar rcs libhello.a libhello.o //產生一個libhello.a

step3: demo_use.c就可以呼叫static library裏面的函式了, 下面兩種寫法都可
gcc -o result demo_use.o -L. -lhello
gcc -o result demo_use.c libhello.a
* -L.意思是搜尋在目前目錄(.)的library
* Note that we omitted the "lib" prefix and the ".a" suffix when mentioning the library on the link command.

[shared library]

----libhello.c-------
#include
hello(){
printf("hello world\n");
}
---------------------
----main.c-----------
#include
printf("Inside main\n");
hello();
return 0;
}
---------------------

step1: gcc -fPIC -c libhello.c
The -fPIC option is to tell the compiler to create Position Independent Code (create libraries using relative addresses rather than absolute addresses because these libraries can be loaded multiple times).

step2: gcc -shared -o libmylib.so libhello.o
將數個.o檔做成一個.so檔
The -shared option is to specify that an architecture-dependent shared library is being created.

step3: gcc -c -o main.o main.c

step4:
gcc -o result -L. -lhello main.o

執行:
LD_LIBRARY_PATH="." ./result

[dynamic loading]
#include <dlfcn.h> /* defines dlopen(), etc.       */
#include
<stdio.h>
 
main(){
void *libc; /* handle of the opened library */
void (*printf_call)();
/* define a function pointer variable to hold the function's address */

if(libc=dlopen("/lib/libc.so.5",RTLD_LAZY)){
/*use the lazy approach(RTLD_LAZY) of checking only when used */
printf_call=dlsym(libc,"printf");
/*Calling Functions Dynamically Using dlsym()*/
(*printf_call)("hello, world\n");
}
}
"When we discussed static libraries we said that the linker will try to look for a file named 'libutil.a'. We lied. Before looking for such a file, it will look for a file named 'libutil.so' - as a shared library. Only if it cannot find a shared library, will it look for 'libutil.a' as a static library. Thus, if we have created two copies of the library, one static and one shared, the shared will be preferred. This can be overridden using some linker flags ('-Wl,static' with some linkers, '-Bstatic' with other types of linkers."

參考資料
Creating Libraries
GCC產生Static Library
Building And Using Static And Shared "C" Libraries
用Open Source工具開發軟體

7/28/2006

Stiff asks, great programmers answer

Stiff asks, great programmers answer
電腦相關書籍
Linus Torvalds: D&D "The C Programming Language", Crawford & Gelsinger’s "Programming the 80386″
Peter Norvig[Research Director at Google]: Structure and Interpretation of Computer Programs
David Heinemeier Hansson[Author of the Rails Framework]: Extreme Programming Explained
Dave Thomas[Author of the „Pragmmatic Programmer”]: "IBM/360 Principles of Operation."
Guido Van Rossum[The Python language creator]: Neil Stephenson’s Quicksilver.
James Gosling[The Java language creator]:Programming Pearls by Jon Bentley.
Tim Bray[One of the XML and Atom specifications author]:Bentley’s Programming Pearls

非電腦相關書籍
Linus Torvalds: Selfish Gene by Dawkins
David Heinemeier Hansson[Author of the Rails Framework]:1984, George Orwell
James Gosling[The Java language creator]:Guns, Germs & Steel by Jared Diamond
Tim Bray[One of the XML and Atom specifications author]:One Day in the Life of Ivan Denisovich

Introduction to Programming in C/C++ with Vim

Introduction to Programming in C/C++ with Vim
《用vim 寫程式快n 倍》
1. ctags
ctags是vim附代的工具, 可以讓從函式跳到該函式的定義處,
[/home/someuser/src]$ctags *
-R: Recurse into directories supplied on command line [no].
會在所在目錄下產生一個tags檔案,下面是檢索用的指令
control-] : 跳到指標關鍵字定義的位置
control-T : 可以用這個指令返回之前的位置

2.
set makeprg=gcc\ -o\ %<\ %
set errorformat=%f:%l:\%m
:make
:cl 列出所有錯誤
:cn 列出下一個錯誤
:cp 列出上一個錯誤

3.taglist
把taglist.txt放到~/.vim/doc
taglist.vim放到~/.vim/plugin
安裝完後
Tlist 打開Taglist視窗
c-w h, c-w l 可以在兩視窗互換
s Change the sort order of the tags (by name or by order)
Jump to the location where the tag under cursor is
defined.

Linux環境下的Socket編程

7/27/2006

bash script

把數行合併成一行, 有幾種作法:

1.tr -d '\n'
2.cat file|xargs echo
3.echo $(<filename)
4.sed -e 'N' -e 's/\n//' input

bash中用.來match任意字元, \1 \2代表第一個以及第個二match
$ cat test1
first:second
one:two
$ sed 's/\(.*\):\(.*\)/\2:\1/' test1
second:first
two:one

7/15/2006

今日閱讀2005-07-15


IE mhtml redirection漏洞利用方法


Project RainbowCrack
RainbowCrack is a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
In short, the RainbowCrack tool is a hash cracker. A traditional brute force cracker try all possible plaintexts one by one in cracking time. It is time consuming to break complex password in this way. The idea of time-memory trade-off is to do all cracking time computation in advance and store the result in files so called "rainbow table". It does take a long time to precompute the tables. But once the one time precomputation is finished, a time-memory trade-off cracker can be hundreds of times faster than a brute force cracker, with the help of precomputed tables.