I try to build a simple hello, world ELF
for my router Xiaomi Router 3g
with cmake
. It runs
Linux OpenWrt 4.14.95 #0 SMP Wed Jan 30 12:21:02 2019 mips GNU/Linux
I use the sdk for ramips platform (my router's platform).
The idea is to use $(PKG_BUILD_DIR)
as a directory to perform an external build (which is usually done via mkdir build ; cd build ; cmake ..
)
The build finishes successfully and when I install it on my router and start it, it fails with:
root@OpenWrt:~# chw
/usr/bin/chw: line 1: ELF: not found
/usr/bin/chw: line 2: syntax error: unexpected "("
When I build the ELF
without cmake
(
include $(INCLUDE_DIR)/cmake.mk
,$(CP) ...
line in the define Build/Prepare
sectiondefine Build/Compile
section), it works just fine: it prints Hello, World!.
Log file is here,
.config
file is here
The source code is available here
Here is the source code:
main.c
:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
CMakeLists.txt
:
cmake_minimum_required (VERSION 2.6)
project (chw)
add_executable(chw main.c)
install(TARGETS chw DESTINATION /usr/bin)
Makefile
:
include $(TOPDIR)/rules.mk
PKG_NAME:=chw
PKG_VERSION:=0.1
PKG_RELEASE:=1
PKG_MAINTAINER:=John Doe <john.doe@example.com>
PKG_LICENSE:=CC0-1.0
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
define Package/chw
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Hello world application
URL:=https://www.example.com
endef
define Package/chw/description
hello world application
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
# $(CP) ./src/{main.c,Makefile} $(PKG_BUILD_DIR)/
endef
define Build/Configure
cmake -B $(PKG_BUILD_DIR) -S ./src
endef
define Build/Compile
$(call Build/Compile/Default,-C $(PKG_BUILD_DIR))
endef
define Package/chw/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/chw $(1)/usr/bin/
endef
$(eval $(call BuildPackage,chw))
Apparently your target system either doesn't support executing ELF files, or doesn't recognize your file as an ELF file.
When you execute a file on a UNIX-like system, it will be executed directly if the kernel recognizes it as an executable format. If not, if there's no #!
line at the top, it will try to execute it as a shell script using /bin/sh
. It's a binary file, but there's no really firm distinction between binary and text files.
The shell apparently tried to interpret the beginning of the file (which includes the characters ELF
) as a command, and wasn't able to find that command in $PATH
.
You'll need to find a way to generate an executable file that your target system will recognize and accept. (I don't know how to do that.)