[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# 
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
root@kali:/home/kali/openwrt# 

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:
https://openwrt.org/docs/guide-developer/helloworld/chapter3#creating_the_package_manifest_file

This is my make file:

include $(TOPDIR)/rules.mk

# Name, version and release number
PKG_NAME:=helloworld
PKG_VERS:=1        
PKG_RELEASE:=1

# Source settings (i.e. where to find the source codes)
# This is a custom variable, used below
SOURCE_DIR:=/home/kali/mypackages/examples/helloworld

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
	SECTION:=examples
	CATEGORY:=Examples
	TITLE:=Hello, World!
endef

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

# 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)
	cp $(SOURCE_DIR)/* $(PKG_BUILD_DIR)
	$(Build/Patch)
endef

# 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
	$(TARGET_CC) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/$1
	$(PKG_BUILD_DIR)/helloworld.o
endef

# 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
endef

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