Executable says "line 1: ELF: not found" when starts
Solution 1
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.)
Solution 2
In addition to other answers here, there might be simple cause for "line 1: ELF: not found, line 2: syntax error: unexpected "(""
:
You are running a 64-bit program on a 32-bit machine. Try changing your compiling config.
Solution 3
Did you remember to change the file property to make it executable?
chmod +x filename
I know I often forget this step when I download something (usually an installation binary). Just a thought!
neshkeev
I am employed as a java software engineer. My interests are foundation of mathematics, the category theory and lambda calculus. I am programming languages agnostic
Updated on June 15, 2022Comments
-
neshkeev almost 2 years
I try to build a simple hello, world
ELF
for my routerXiaomi Router 3g
withcmake
. It runsLinux 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 viamkdir 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
withoutcmake
(- comment out the line
include $(INCLUDE_DIR)/cmake.mk
, - uncomment the
$(CP) ...
line in thedefine Build/Prepare
section - eliminate the
define Build/Compile
section
), it works just fine: it prints Hello, World!.
Log file is here,
.config
file is hereThe 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 <[email protected]> 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))
- comment out the line
-
Nick Chan Abdullah about 3 yearsthis is exactly what I encountered with my .net core program on debian. thanks
-
Marcello de Sales about 2 yearsSaved me while implementing a docker image for python!
-
Keith Thompson about 2 yearsThat wouldn't cause that particular error message.