Debian Hello World: Difference between revisions
(Clean up after compilation: fix umount commands) |
|||
(8 intermediate revisions by 2 users not shown) | |||
Line 32: | Line 32: | ||
==Create myhello.c file via vim== | ==Create myhello.c file via vim== | ||
# vim myhello.c | # vim myhello.c | ||
Paste below code | Paste below code | ||
<pre> | <pre> | ||
#include <stdio.h> | #include <stdio.h> | ||
Line 41: | Line 41: | ||
} | } | ||
</pre> | </pre> | ||
ESC :wq! and save the file | ESC :wq! and save the file. | ||
== Compile code on Target == | == Compile code on Target == | ||
# apt-get update; apt-get install build-essentials gcc g++ | |||
# gcc myhello.c -o myhello.out | # gcc myhello.c -o myhello.out | ||
== Execute on Target == | == Execute on Target == | ||
# ./myhello.out | # ./myhello.out | ||
Line 50: | Line 52: | ||
# | # | ||
== Compile code via chroot(On PC for target) == | == Compile code via chroot (On PC for target) == | ||
$ cd {{#var:BUILD_FOLDER}} | |||
$ export ROOTFS_BASE={{#var:BUILD_FOLDER}}/rootfs/ | $ export ROOTFS_BASE={{#var:BUILD_FOLDER}}/rootfs/ | ||
$ sudo cp variscite/qemu_64bit/qemu-aarch64-static ${ROOTFS_BASE}/usr/bin/qemu-aarch64-static | $ sudo cp variscite/qemu_64bit/qemu-aarch64-static ${ROOTFS_BASE}/usr/bin/qemu-aarch64-static | ||
$ sudo mount -o bind /proc ${ROOTFS_BASE}/proc | $ sudo mount -o bind /proc ${ROOTFS_BASE}/proc | ||
$ sudo mount -o bind /dev ${ROOTFS_BASE}/dev | $ sudo mount -o bind /dev ${ROOTFS_BASE}/dev | ||
$ sudo mount -o bind /sys ${ROOTFS_BASE}/sys | |||
$ sudo mount -o bind /dev/pts ${ROOTFS_BASE}/dev/pts | $ sudo mount -o bind /dev/pts ${ROOTFS_BASE}/dev/pts | ||
$ sudo chroot $ROOTFS_BASE | $ sudo chroot $ROOTFS_BASE | ||
# gcc myhello.c -o myhello.out | # gcc myhello.c -o myhello.out | ||
You have two choices to execute this code on target | |||
* Transfer the binary to target via SCP | |||
* Pack rootfs and re-flash the rootfs. <br> | |||
It should have the binary you just compiled. | |||
== Clean up after compilation == | |||
To unmount the virtual filesystem run the following commands in the same exact order. | |||
$ sudo umount ${ROOTFS_BASE}/dev/pts | |||
$ sudo umount ${ROOTFS_BASE}/proc | |||
$ sudo umount ${ROOTFS_BASE}/dev | |||
$ sudo umount ${ROOTFS_BASE}/sys | |||
Note: Failing to follow the above order may result in an unresponsive PC. |
Latest revision as of 07:44, 27 April 2023
Build a sample C "Hello, world!" program (Cross Compiling)
Create a file called myhello.c with the following content:
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
Export the C (cross-)compiler path:
$ export CC=~/var_mx8mm_dart_debian_buster/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
Compile:
$ $CC myhello.c -o myhello
Now you should have an app called myhello, that can be run on your target board.
You can add it to your rootfs image or copy it directly to the rootfs on the board (using scp, for example).
Build a sample C "Hello, world!" program on Target
One of the biggest advantages of having Debian OS is you can compile your code directly on the target. Although not recommended as linking big programs can take entire memory but for smaller test is sufficient. This reduces back and forth transfer of the compiled binary between host PC. On Target:
Getting Ready target to install tools
# apt-get install -y gcc g++ automaker cmake build-essential vim
Create myhello.c file via vim
# vim myhello.c
Paste below code
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
ESC :wq! and save the file.
Compile code on Target
# apt-get update; apt-get install build-essentials gcc g++ # gcc myhello.c -o myhello.out
Execute on Target
# ./myhello.out Hello, World! #
Compile code via chroot (On PC for target)
$ cd ~/var_mx8mm_dart_debian_buster $ export ROOTFS_BASE=~/var_mx8mm_dart_debian_buster/rootfs/ $ sudo cp variscite/qemu_64bit/qemu-aarch64-static ${ROOTFS_BASE}/usr/bin/qemu-aarch64-static $ sudo mount -o bind /proc ${ROOTFS_BASE}/proc $ sudo mount -o bind /dev ${ROOTFS_BASE}/dev $ sudo mount -o bind /sys ${ROOTFS_BASE}/sys $ sudo mount -o bind /dev/pts ${ROOTFS_BASE}/dev/pts $ sudo chroot $ROOTFS_BASE # gcc myhello.c -o myhello.out
You have two choices to execute this code on target
- Transfer the binary to target via SCP
- Pack rootfs and re-flash the rootfs.
It should have the binary you just compiled.
Clean up after compilation
To unmount the virtual filesystem run the following commands in the same exact order.
$ sudo umount ${ROOTFS_BASE}/dev/pts $ sudo umount ${ROOTFS_BASE}/proc $ sudo umount ${ROOTFS_BASE}/dev $ sudo umount ${ROOTFS_BASE}/sys
Note: Failing to follow the above order may result in an unresponsive PC.