【问题标题】:bash: ./comp.out: Permission deniedbash:./comp.out:权限被拒绝
【发布时间】:2016-03-30 07:19:22
【问题描述】:

我正在尝试编写一个程序来比较 2 个文件并返回它们是否相等。

我只能使用以下函数:forkdupdup2openwriteexecread

当我在 linux gcc 上编译程序时,它返回:

bash: ./comp.out: 权限被拒绝

代码:

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int CheckSimilar(char *path1, char *path2);

int main(int argc, char **argv) {

    int returnValue = CheckSimilar(argv[1], argv[2]);

    switch (returnValue){

        case 1:
            write(1, "1\n", 3);
            break;
        case 2:
            write(1, "2\n", 3);
            break;
        case 3:
            write(1, "3\n", 3);
            break;
        default:
            break;
    }

    return 0;
}

/*
* This function checks if the files are similar or similar by case     sensitive
* it gets 2 files, and returns: 3 if identical, 2 if identical but only if not
* case sensitive or 1 else.
*/

如何更改权限?

shay@shay-Latitude-E6410 ~/workspace/targ1OS $ gcc -c ec11.c -o      comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ls
comp.out  Debug  ec11.c
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out       /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: Permission denied
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
comp.out: command not found
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: Permission denied
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ^C
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ls -al ./comp.out
-rw-r--r-- 1 shay shay 2640 Mar 30 10:05 ./comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ chmod ogu+x ./comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: cannot execute binary file: Exec format error
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ gcc -c ec11.c -o comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: Permission denied
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ chmod ogu+x ./comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: cannot execute binary file: Exec format error
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
comp.out: command not found

【问题讨论】:

  • 运行sudo ./comp.out,也许吧?
  • 但是我不能在终端上使用命令。
  • @ShayChercavsky 您实际上是在将源代码编译为可执行程序,还是只是想运行源文件?或者您可能正在创建一个目标文件?您需要向我们展示您如何构建源代码来创建程序
  • @ShayChercavsky 然后展示我们你是如何做到的。 编辑您的问题以显示一个或多个命令,包括所有标志。
  • 顺便说一句,在不相关的说明中,您应该真正检查程序是否有两个参数,否则您将有 未定义的行为,因为您可能会使用 @987654331 @越界。检查例如if (argc &lt; 3) { printf("To few arguments, must have two\n"); return 1; }CheckSimilar 调用之前。

标签: c linux gcc permissions


【解决方案1】:

命令

gcc -c ec11.c -o      comp.out

创建一个目标文件,而不是一个可执行程序文件。目标文件是编译后的translation units(大致是包含所有头文件的源文件),仅此而已。是 -c 标志告诉编译器前端程序 gcc 创建一个目标文件,所以要么删除 -c 标志,要么显式链接。

所以

$ gcc ec11.c -o comp.out

或者

$ gcc -c ec11.c
$ gcc ec11.o -o comp.out

在不相关的说明中,我建议您在编译时添加警告标志,以便编译器向您提供更多关于运行时可能导致问题的警告(例如未定义的行为或逻辑/语义问题)。我个人至少使用标志-Wall -Wextra -pedantic

【讨论】:

  • 但现在我有一个不同的问题,它说 shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out input.txt input.txtCannot read input file
  • @ShayChercavsky 这是另一个问题的另一个问题。
猜你喜欢
  • 2016-01-15
  • 2011-08-20
  • 2014-06-21
  • 1970-01-01
  • 2022-11-22
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多