如何在 Linux 中运行二进制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9477157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:52:43  来源:igfitidea点击:

How to run binary file in Linux

linuxbash

提问by Sijith

I have a file called commanKTand want to run it in a Linux terminal. Can someone help by giving the command to run this file? I tried ./commonRTbut I'm getting the error:

我有一个名为的文件commanKT,想在 Linux 终端中运行它。有人可以通过提供运行此文件的命令来提供帮助吗?我试过了,./commonRT但出现错误:

"bash: ./commonrt: cannot execute binary file"

[blackberry@BuildMc MainApp]$ ls -al commonKT
-rwxrwxr-x. 1 sijith sijith 10314053 Feb 27 16:49 commonKT

回答by Jagannath

:-) If not typo, why are you using ./commonRTinstead of ./commonKT??

:-) 如果不是错别字,你为什么用./commonRT而不是./commonKT??

回答by Ignacio Vazquez-Abrams

The volume it's on is mounted noexec.

它所在的卷已安装noexec

回答by j?rgensen

Or, the file is of a filetype and/or architecture that you just cannot run with your hardware and/or there is also no fallback binfmt_misc entry to handle the particular format in some other way. Use file(1)to determine.

或者,该文件的文件类型和/或体系结构无法与硬件一起运行和/或也没有备用 binfmt_misc 条目以其他方式处理特定格式。使用file(1)来确定。

回答by Frazer

To execute a binary, use: ./binary_name.

要执行二进制文件,请使用:./binary_name

If you get an error:

如果出现错误:

bash: ./binary_name: cannot execute binary file

bash: ./binary_name: 无法执行二进制文件

it'll be because it was compiled using a tool chain that was for a different target to that which you're attempting to run the binary on.

这将是因为它是使用工具链编译的,该工具链用于与您尝试运行二进制文件的目标不同的目标。

For example, if you compile 'binary_name.c' with arm-none-linux-gnueabi-gccand try run the generated binary on an x86 machine, you will get the aforementioned error.

例如,如果您编译“binary_name.c”arm-none-linux-gnueabi-gcc并尝试在 x86 机器上运行生成的二进制文件,您将收到上述错误。

回答by Olu Smith

To execute a binary or .run file in Linux from the shell, use the dot forward slash friend

要从 shell 在 Linux 中执行二进制文件或 .run 文件,请使用点正斜杠友元

 ./binary_file_name

and if it fails say because of permissions, you could try this before executing it

如果由于权限而失败,您可以在执行之前尝试此操作

 chmod +x binary_file_name
 # then execute it
 ./binary_file_name

Hope it helps

希望能帮助到你

回答by user1978011

It is possible that you compiled your binary with incompatible architecture settings on your build host vs. your execution host. Can you please have a look at the enabled target settings via

您可能在构建主机与执行主机上使用不兼容的架构设置编译了二进制文件。您能否通过以下方式查看启用的目标设置

g++ {all-your-build-flags-here} -Q -v --help=target

on your build host? In particular, the COLLECT_GCC_OPTIONSvariable may give you valuable debug info. Then have a look at the CPU capabilities on your execution host via

在您的构建主机上?特别是,该COLLECT_GCC_OPTIONS变量可能会为您提供有价值的调试信息。然后通过以下方式查看执行主机上的 CPU 功能

cat /proc/cpuinfo | grep -m1 flags

Look out for mismatches such as -msse4.2 [enabled]on your build host but a missing sse4_2flag in the CPU capabilities.

注意不匹配,例如-msse4.2 [enabled]构建主机上的不匹配,但sse4_2CPU 功能中缺少标志。

If that doesn't help, please provide the output of ldd commonKTon both build and execution host.

如果这没有帮助,请ldd commonKT在构建和执行主机上提供输出。

回答by chw21

This is an answer to @craq :

这是对@craq 的回答:

I just compiled the file from C source and set it to be executable with chmod. There were no warning or error messages from gcc.

我刚刚从 C 源代码编译了该文件,并使用 chmod 将其设置为可执行。没有来自 gcc 的警告或错误消息。

I'm a bit surprised that you had to 'set it to executable' -- my gccalways sets the executable flag itself. This suggests to me that gccdidn't expect this to be the final executable file, or that it didn't expect it to be executable on this system.

我有点惊讶您必须“将其设置为可执行文件”——我gcc总是自己设置可执行文件标志。这向我表明,gcc没有想到这是最终的可执行文件,或者没有想到它可以在这个系统上执行。

Now I've tried to just create the object file, like so:

现在我尝试只创建目标文件,如下所示:

$ gcc -c -o hello hello.c
$ chmod +x hello

(hello.cis a typical "Hello World" program.) But my error message is a bit different:

hello.c是一个典型的“Hello World”程序。)但我的错误信息有点不同:

$ ./hello
bash: ./hello: cannot execute binary file: Exec format error`

On the other hand, this way, the output of the filecommand is identical to yours:

另一方面,这样,file命令的输出与您的相同:

$ file hello
hello: ELF 64-bit LSB  relocatable, x86-64, version 1 (SYSV), not stripped

Whereas if I compile correctly, its output is much longer.

而如果我编译正确,它的输出会更长。

$ gcc -o hello hello.c
$ file hello
hello: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=131bb123a67dd3089d23d5aaaa65a79c4c6a0ef7, not stripped

What I am saying is: I suspect it has something to do with the way you compile and link your code. Maybe you can shed some light on how you do that?

我要说的是:我怀疑这与您编译和链接代码的方式有关。也许你可以解释一下你是如何做到的?

回答by musbach

If it is not a typo, as pointed out earlier, it could be wrong compiler options like compiling 64 bit under 32 bit. It must not be a toolchain.

如果不是拼写错误,如前所述,它可能是错误的编译器选项,例如在 32 位下编译 64 位。它不能是工具链。

回答by Fábio

The only way that works for me (extracted from here):

对我有用的唯一方法(从这里提取):

chmod a+x name_of_file.bin

Then run it by writing

然后通过编写运行它

./name_of_file.bin

If you get a permission error you might have to launch your application with root privileges:

如果您收到权限错误,您可能必须以 root 权限启动您的应用程序:

 sudo ./name_of_file.bin

回答by user2919481

your compilation option -cmakes your compiling just compilation and assembly, but no link.

您的编译选项-c使您的编译只是编译和汇编,但没有链接。