无法在MIPS架构OpenWRT路由器上运行C程序的技术求助
Hey there! Let's walk through how to get a simple C program up and running on your MIPS-based OpenWRT router. Since this device uses the Broadcom BMIPS3300 (MIPS32r1) architecture, we can't compile directly on the router—it's too resource-constrained—so cross-compilation is the way to go. Here's your step-by-step playbook:
1. Set Up the Cross-Compilation Toolchain
First, you need a toolchain that generates code matching your router's MIPS32r1 instruction set. The most reliable way is to build it via the OpenWRT Build System:
- Grab the latest OpenWRT source code compatible with your WL500GP V1 (stable branches like 19.07 or 21.02 work well—double-check firmware compatibility first).
- Run
make menuconfigto configure the build:- Under Target System, select
Broadcom BCM47xx/53xx (MIPS) - Under Subtarget, pick
Broadcom MIPS32r1 - Under Target Profile, choose
Asus WL500GP V1 - Toolchain-related options are enabled by default for your target, but confirm they're checked
- Under Target System, select
- Run
make toolchain/installto build and install the toolchain. This will take a bit of time, but it guarantees full compatibility with your router's firmware.
Pre-built toolchains are available in OpenWRT's archives, but building your own avoids potential mismatches.
2. Write a Simple C Program
Let's start with a classic test to validate the workflow. Create a file named hello.c with this code:
#include <stdio.h> int main() { printf("Hello from my WL500GP V1 router!\n"); return 0; }
3. Cross-Compile the Program
Locate your cross-compiler in the build system's staging_dir/toolchain-mips_34kc_gcc-<version>_musl/bin/ directory. The compiler binary will look like mips-openwrt-linux-musl-gcc (the exact name depends on your GCC/musl version).
Compile your program with this command:
/path/to/your/toolchain/mips-openwrt-linux-musl-gcc hello.c -o hello
Pro Tip: Static Compilation
If you want to avoid dependency issues with the router's system libraries, compile statically—this bundles all required libraries into the executable, making it larger but fully self-contained:
/path/to/your/toolchain/mips-openwrt-linux-musl-gcc hello.c -o hello-static -static
4. Transfer the Program to the Router
Use scp to send the compiled binary to your router's /tmp directory (it's writable and non-persistent, perfect for testing):
scp hello root@<your-router-ip>:/tmp/
Replace <your-router-ip> with your router's actual IP (usually 192.168.1.1 or 192.168.0.1).
5. Run the Program on the Router
SSH into your router, then make the binary executable and run it:
# SSH into the router ssh root@<your-router-ip> # Make the program executable chmod +x /tmp/hello # Run it! /tmp/hello
You should see your hello message printed to the terminal—success!
Key Notes to Remember
- Instruction Set Alignment: The toolchain targets MIPS32r1, which matches your router's
isa : mips1 mips2 mips32r1from/proc/cpuinfo, so no extra compile flags are needed. If you ever need to explicitly specify, add-march=mips32r1to your command. - Router Resources: The WL500GP V1 has limited RAM and storage, so keep your programs lightweight. Avoid heavy libraries or memory-intensive operations.
- Library Compatibility: If you get "file not found" errors when running the program, double-check that your toolchain's C library matches the one on your router (OpenWRT uses musl by default, so our toolchain should align perfectly).
内容的提问来源于stack exchange,提问作者Michał Ogorzałek

