无法执行.out文件,获得权限被拒绝

我编写了一个C ++程序并将其编译为生成a.out文件。 但是,每当我尝试运行它时,我都会被拒绝。 我读到我们可以使用sudo,但我不能完全使用它。 我使用像sudo“./a.out”之类的东西,但这也行不通。

编辑

这是我尝试“./a.out”时收到的消息。

bash: ./a.out: Permission denied 

通常, g++为创建的文件提供执行权限。 如果未传递-o选项,则该文件将命名为a.out

您的文件没有设置执行位的两个可能原因及其解决方案:

  1. umask值设置为类似0133的值,从而防止设置执行位。 解决方案:显式设置权限:

    chmod 755 a.out

  2. 您正在处理的文件系统不支持Linux权限。 如果您将文件放在FAT32格式的闪存驱动器上,可能就是这种情况。 解决方案:备份文件并将其格式化为ext2,或者使用fmask=0022umask=0022 (省略fmask )安装驱动器。 有关详细信息,请参阅mount 手册页上的“装载 脂肪选项”部分。

对于没有设置执行位的bash脚本,可以运行bash file.sh 对于具有可执行内容的所有文件(具有shebang行#!/path/to/interpreter set的编译文件和文件)存在这样的特征。 要在没有设置执行位的情况下执行文件,请使用特殊文件/lib/ld-linux.so.2 (或64位应用程序的/lib/ld-linux-x86-64.so.2 )来运行这样的程序:

 /lib/ld-linux-x86-64.so.2 a.out 

.out是一个不寻常的扩展。 通常这通常表示“跟踪输出”文件。

检查您用于编译的语法

例如

 gcc myfile.c /usr/lib/libsomelibrary.a -o outputfilename 

或者可能

 g++ myfile.cpp -lm -o outputfilename 

您可以检查是否在文件上设置了可执行位

 ls -l a.out 

或者你可以强制执行可执行位

 chmod +x a.out 

然后你可以运行你的文件

 ./a.out 

或简单地说

 a.out 

您还应该检查输出文件是否已正确写为二进制文件

 file a.out 

这将报告文件的格式 – 脚本或二进制文件

您很少需要以root身份执行,除非您已限制谁应该能够运行可执行文件。

如果你已经编译为root(例如sudo make),或者有一个以root身份安装可执行文件的Makefile那么我可以建议你在用户登录时重新获得权限

 sudo chown fred:fred a.out 

即用您的用户ID替换“fred”。

只需将文件夹复制到您的主文件夹即可。 您可能正试图在外部驱动器或其他东西上运行它。

第一个答案中FAT文件系统的解决方法

“如果您将文件放在FAT32格式的闪存驱动器上,可能就是这种情况。解决方案:( …)使用fmask = 0022或umask = 0022(省略fmask)安装驱动器。”

通常不起作用 – 无论如何,umask的默认值大部分都是0022,所以这不会改变任何东西。

但是,另一个mount参数的默认值实际上不允许执行二进制文件,尤其是当FAT文件系统以非root用户身份挂载时: noexec

所以只需使用exec选项安装FAT格式的驱动器,如下所示:

 sudo mount -o exec /dev/sd.. /mountpoint 

(这通常必须以root身份完成,因此“sudo”)并且您应该能够直接从那里执行二进制文件。

我打赌你的程序没有’m​​ain()’函数,就好像它一样,你的编译器会生成a.out可执行文件。 现在它只是一个充满代码的对象文件,但是没有入口点。 main()是C和C ++中的一个特殊函数名,它告诉编译器创建一个程序,而不仅仅是可以链接到程序或库的目标文件。

我有兴趣知道你用来生成这个文件的命令行,因为GNU GCC的c ++编译器,g ++,不会让我创建一个没有主要function的简单程序:

 #include  using namespace std; void no_main() { cout << "Hello World" << endl; } $ g++ hello.cc /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 12 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13 /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 21 /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../crt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status 

但是,如果我将'void no_main'更改为'int main',它的工作原理如下:

 $ g++ hello.cc $ ./a.out Hello World