
說實話,自來水博物館裡面真的是甚麼都沒有..有的就是那棟巴洛克風的建築.
不過不可諱言的,那棟建築物在黃昏及夜晚真的很美.
從建築物窗戶透出昏黃的燈光,在外面以略強的燈光直打外牆,配合巴洛克的風格,根本就是絕配.
只是我想不懂的是..為何大家都是白天來這裡拍婚紗...真


苦.澀.酸.甜...生活的筆記,生命的回憶
any | true if any bits are set |
count | returns the number of set bits |
flip | reverses the bitset |
none | true if no bits are set |
reset | sets bits to zero |
set | sets bits |
size | number of bits that the bitset can hold |
test | returns the value of a given bit |
to_string | string representation of the bitset |
to_ulong | returns an integer representation of the bitset |
#include
#include
using namespace std;
int main ()
{
bitset<4> mybits;
cout << class="comm">// 1111
cout << class="comm">// 1011
cout << class="comm">// 1111
return 0;
}
範例2:
#include
#include
#include
using namespace std;
int main (關閉某個bit)
{
bitset<4> mybits (string("1011"));
cout << class="comm">// 1001
cout << class="comm">// 0000
return 0;
}
使用 Automake產生 Makefile 的過程和以往自行編寫的方式非常不一樣,只需定義一些用到的巨集即可。
我 們把巨集及目標 (target) 寫在 Makefile.am 檔內,Automake讀入 Makefile.am 檔後會把這一串已經定義好的巨集展開並且產生對應的Makefile.in 檔, 然後再由 configure 這個 shell script 根據Makefile.in 產生適合的 Makefile。
這篇簡介是 Autoconf 及 Automake 的入門,希望這篇檔能幫助你對產生 Makefile有個簡單的依據。其他有關開發 GNU 程式或 C 程式設計及 Makefile 的詳細運用及技巧,我建議你從 GNU Coding Standards3 (GNU 編碼標準規定) 讀起,裏面包含了 GNU Makefile 慣例,還有發展 GNU 軟體套件的標準程式和慣例。這些 GNU 軟體的線上說明文件可以在 http://www.gnu.org/ 這個網站上找到。
1. 編輯 configure.in 檔
configure.in 檔的內容是一連串 GNU m4 的巨集,這些巨集經過autoconf 處理後會變成檢查系統特徵的 shell script。
我們可先用 autoscan 掃描原始檔以產生一個 configure.scan 檔,再對 configure.scan 做些修改成 configure.in 檔。
configure.in 內必須以 AC_INIT 巨集開頭,以AC_OUTPUT 巨集結尾。中間定義多個使用者自定巨集 (User Defined Micro),使用者自定巨集的順序沒有規定。
configure.in常用的範例巨集如下:
dnl
為注解,這個巨集後面的字不會被處理。
AC_INIT(FILE)
這個巨集用來檢查原始碼所在的路徑,autoscan 會自動產生,我們不必修改它。
AM_INIT_AUTOMAKE(PACKAGE,VERSION)
PACKAGE 是我們所要產生軟體套件的名稱,VERSION 是版本編號。
AC_PROG_CC
檢查系統可用的 C 編譯器,如果原始程式是用 C 寫的就需要這個巨集。
AC_OUTPUT(FILE)
設定 configure 所要產生的檔案,如果是 Makefile 的話,configure 便會把它檢查出來的結果帶入 Makefile.in 檔然後產生合適的 Makefile。
2. 產生 aclocol.mr 檔
實 際上,我們使用 Automake 時,還須要一些其他的巨集,這些額外的巨集我們用 aclocal 來幫我們產生。執行 aclocal 會產生 aclocal.m4 檔,如果沒有特別的用途,我們可以不必修改它,用 aclocal 所產生的巨集會告訴 Automake 怎麽做。
3. 產生 configure 檔
有了 configure.in 及 aclocal.m4 兩個檔案後,便可以執行 autoconf來產生 configure 檔了。
Autoconf 是用來產生 'configure' 檔的工具。'configure' 是一個shell script,它可以自動設定原始程式以符合各種不同平臺上 Unix 系統的特性,並且根據系統三數及環境產生合適的 Makefile 檔或是C 的標頭檔 (header file),讓原始程式可以很方便地在這些不同的平臺上被編譯出來。Autoconf 會讀取 configure.in 檔然後產生 'configure' 這個shell script。
4. 編輯 Makefile.am 檔
接下來我們要編輯 Makefile.am 檔,Automake 會根據 configure.in 中的巨集把Makefile.am 轉成 Makefile.in 檔。Makefile.am 檔定義我們所要產的目標:
AUTOMAKE_OPTIONS
Automake 主要是幫助開發 GNU 軟體的人員維護軟體套件,所以在執行 automake 時,會檢查目錄下是否存在標準 GNU 軟體套件中應具備的檔檔案,例如 'NEWS'、'AUTHOR'、'ChangeLog' 等文件檔。
設成 foreign 時,automake 會改用一般軟體套件的標準來檢查。
bin_PROGRAMS
定義我們所要產生的執行檔檔名。如果要產生多個執行檔,每個檔名用空白字元隔開。
hello_SOURCES
定義執行檔所需要的原始檔。
如果 'hello' 這個程式是由多個原始檔所產生,必須把它所用到的原始檔都列出來,以空白字元隔開。
5. 產生Makefile.in
Automake 產生出來的 Makefile.in檔是完全符合 GNU Makefile 的慣例,我們只要執行 configure 這個shell script 便可以產生合適的 Makefile 檔了。
# automake --add-missing
加上 --add-missing 選項是告訴 automake 順便幫我們加入包裝一個軟體套件所必備的檔案。
6.使用 Makefile
執行 ./configure,利用 configure 產生Makefile 檔。
要 注意的是,利用 Autoconf 及 Automake 所產生出來的軟體套件是可以在沒有安裝 Autoconf 及 Automake 的環境上使用的,因為 configure 是一個 shell script,它己被設計可以在一般 Unix 的 sh 這個 shell 下執行。但是如果要修改 configure.in 及 Makefile.am 檔再產生新的configure 及 Makefile.in 檔時就一定要有 Autoconf 及 Automake 了。
Autoconf 和 Automake 功能十分強大,你可以從它們所附的 info 檔找到詳細的用法。你也可以從許多現存的 GNU 軟體或 Open Source 軟體中找到相關的 configure.in 或 Makefile.am 檔,它們是學習 Autoconf 及Automake 更多技巧的最佳範例。
在 Linux 上寫程式的人大概都要使用 Makefile執行編譯程式的工作,可是要寫出一個 Makefile就不簡單了。網路上介紹 Makefile 的資源很多,可是要變成高手並不容易,難怪許多人聞 Linux 色變。本文將介紹如兩套軟體來協助我們『自動』產生 Makefile 檔: GNU Autoconf 及 Automake
讓開發出來的軟體可以像一般常見的 GNU 軟體一樣,只要用 ``./configure'', ``make all'',``make install'' 就可以完成compile和install的動作。
希望這份簡介能幫助您輕鬆地進入 Linux Programming 的殿堂。
1. 上路之前
程 式設計師只需寫一些預先定義好的巨集 (macro),交給 Automake 處理後會產生一個可供 Autoconf 使用的 Makefile.in 檔。再配合利用Autoconf 產生的自動設定檔 configure 即可產生一份符合 GNU Makefile慣例的 Makeifle 了。
在開始試著用 Automake 之前,請先確認你的系統已經安裝以下的軟體:
l GNU Automake
l GNU Autoconf
l GNU m4
l perl
l GNU Libtool (如果你需要產生 shared library)
我會建議你最好也使用 GNU C/C++ 編譯器、GNU Make 以及其他 GNU 的工具程式來做為開發的環境,這些工具都是屬於 Open Source Software不僅免費而且功能強大。
2. 一個簡單的例子
Automake 所產生的 Makefile 除了可以做到程式的編譯和連結,還把產生相關資源檔 (如 manual page及 info 檔) 都考慮進去。所以原始程式所存放的目錄架構最好符合 GNU 的標準慣例,接下來我拿 hello.c 來做為例子。
在工作目錄下建立一個新的子目錄 ``devel'',再在 devel 下建立一個``hello'' 的子目錄,我們的程式及其相關檔案存放在 ./devel/hello 目錄下:
# mkdir devel
# cd devel
# mkdir hello
# cd hello
用編輯器寫好 hello.c 檔
#include int main(int argc, char** argv) { printf(``Hello world!\n''); return 0; } |
接下來就要用 Autoconf 及 Automake 來幫我們產生 Makefile 檔了,
2.1. 產生一個 configure.scan 的檔案
執行 autoscan 後會產生一個configure.scan 的檔案,可以用它做為 configure.in檔的原型。
# autoscan
# ls
configure.scan hello.c
2.2. 產生一個 configure.in 的檔案
編輯 configure.scan 檔,並且把它的檔名改成 configure.in, 修改資料內容如下:
dnl Process this file with autoconf to produce a configure script. AC_INIT(hello.c) AM_INIT_AUTOMAKE(hello, 1.0) dnl Checks for programs. AC_PROG_CC dnl Checks for libraries. dnl Checks for header files. dnl Checks for typedefs, structures, and compiler characteristics. dnl Checks for library functions. AC_OUTPUT(Makefile) |
2.3. 產生 aclocal.m4檔案
執行 aclocal會產生GNU m4 的巨集到aclocal.m4
# aclocal
2.4. 產生 configure檔案
執行 autoconf,Autoconf 會讀取 configure.in 檔然後產生 'configure' 這個shell script。
# autoconf
2.5. 產生 Makefile.in檔案
接下來我們要編輯 Makefile.am 檔,Automake 會根據 configure.in 中的巨集把Makefile.am 轉成 Makefile.in 檔。
Makefile.am內容
AUTOMAKE_OPTIONS= foreign bin_PROGRAMS= hello hello_SOURCES= hello.c |
這個程式需要 'hello.c'、'main.c'、'hello.h' 三個檔案的話,則定義 hello_SOURCES= hello.c main.c hello.h 如果我們定義多個執行檔,則對每個執行檔都要定義相對的filename_SOURCES。
Automake 會根據 Makefile.am 檔產生一些檔案,包含最重要的 Makefile.in
# automake --add-missing
automake: configure.in: installing `./install-sh'
automake: configure.in: installing `./mkinstalldirs'
automake: configure.in: installing `./missing'
2.6. 最後執行 ./configure
# ./configure
creating cache ./config.cache
checking for a BSD compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking whether make sets ${MAKE}... yes
checking for working aclocal... found
checking for working autoconf... found
checking for working automake... found
checking for working autoheader... found
checking for working makeinfo... found
checking for gcc... gcc
checking whether the C compiler (gcc ) works... yes
checking whether the C compiler (gcc ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
updating cache ./config.cache
creating ./config.status
creating Makefile
現在你的目錄下已經產生了一個 Makefile 檔,下個 ``make'' 指令就可以開始編譯 hello.c 成執行檔。你還可以試試 ``make clean'',''make install'',''make dist'' 看看會有什麽結果。
# make
gcc -DPACKAGE=\"hello\" -DVERSION=\"1.0\" -I. -I. -g -O2 -c hello.c
gcc -g -O2 -o hello hello.o
# ./hello
Hello! GNU!
在命令列下寄送mail很多人都會,但是更多人不會。本篇告訴你怎麼寄純文字、加附檔及HTML格式的mail 。
在Unix的系統管理上我們常會把各式各樣指令的輸出導到檔案去,但是檔案在系統上,想看一下內容難免還得要登入上去。於是懶惰的人就會把這些輸出寄出來給相關的人,這樣就不需要還得連進去系統了。
在Unix下寄mail是一件很平常的事,有人可能會覺得這個有點老生常談,但事實是還是有人會來問我命令列下要怎麼寄 mail,而這個問題在網路上應該還是FAQ中的FAQ。
在Unix下最常被用來寄信的指令應該非mail莫屬,因為它普遍的存在各種不同版本不同平台的Unix系統上。或許你習慣用它的加強版 mailx。不過這二個用起來差不多我也不清楚到底有什麼差別,知道的人跟我講一下吧。另外有一個指令叫mutt 它是一個功能相當強大的MUA軟體,如果你有安裝的話,也可以使用它來寄mail。
假設你想把/etc/hosts的內容傳到你的電腦上,你不需要使用ftp來把它抓到你的電腦,只要使用指令來將它寄給你自己。
# cat /etc/hosts |mailx -s 'hosts table' jerry@abc.com
許多人應該都知道要怎麼用,而且也很多人應該是這樣子用的。上面的指令並沒有錯,只是可以直接把檔案導入給 mailx,為什麼要cat給mailx呢?公司薪水給的又不多,實在沒必要多花時間在處理公事上,所以字能少打就少打吧。
# mailx -s 'hosts table' jerry@abc.com < /etc/hosts
-s(Subject)是主旨,如果主旨有空白字元的話,記得要用單引號(')或是雙引號(")括起來。最後一個參數是收件人,如果收件人有多個的時候要用逗號(,)分隔,如jerry@abc.com,jerry@abc.com.tw。
或者你在Linux下,而且又裝了mutt。你也可以使用mutt來寄信,mutt的參數和mail差不多。
# mutt -s 'hosts table' jerry@abc.com < /etc/hosts
可是這樣子寄,/etc/hosts的內容是在信裡面呀,我想把它變成附件可以嗎?當然可以,只要跟其他的程式配合就可以了。
# uuencode /etc/hosts hosts.txt | mailx -s 'hosts table' jerry@abc.com
利用uuencode把/etc/hosts編碼過後丟給mailx就可以了,uuencode第一個參數是要編碼的檔案,第二個參數是編碼後的檔名,在這裡就是附件的檔名。下面的例子示範了把tar檔變成附件直接寄出。
# tar cf - /etc/* | uuencode etc.tar | mailx -s 'hosts table' jerry@abc.com
如果你用的是mutt那就更簡單了。只要加個-a(attach file)
# echo '/etc/hosts'|mutt -a /etc/hosts -s 'hosts table' jerry@abc.com
有人會問說,Jerry,我不要只是寄純文字的mail,可以寄html格式的mail嗎?在我回答之前,可以容許我先打你一拳嗎?如果你有 mutt(1.5.x 以上才行)的話要寄html格式的mail就很容易了(什麼,到這個時候還沒發現mutt有多好用嗎?還不快去裝起來。)。一行指令就可以搞定。
# mutt -e 'set content_type="text/html"' jerry@abc.com -s 'html format' <>
mutt允許我們對要寄出的mail修改它的檔頭(header),所以可以使用-e來設定檔頭的變數content_type 為"text/html",這個變數名稱content_type和真正檔頭Content-type的字不一樣哦!不要搞錯了。最後再導入一個html 格式的檔案給它就可以了。記住,-e後面那一串字不能錯哦!(1.5.x 以上才有這個變數。)
如果沒有mutt呢!裝一個,不然就繼續看下去。絕大多數的Unix系統應該都有內建sendmail,而sendmail有個參數-t可以達成我們的要求。看看下面的範例。
# echo 'Mime-Version: 1.0
> Content-type: text/html; charset="iso-8859-1"
> From: jerry@abc.com
> To: jerry@abc.com.tw
> Subject: test.......
>
> html file
> ' | sendmail -t
使用echo把檔頭和所要html的內容全部輸出給sendmail這樣子就可以了。不過我想沒人會真的在命令列這樣使用,多半會用在script裡面。下面提供一個script範例給你參考,主要的部份在被#包住的那個區塊,把你要的輸出做成html格式就可以了。
#!/bin/sh
fromuser=jerry@abc.com
touser=jerry@abc.com,abc,boss@abc.com
subject="`uname -n` df output"
sendmail="/usr/sbin/sendmail -t"
header="Mime-Version: 1.0
Content-type: text/html; charset="iso-8859-1"
From: $fromuser
To: $touser
Subject: $subject
""footer="
下面二個script,第一個是shell script;第二個是perl script,你可以把要輸出的內容存成html檔,再用sendhtml直接寄出html格式的mail。寫的不怎麼樣,但最少可以使用啦!也給你一個 想法做參考,你可以以此延伸然後寫一個符合你的script。
使用的方法如下,
sendhtml.pl "my subject" jerry@abc.com my_file.html
#!/bin/sh
## usage : sendhtml.sh subject To_User html_file
subject="$1"
touser="$2"
htmlfile="$3"
fromuser="jerry@abc.com"
sendmail="/usr/sbin/sendmail -t"
header="Mime-Version: 1.0
Content-type: text/html; charset="iso-8859-1"
From: $fromuser
To: $touser
Subject: $subject"
{
echo "$header"
cat "$htmlfile"
} | $sendmail
#!/usr/bin/perl -w
# usage : sendhtml.pl subject To_User html_file
my $sendmail = "/usr/sbin/sendmail";
my $fromuser = "jerry@abc.com";
$subject = $ARGV[0];
$touser = $ARGV[1];
$htmlfile = $ARGV[2];
$header="Mime-Version: 1.0
Content-type: text/html; charset="iso-8859-1"
From: $fromuser
To: $touser
Subject: $subjectn";
open(fd2, "| $sendmail -t");
print fd2 $header;
open(fd1, $htmlfile);
while(my $line=
print fd2 "$linen";
}
close(fd2);
close(fd1);
在寄附件的部份,能不用uuencode就不要用,因為uuencode+mailx所寄出來的mail並不會在檔頭的部份加入MIME編碼的訊 息,而現在有越來越多的郵件程式不會正確讀取此種格式的mail。如yahoo, MSN....等webmail,在開啟這些mail時都會顯示亂碼,而不是正確的附檔,所以建議儘量使用別的程式來取代。
在命令列下寄mail的方法不只這些,如使用nail(已經改名叫mailx)或是使用perl、php....等也是可以,但以一般需求來講,我覺得這些應該已經夠了。如果你有什麼好的方法,歡迎你和大家分享。