[Solved] How to cross compile c program for OpenWrt

I have followed openwrt documentation: https://openwrt.org/docs/guide-developer/helloworld/start
I am compiling hello world program for OpenWRT router
Now i am getting this error:

root@kali:/home/kali/openwrt# make -j1 V=sc package/helloworld/compile
make[1]: Entering directory '/home/kali/openwrt'
make[2]: Entering directory '/home/kali/openwrt/package/libs/toolchain'
echo "libc" >> /home/kali/openwrt/staging_dir/target-mipsel_24kc_musl/pkginfo/toolchain.default.install
echo "libgcc" >> /home/kali/openwrt/staging_dir/target-mipsel_24kc_musl/pkginfo/toolchain.default.install
echo "libpthread" >> /home/kali/openwrt/staging_dir/target-mipsel_24kc_musl/pkginfo/toolchain.default.install
make[2]: Leaving directory '/home/kali/openwrt/package/libs/toolchain'
time: package/libs/toolchain/compile#0.26#0.14#0.37
make[2]: Entering directory '/home/kali/mypackages/examples/helloworld'
rm -f /home/kali/openwrt/build_dir/target-mipsel_24kc_musl/helloworld/.built
touch /home/kali/openwrt/build_dir/target-mipsel_24kc_musl/helloworld/.built_check
mipsel-openwrt-linux-musl-gcc -Os -pipe -mno-branch-likely -mips32r2 -mtune=24kc -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -msoft-float -mips16 -minterlink-mips16 -iremap/home/kali/openwrt/build_dir/target-mipsel_24kc_musl/helloworld:helloworld -Wformat -Werror=format-security -fstack-protector -D_FORTIFY_SOURCE=1 -Wl,-z,now -Wl,-z,relro -o /home/kali/openwrt/build_dir/target-mipsel_24kc_musl/helloworld/helloworld.o -c /home/kali/openwrt/build_dir/target-mipsel_24kc_musl/helloworld/helloworld.c
mipsel-openwrt-linux-musl-gcc -L/home/kali/openwrt/staging_dir/target-mipsel_24kc_musl/usr/lib -L/home/kali/openwrt/staging_dir/target-mipsel_24kc_musl/lib -L/home/kali/openwrt/staging_dir/toolchain-mipsel_24kc_gcc-7.5.0_musl/usr/lib -L/home/kali/openwrt/staging_dir/toolchain-mipsel_24kc_gcc-7.5.0_musl/lib -znow -zrelro -o /home/kali/openwrt/build_dir/target-mipsel_24kc_musl/helloworld/helloworld
**mipsel-openwrt-linux-musl-gcc: fatal error: no input files**
**compilation terminated.**
make[2]: *** [Makefile:52: /home/kali/openwrt/build_dir/target-mipsel_24kc_musl/helloworld/.built] Error 1
make[2]: Leaving directory '/home/kali/mypackages/examples/helloworld'
time: package/feeds/mypackages/helloworld/compile#0.26#0.26#0.55
make[1]: *** [package/Makefile:113: package/feeds/mypackages/helloworld/compile] Error 2
make[1]: Leaving directory '/home/kali/openwrt'
make: *** [/home/kali/openwrt/include/toplevel.mk:227: package/helloworld/compile] Error 2

Thank You.

gcc can't find the file.
It seems your linking command is not correct as it can't find the object file. (last line before failure)

You're probably missing the .o at the end of line 38 from the Makefile example that you pointed to:

This is my make file:

include $(TOPDIR)/rules.mk

# Name, version and release number

# Source settings (i.e. where to find the source codes)
# This is a custom variable, used below

include $(INCLUDE_DIR)/package.mk

# Package definition; instructs on how and where our package will appear in the overall configuration menu ('make menuconfig')
define Package/helloworld
	TITLE:=Hello, World!

# Package description; a more verbose description on what our package does
define Package/helloworld/description
	A simple "Hello, world!" -application.

# Package preparation instructions; create the build directory and copy the source code. 
# The last command is necessary to ensure our preparation instructions remain compatible with the patching system.
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)

# Package build instructions; invoke the target-specific compiler to first compile the source file, and then to link the file into the final executable
define Build/Compile
	$(TARGET_CC) $(TARGET_CFLAGS) -o $(PKG_BUILD_DIR)/helloworld.o -c $(PKG_BUILD_DIR)/helloworld.c

# Package install instructions; create a directory inside the package to hold our executable, and then copy the executable we built previously into the folder
define Package/helloworld/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/usr/bin

# This command is always the last, it uses the definitions and variables we give above in order to get the job done
$(eval $(call BuildPackage,helloworld))

as I said in my previous answer, this is incorrect as it should be on one line as in the example Makefile.

1 Like

thank you so much... how can i give you the tick ?

1 Like

no problem (sorry, I seem to have got the line wrong in my first answer).

For the tick, I think it works like this:

not getting the tick option

I've never used it myself. I think this is the official reference on how to do it:

Don't worry too much about it, it's not that important. Thanks for trying though, I appreciate it.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.