Compiling on OpenWRT
Jump to navigation Jump to search

These are some notes, we'll see if it works and warrants a full article...

  • Entware has better resources documentation for compiling on a router:
  • A key resource are headers;
    • To keep OpenWRT and Entware stuff separate, create the /opt directory: mkdir /opt
    • Then it is safe to install / download this: wget -qO- | tar xvz -C /opt/include
  • And from this source, it is easy to add the path when compiling:
    • By default, gcc searches these directories first: /usr/include and /usr/lib/gcc/arm-openwrt-linux-muslgnueabi/7.4.0/include
    • Downloading the above Entware Header File, this command can be used to search a different directory, like /opt/include, first: -IWhatEverDirectory
  • Items to install;
    • GCC:
      • opkg install gcc
    • Make:
      • opkg install make
    • Entware recommends installing the following;
      • opkg install busybox ldd make gawk sed (ldd and sed are not available as separate packages in OpenWRT, but both are included within the OpenWRT full version of BusyBox)
    • Additional utilities are recommended
      • Install the full version of BASH: opkg install bash
  • Environment Variables to add (these can be added in a myriad of ways, see the end of this section;
    • export LDFLAGS="-Wl,-rpath=/usr/lib:/opt/lib -Wl,--dynamic-linker=/usr/lib/gcc/arm-openwrt-linux-muslgnueabi/7.4.0/ -L/usr/lib:/opt/lib:/opt/include
    • NOTE: The original DD-WRT / Entware version is this: export LDFLAGS="-Wl,-rpath=/opt/lib -Wl,--dynamic-linker=/opt/lib/ -L/opt/lib", and needs to be modified for OpenWRT as shown above.
    • export CFLAGS="-O2 -pipe -march=armv7-a -mtune=cortex-a9 -fno-caller-saves -mfloat-abi=soft -I/opt/include "
    • NOTE: The original DD-WRT / Entware version is this: export CFLAGS="-O2 -pipe -march=armv7-a -mtune=cortex-a9 -fno-caller-saves -mfloat-abi=soft ", and needs to be modified for OpenWRT as shown above.
    • Adding the above Environment Variables;
      • Run each of the below command lines
        • export LDFLAGS="-Wl,-rpath=/usr/lib:/opt/lib -Wl,--dynamic-linker=/usr/lib/gcc/arm-openwrt-linux-muslgnueabi/7.4.0/ -L/usr/lib:/opt/lib:/opt/include
        • export CFLAGS="-O2 -pipe -march=armv7-a -mtune=cortex-a9 -fno-caller-saves -mfloat-abi=soft -I/opt/include "
      • Put the above two EXPORT commands in a single file, and run the following command;
        • source WhatEverFileName
      • Add the above two EXPORT commands to a user's profile
        • nano ~/.profile
        • NOTE: For the above to work, the full BASH shell must be installed (opkg install bash) AND the ~/.profile file must include the directive: SHELL=/bin/bash
  • far as the Entware documentation for Compiling:
    • Additional information for the ./configure --prefix /opt command, and what --prefix means:
    • Although they offer a link and a command for downloading additional libraries to /opt/include, there doesn't appear to be any notation of where that is utilized. Just having it as a bunch of files in a directory is useless unless a compiler like GCC can make use of the files. It should be
  • ELF Definition:


  • Create a file named nano
  • Add the following directives (from the above section) and save it;
export LDFLAGS="-Wl,-rpath=/usr/lib:/opt/lib -Wl,--dynamic-linker=/usr/lib/gcc/arm-openwrt-linux-muslgnueabi/7.4.0/ -L/usr/lib:/opt/lib:/opt/include

export CFLAGS="-O2 -pipe -march=armv7-a -mtune=cortex-a9 -fno-caller-saves -mfloat-abi=soft -I/opt/include "
  • Create a "Hello World" source code file: nano hw.c
  • Add the following text and save it;
#include <stdio.h>    

int main(void)    
printf("\nHello, world!\n\n");        
return 0;        

The below two commands are from an OpenWRT example here:

  • Type this command: gcc -c -o hw.o hw.c -Wall
  • Type this command: gcc -o hw hw.o

...and then run it: ./hw, which should produce the below output;

Hello, world!