| 94 | ==== Step1. 修改hello.c ==== |
| 95 | {{{ |
| 96 | #!text |
| 97 | /* hello.c: A program to show the time since the Epoch */ |
| 98 | |
| 99 | #include <stdio.h> |
| 100 | #include <sys/time.h> |
| 101 | |
| 102 | int main(int argc, char* argv[]) |
| 103 | { |
| 104 | double sec; |
| 105 | struct timeval tv; |
| 106 | |
| 107 | gettimeofday(&tv, NULL); |
| 108 | sec = tv.tv_sec; |
| 109 | sec += tv.tv_usec / 1000000.0; |
| 110 | |
| 111 | printf("%f\n", sec); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | }}} |
| 116 | |
| 117 | ==== Step2. 更新設定檔 === |
| 118 | {{{ |
| 119 | $ autoscan |
| 120 | $ mv configure.scan configure.ac |
| 121 | $ autoconf |
| 122 | $ autoheader |
| 123 | $ ./configure |
| 124 | }}} |
| 125 | |
| 126 | ==== Step3. 修改source code使其支援protable ==== |
| 127 | {{{ |
| 128 | #!text |
| 129 | /* hello.c: A program to show the time since the Epoch */ |
| 130 | |
| 131 | #include <stdio.h> |
| 132 | #include "config.h" |
| 133 | |
| 134 | #ifdef HAVE_SYS_TIME_H |
| 135 | #include <sys/time.h> |
| 136 | #else |
| 137 | #include <time.h> |
| 138 | #endif |
| 139 | |
| 140 | double get_sec_since_epoch() |
| 141 | { |
| 142 | double sec; |
| 143 | |
| 144 | #ifdef HAVE_GETTIMEOFDAY |
| 145 | struct timeval tv; |
| 146 | |
| 147 | gettimeofday(&tv, NULL); |
| 148 | sec = tv.tv_sec; |
| 149 | sec += tv.tv_usec / 1000000.0; |
| 150 | #else |
| 151 | sec = time(NULL); |
| 152 | #endif |
| 153 | |
| 154 | return sec; |
| 155 | } |
| 156 | |
| 157 | int main(int argc, char* argv[]) |
| 158 | { |
| 159 | printf("%f\n", get_sec_since_epoch()); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | }}} |
| 164 | |
| 165 | ==== Step4. 編譯及執行 ==== |
| 166 | {{{ |
| 167 | $ autoscan |
| 168 | $ ./configure |
| 169 | $ make clean all |
| 170 | $ ./hello |
| 171 | }}} |
| 172 | |