We write “Hello, World!” in C for OpenWRT

Result

Result

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.

  1. Let's take a look at the openwrt web interface

  2. 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?

  1. After following the link, we look for the version of OpenWRT installed on our router. For me, it was 23.05.3

  2. Click here targets

  3. Based on the target platform, we look at what architecture you have, for me it is ath79I look for it and go through it

  4. 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

  5. We go to the very bottom and find the inscription: Supplementary Files

  6. Looking for openwrt-sdk-….

  7. Right-click on the found openwrt-sdk and copy the link

Installing OpenWRT-SDK

  1. Moving on to WSL (cmd.exe -> wsl)

  2. 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

  3. 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 minutes

  4. Go 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

  5. 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 path

  6. Let's check that everything is done correctly:
    mips-openwrt-linux-gcc --version

    Will display such information

    Will display such information

Compile the code

  1. Don't forget to exit the compiler folder: cd ..

  2. Create a file hello.c using: > hello.c

  3. 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 find hello.c there, then open it with any convenient editor

  4. We insert:
    #include <stdio.h>
    int main()
    {
    printf("Hello, World!\n");
    return 0;
    }

  5. We save

  6. Compile: mips-openwrt-linux-gcc -o hello hello.c

  7. 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

Ready!

Ready!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *