#include <stdio.h>
int main() {
char c;
int i;
float f;
double d;
char *cptr=&c;
int *iptr=&i;
float *fptr=&f;
double *dptr=&d;
printf("變數名稱 記憶體位址 占用記憶體 (bytes)\n");
printf("======== ================ ==================\n");
printf(" c \t %p \t %d\n", &c, sizeof(c));
printf(" i \t %p \t %d\n", &i, sizeof(i));
printf(" f \t %p \t %d\n", &f, sizeof(f));
printf(" d \t %p \t %d\n", &d, sizeof(d));
printf(" cptr \t %p \t %d\n", &cptr, sizeof(cptr));
printf(" iptr \t %p \t %d\n", &iptr, sizeof(iptr));
printf(" fptr \t %p \t %d\n", &fptr, sizeof(fptr));
printf(" dptr \t %p \t %d\n", &dptr, sizeof(dptr));
return 0;
}
按 Ctrl+O 輸入檔名 datalength.c 按 Enter, 再按 Ctrl+X 跳出 nano 編輯介面後用 gcc 編譯原始程式 :
login as: pi
pi@192.168.2.117's password:
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Sep 27 19:52:28 2017 from 192.168.2.110
pi@raspberrypi:~ $ nano datalength.c
pi@raspberrypi:~ $ gcc -o datalength datalength.c
用 ls -al 指令檢查 home 目錄下確實編譯出 datalength 這個可執行檔 :
pi@raspberrypi:~ $ ls -al
total 10348
drwxr-xr-x 24 pi pi 4096 Sep 29 07:33 .
drwxr-xr-x 3 root root 4096 Nov 26 2016 ..
-rw-r--r-- 1 pi pi 69 Nov 26 2016 .asoundrc
-rw------- 1 pi pi 3112 Sep 15 00:15 .bash_history
-rw-r--r-- 1 pi pi 220 Nov 26 2016 .bash_logout
-rw-r--r-- 1 pi pi 3512 Nov 26 2016 .bashrc
drwxr-xr-x 7 pi pi 4096 Mar 21 2017 .cache
drwx------ 13 pi pi 4096 Sep 18 19:32 .config
-rw-r--r-- 1 pi pi 1786 Feb 22 2017 crontab.txt
-rwxr-xr-x 1 pi pi 6356 Sep 29 07:33 datalength
-rw-r--r-- 1 pi pi 794 Sep 29 07:32 datalength.c
drwx------ 3 pi pi 4096 Nov 26 2016 .dbus
drwxr-xr-x 2 pi pi 4096 Nov 26 2016 Desktop
drwxr-xr-x 5 pi pi 4096 Nov 26 2016 Documents
pi@raspberrypi:~ $ datalength
-bash: datalength: command not found
根據下面這篇的解釋, 在目前的 home 目錄下執行 datalength 會讓樹莓派以為你是要執行預設使用者程式目錄 /user/bin 下面的 datalength, 但此程式事實上是在 home 目錄下, 樹莓派在 /usr/bin 下找不到 datalength 這程式, 所以回應 "command not found" 錯誤 :
# Question about C programming on the Pi. printf/STDOUT
"By just running test you are running the command called test which lives in a directory in your path, probably /usr/bin/test, Linux will always run things from your path and not something in the current directory."
正確的執行方式是前面要加 ./ 這個路徑 :
pi@raspberrypi:~ $ ./datalength
變數名稱 記憶體位址 占用記憶體 (bytes)
======== ================ ==================
c 0xbec46297 1
i 0xbec46290 4
f 0xbec4628c 4
d 0xbec46280 8
cptr 0xbec4627c 4
iptr 0xbec46278 4
fptr 0xbec46274 4
dptr 0xbec46270 4
這樣就能正常執行了.
參考 :
# Linux Pi的奇幻旅程(29)-Hello World!
# 设置并使用树莓派进行Python和C语言编程 (上)
# 樹莓派 Raspberry Pi 自行編譯與安裝 GCC 6 編譯器教學
# [How To Raspberry Pi] 安裝 GCC
# 【樹莓派】編譯一個Hello World程式在RPi上執行
# Question about C programming on the Pi. printf/STDOUT
1 則留言 :
版主,
請問一下,如果我是在樹莓派3B的IDLE 使用python做了一個UI,且使用IDLE 可以 run這個UI出來,
問題是如果我想要將其像上面一樣編譯成exe,我應該怎麼做?又如果我要將其編譯後產生執行檔移到相同
另外一片樹莓派3B上執行可以嗎?
謝謝
張貼留言