0

I have been having some trouble getting my gcc and g++ compiler to work on my mac (OSX Yosemite 10.10.2).

I have written up a simple "Hello World" program and even these seem to not work. The two program that I tried to run are

hello.c

#include <stdio.h>

int main()
{
    printf("Hello world\n");
    return 0;
}

hello.cpp

#include <iostream>

int main()
{
    std::cout << "Hello World";
}

I can compile the C program using cc hello.c and everything works fine, but when I do gcc hello.c I get this error

[1]    38508 segmentation fault  gcc hello.c

I get a similar error attempting to compile my C++ code

[1]    38596 segmentation fault  g++ hello.cpp

I did which gcc and I get /opt/local/bin/gcc and that directory is in my path. ( /usr/texbin /opt/local/bin /opt/local/sbin /bin /usr/sbin /sbin /usr/local/bin/usr/bin )

So I am confused as to what is happening. I thought I downloaded all of the Xcode things that I needed. I would like to get gcc and g++ running properly. I hope that you can help.

Thanks!

eitanlees
  • 1,244
  • 10
  • 14

2 Answers2

2

It seems that gcc and g++ have to be installed/added to the MAC os.

From your description, I would expect that the wrong version of those tools was installed.

This answer should help.

Be sure to read all the answers to the question before proceeding with a gcc installation.

Community
  • 1
  • 1
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • I used the PATH manipulation they suggested and it seems to have fixed my problem. export PATH="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:$PATH" – eitanlees Mar 26 '15 at 04:03
-1

I had a similar problem where even gcc --versionwas giving me a "Segmentation fault: 11". This is on OSX 10.10.5 with XCode 6.4. After much googling and no solution, I found that clang (Apple's LLVM-based C compiler) is intended to be a compatible replacement for gcc, so I just sym-linked gcc to clang as follows:

whence gcc #=> /usr/local/bin/gcc
whence clang #=> /usr/bin/clang
cd /usr/local/bin
sudo mv gcc gcc_OLD
sudo ln -s /usr/bin/clang /usr/local/bin/gcc
gcc -v
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

Now I am able to successfully compile c-language stuff, like my ruby extensions.

JESii
  • 4,678
  • 2
  • 39
  • 44