Write your first C program
Last updated
Last updated
Step 1: Find a suitable text editor that suits you eg: Atom, Sublime text, vim, notepad ++, etc..
Step2: Write a small C code on your HOST PC and save it with a .c
extension(eg: hello.c
). You can try our example code given below:
Step 3: Open your terminal (ctrl + alt + T), navigate to the Toolchain folder and untar the gcc-linaro-7.3.1-2018.05-i686_arm-linux-gnueabihf.tar.xz
file using the command tar -xvf gcc-linaro-7.3.1-2018.05-i686_arm-linux-gnueabihf.tar.xz
.
Step 4: Navigate to the mentioned folder /Toolchain/gcc-linaro-7.3.1-2018.05-i686_arm-linux-gnueabihf/bin
and then type in pwd
and copy the path that appears.
Step 5: Open the env.sh
file and paste the copied path to the corresponding line.
Step 6: Run the env.sh
file by using the command . env.sh
.
Make sure the the toolchain is running on your PC by typing "arm" and hitting the <TAB> key twice.
If the toolchain isn't running you'll only see a very few of them. NOTE: Do not close or switch your terminal as it will disable the toolchain that is running. We need to compile the C file with the enabled toolchain to get an executable that can be executed on the Board, as our PC is an X86 architecture and the board is an ARM architecture.
Step 7: Navigate back to your C folder and compile the C code using the toolchain enabled terminal.
arm-linux-gnueabihf-gcc hello.c -o hello_board
Step 8: Copy the file to /var/lib/tftpboot
. Type in cp hello_board /var/lib/tftpboot
to copy the binary.
Step 9: Open a Terminal on the board, Transfer the compiled binary "hello_board"
to the board either by TFTP or by SD Card. (Click here to follow the process to transfer files from Host to Board)
Now you can see your binary on the rootfs of the board.
Step 10: Give the necessary permissions chmod +x hello_board
Step 11: Run the binary using ./hello_board
------------------Congratulations on running your first C program on the Board!!--------------------