C语言 权限被拒绝在 OS X (10.8.2) Mountain Lion 上执行任何 gcc 编译的 c 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15396823/
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-09-02 05:41:33  来源:igfitidea点击:

Permission denied executing any gcc-compiled c program on OS X (10.8.2) Mountain Lion

cxcodemacosgccpermission-denied

提问by Michael I

When executing any C program that I have compiled with gccfrom the terminal, I get a permission denied error.

在执行我gcc从终端编译的任何 C 程序时,出现权限被拒绝错误。

To start, I have verified and repaired permissions on my drive (before doing this, the same problem was happening).

首先,我已经验证并修复了驱动器上的权限(在此之前,发生了同样的问题)。

To illustrate and isolate the problem, I'll show you what happens with this ultra-simple Hello World program (with other programs, the same thing occurs):

为了说明和隔离问题,我将向您展示这个超简单的 Hello World 程序会发生什么(对于其他程序,也会发生同样的事情):

#include <stdio.h>

main()
{
    printf("Hello World");
}

Now, I save this to my desktop as helloworld.c. At this point, from the desktop, an ls -lreturns:

现在,我将其作为helloworld.c. 此时,从桌面ls -l返回:

total 8
-rw-r--r--  1 michael  staff  56 Mar 13 14:08 helloworld.c

I then compile with gcc -c helloworld.c -o helloworld(I've also tried compiling without the -oflag with the same results). No warnings or errors. An ls -lnow returns:

然后我编译gcc -c helloworld.c -o helloworld(我也试过在没有-o标志的情况下编译,结果相同)。没有警告或错误。一个ls -l现在的回报:

total 16
-rw-r--r--  1 michael  staff   56 Mar 13 14:08 helloworld.c
-rw-r--r--  1 michael  staff  724 Mar 13 14:16 helloworld.o

Attempting to execute the output of gcc, with ./helloworld.oreturns:

尝试执行 gcc 的输出,./helloworld.o返回:

-bash: ./helloworld.o: Permission denied

Just for the sake of debugging, if I execute with sudo (sudo ./helloworld.o), it returns:

只是为了调试,如果我用 sudo( sudo ./helloworld.o)执行,它返回:

sudo: ./helloworld.o: command not found

Now, if I attempt to set the executable flag using chmod +x helloworld.o, as was recommended on a lot of search results I've found, ls -lreturns:

现在,如果我尝试使用 设置可执行标志chmod +x helloworld.o,正如我发现的许多搜索结果所推荐的那样,ls -l返回:

total 16
-rw-r--r--  1 michael  staff   56 Mar 13 14:08 helloworld.c
-rwxr-xr-x  1 michael  staff  724 Mar 13 14:16 helloworld.o

However, attempting to execute with ./helloworld.onow returns:

但是,尝试使用./helloworld.onow执行会返回:

-bash: ./helloworld.o: Malformed Mach-o file

Now, for debugging sake, where gccreturns:

现在,为了调试,where gcc返回:

/usr/bin/gcc

So, you can see that I am not using a third party gcc.

所以,你可以看到我没有使用第三方gcc

Does anyone know what could be the problem? I've tried searching around, but I couldn't find any working solutions. I was having this same problem earlier and have since freshly reinstalled OS X (for different reasons) and am still having this problem with a clean and organized development environment. For reference, I'm on OS X 10.8.2 and have Xcode 4.6 along with the latest version of Xcode Command Line tools (from Apple's developer website). I have not installed gcc from Homebrew or from any other third party source; it is the gcc that came with Xcode Command Line tools.

有谁知道可能是什么问题?我试过四处寻找,但找不到任何可行的解决方案。我之前也遇到过同样的问题,并且自从重新安装了 OS X(出于不同的原因)之后,在干净有序的开发环境中仍然遇到这个问题。作为参考,我在 OS X 10.8.2 上使用 Xcode 4.6 以及最新版本的 Xcode 命令行工具(来自 Apple 的开发者网站)。我没有从 Homebrew 或任何其他第三方来源安装 gcc;它是 Xcode 命令行工具附带的 gcc。

Thank you very much for your help! I really appreciate you taking your time to read, diagnose and offer your help. :)

非常感谢您的帮助!我非常感谢您花时间阅读、诊断和提供帮助。:)

回答by teppic

You're trying to execute object code (helloworld.o) rather than compile and link an executable binary. Don't use the -coption. Run this instead:

您正在尝试执行目标代码 (helloworld.o) 而不是编译和链接可执行二进制文件。不要使用该-c选项。运行这个:

$ gcc helloworld.c -o helloworld

It will be created with executable permissions. You can then run.

它将使用可执行权限创建。然后就可以跑了。

$ ./helloworld

as normal.

像平常一样。



这样做的原因是该-c-c选项告诉编译器只是compile编译文件,但不link链接它。需要链接过程来创建最终的可执行文件。如果您不提供-c-c,gcc 将在幕后为您编译和链接。