Why would clang++ fail to compile on a Mac, under Mavericks, except with sudo?

After a most recent software update on my mac, I'm not able to compile and link a c++ hello world program without sudo.

The program (helloworld.cpp):

#include <iostream>

int main(){
  std::cout << "hello worldn";
  return 0;
}

The invocation:

clang++ helloworld.cpp

Fails with error:

ld: can't write output file: a.out for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

But if I do this under sudo,

sudo clang++ helloworld.cpp

There's no problem.

What might be causing this, and how might I be able to resolve this?


EDIT, again : The answer turned out not to be working directory permissions, as a couple of people suggested, but the permissions associated with the output file, a.out, of my hello world program. Credit to Petesh for the solution.


You must be sitting in a directory which is not writable by your user. Look at pwd and ls -ld . to see where you are and what the permissions are there. Try also creating an empty file by touch foo.txt in the same directory where you ran Clang.


Most likely answer is you're running clang++ when your current working directory is not one you have permissions to write to.

Try ensuring that the directory is owned/writeable by you, by running eg:

sudo chown -R `whoami` .

(Note, this may not be appropriate depending on which directory you're in).

In some cases this happens after a OSX update/upgrade in projects that before was not necessary.


You probably ran gcc as root (via sudo), and thus the a.out file generated is owned by root. So just delete it and the problem will go away.

Why did you do that? Because annoyingly, xcode makes you run it that way to agree to the license agreement!

链接地址: http://www.djcxy.com/p/15144.html

上一篇: 在GCC中,Clang和MSVC有什么办法符合C ++ 98而不是C ++ 03?

下一篇: 为什么clang ++在Mavericks下无法在Mac上编译,除了使用sudo?