Undefined symbols for architecture x86_64

I recently upgraded from Mac OS Mavericks to Yosemite and in the process broke GCC. Suddenly all I got when trying to compile with g++ was this error message:

~/dev/tmp$ g++ asdf.cpp
Undefined symbols for architecture x86_64:
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

Just to be clear, the code I was trying to compile was nothing fancy:

int main()
    return 0;

In reality it wasn’t really the compilation that was the problem, but rather the linking. After spending hours on google without finding any similar cases I nearly gave up.

Before the upgrade I had installed GCC with Homebrew, and it was working fine. On my system g++ is symlinked to /usr/local/Cellar/gcc/4.9.2/bin/g++-4.9. I  tried reinstalling GCC, recompile from source and upgrade Xcode, but nothing worked. It was driving me crazy. Then I  discovered the doctor option for Homebrew which gave me a break:

$ brew doctor
Warning: You have MacPorts or Fink installed:

This can cause trouble. You don't have to uninstall them, but you may want to
temporarily move them out of the way, e.g.

  sudo mv /opt/local ~/macports

I didn’t ever remember that I had Macports installed, but here it was, potentially causing problems. To uninstall I ran the following command from their website:

sudo port -fp uninstall installed

And what do you know. It turned out that there was another version of GCC installed with macports. After getting rid of that everything suddenly worked as expected!

I don’t know why this suddenly happened after upgrading to Yosemite or why it worked back in Mavericks, but at I can finally compile stuff again. If you encounter the same problem, hopefully this helps you out.