Rob Lachlan Rob Lachlan - 1 year ago 49
C++ Question

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.

Answer Source

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.