We write “Hello, World!” in C for OpenWRT
Brief history
I was wondering how I could compile C code for OpenWRT on my router.
There are instructions on the Internet, but they are vague and they are already 6-8 years old. Therefore, this article contains up-to-date information on compilation.
Requirements
WSL (with Ubuntu 22.04+)
(or VDS/VPS, if for some reason it is not possible to install WSL)
Looking for OpenWRT-SDK
But, most importantly, it is worth considering that depending on the target platform, the program you compiled may not be suitable for all routers at the same time. This means that if you compile from under openwrt-sdk for ath79, then for a router on the platform (for example) arm or at91 your program will not work.
So, first, let's define the target platform. This can be done in two ways.
Let's take a look at the openwrt web interface
Connect to the router via ssh and enter:
cat /etc/openwrt_release
We have determined it, and now we are looking for OpenWRT-SDK for this platform.
By link: https://downloads.openwrt.org/releases/
How to search?
After following the link, we look for the version of OpenWRT installed on our router. For me, it was 23.05.3
Click here targets
Based on the target platform, we look at what architecture you have, for me it is ath79I look for it and go through it
Next we turn again to the target platform, in the second part the type is written, for me it is genericlet's move on to it
We go to the very bottom and find the inscription: Supplementary Files
Looking for openwrt-sdk-….
Right-click on the found openwrt-sdk and copy the link
Installing OpenWRT-SDK
Moving on to WSL (
cmd.exe -> wsl
)Let's download OpenWRT-SDK. For this we use
wget link
and instead of the word link insert the link by simply right-clicking. For example, it should look like this:wget https://downloads.openwrt.org/releases/23.05.4/targets/ath79/generic/openwrt-sdk-23.05.4-ath79-generic_gcc-12.3.0_musl.Linux-x86_64.tar.xz
Unzip the SDK. To do this, use
tar -xvf filename
and instead of filename insert the name of the file you downloaded. Example:tar -xvf openwrt-sdk-23.05.4-ath79-generic_gcc-12.3.0_musl.Linux-x86_64.tar.xz
ps unpacking may take some time, about 2-7 minutesGo to the unpacked SDK directory. Use
cd filename
.
Example:cd openwrt-sdk-23.05.4-ath79-generic_gcc-12.3.0_musl.Linux-x86_64
Add the paths to the compiler to the PATH variable:
export STAGING_DIR=$PWD/staging_dir
export $PATH=$PWD/staging_dir/toolchain-mips_24kc_gcc-12.3.0_musl/bin
ps i suspect the paths may be a little different, be careful
pss $PWD is a variable in Linux that contains the absolute pathLet's check that everything is done correctly:
mips-openwrt-linux-gcc --version
Compile the code
Don't forget to exit the compiler folder:
cd ..
Create a file
hello.c
using:> hello.c
Open it with vi or nano (or some other editor).
If this creates difficulties, you can go to the following path in Windows Explorer:C:\Users\UserName
and findhello.c
there, then open it with any convenient editorWe insert:
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}We save
Compile:
mips-openwrt-linux-gcc -o hello hello.c
Ready
Testing Hello World on OpenWRT
We need to deliver the received goods by any means possible. hello
(binary file) to our router.
The most convenient option, in my opinion, is to use sFTP
through FileZilla
Before this, you need to install the openssh-sftp server. It weighs ~68 Kilobytes.
Do you have enough space? Let's see through df -h
on the line overlayfs:/overlay
(I have 1.1 megabytes free)
We enter opkg update
and after updating the packages we enter opkg install openssh-sftp-server
Download FileZilla. In it Файл -> Менеджер Сайтов -> Новый сайт
In the Host field, enter the IP through which we enter via ssh, the default user is root, and the password, I think, is clear. Then we connect and get to the root /root
Now we transfer the file hello
into this root.
We return to the router's ssh and grant execution rights: chmod +x hello
We enter ./hello