Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 world\n";
  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.

like image 292
Rob Lachlan Avatar asked Oct 14 '14 04:10

Rob Lachlan


3 Answers

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 e.g.:

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.

like image 101
omnigrok Avatar answered Nov 14 '22 13:11

omnigrok


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.

like image 39
John Zwinck Avatar answered Nov 14 '22 11:11

John Zwinck


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!

like image 23
user3973 Avatar answered Nov 14 '22 11:11

user3973