> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/general/development-on-crowpi/cross-compiler-raspberry-pi.md).

# Cross-Compile

## Cross-Compile

Cross-compile เป็นการ compile code บน platform หนึ่งแล้วสามารถไปรันอีก platform หนึ่งได้ ซึ่งการ Cross-complie มักจะใช้เมื่อโปรแกรมนั้นมีความซับซ้อนทำให้การ complie บนบอร์ดอาจจะใช้เวลานาน ดังนั้นการ Cross-complie เป็นทางเลือกหนึ่งในการช่วย complie โปรแกรม

### Cross-Compile for RaspberryPi&#x20;

เราสามารถเขียนโปรแกรมลงคอมพิวเตอร์ของเราและทำการ Cross-Complie จาก platform Ubuntu แล้วทำโปรแกรมที่ complie แล้วไปรันบน Raspberry Pi OS ได้ โดยมีตัวอย่างดังต่อไปนี้

&#x20;1\. ติดตั้งแพคเกจ Cross-compile ภาษา C/C++ โดยพิมพ์คำสั่งลงใน terminal บน VScode โดยใช้คำสั่งดังนี้

```
sudo apt-get install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
```

2\. หลังจากติดตั้งเสร็จทำการ cross-compile code hello\_world.cpp ที่เคยสร้างไว้ก่อนหน้านี้โดยพิมพ์คำสั่ง ดังนี้

```
arm-linux-gnueabihf-g++ hello_world.cpp -o hello
```

พิมพ์ ls จะปรากฏไฟล์ที่สามารถนำไปใช้กับ Raspberrypi&#x20;

```
ls
```

3\.  ตรวจสอบ ip ของ Raspberrypi ที่ต้องการ Remote

&#x20;   3.1 เปิด Raspberrypi และทำการเชื่อมต่อ wifi และ internet ให้กับบอร์ด Raspberrypi&#x20;

&#x20;   3.2 เปิด หน้า Terminal ของบอร์ด Raspberrypi พิมพ์คำสั่งเพื่อแสดง ip ของบอร์ด Raspberrypi&#x20;

```
ifconfig
```

4\. เมื่อทราบ ip ของบอร์ด RaspberryPi แล้ว กลับมาที่VScode บนคอมพิวเตอร์ ทดลอง คัดลอกไฟล์ที่ complie ไปยัง RaspberryPi โดยใช้คำสั่งดังนี้

```
scp hello pi@ip-address:~/.
```

5\. ทำการ secure shell (SSH) เพื่อเข้าควบคุม บอร์ด RaspberryPi ผ่านคอมพิวเตอร์

```
ssh pi@ip-address
```

6\. พิมพ์คำสั่ง ls จะเห็นไฟล์ที่ได้จากคอมพิวเตอร์ ให้ทำการ complie โดยใช้คำสั่งดังนี้

```
./hello
```
