maverick_GOD's blog

By maverick_GOD, history, 3 years ago, In English

I code in C++, I was a Windows user, recently switched to mac. I downloaded gcc-10 from homebrew but still, whenever I compile on any editor, it got compiled with Clang. Then I deleted Clang by going to the directory where it was stored, I thought it will then compile with GCC. But now it stops compiling and throwing error messages.

Q.Why do I wish to use gcc instead of clang? -> Clang doesn't have <bits/stdc++.h> header and while solving one question I got to know it doesn't have unordered map also (read on StackOverflow later). -> And I don't know what more is not there in clang.

Can anyone please tell me ========================= either how to successfully compile C++ using gcc on MAC or can tell me all the difference between clang and gcc and how should I manage.

Even I talked with apple care for about 45 mins and they also don't know the solution.

  • Vote: I like it
  • +8
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

I'm not sure about the case for M1. But if you type gcc-10 in your shell and it shows gcc instead of clang, you can try to append alias gcc='gcc-10' to the end of your ~/.bash_profile.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    Actually, this doesn't work for me but after experimenting lot of stuff, I got the solution and a better understanding. I will share that in a different blog.

    Thanks, for your reply:)

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I'm not sure about M1 too, but in MacOS with Intel processors you can install gcc with brew package manager (something like brew install gcc). After that you can use gcc-10 located at /usr/loca/bin/gcc-10.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You are correct only. For M1,gcc-10 is located at /opt/homebrew/bin/gcc-10 .

    Actually, there are many blogs and tutorials for Intel chips, and nothing available exactly for M1. I will upload one :)

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      brother, please upload the blog for M1. I am also facing the same problem.

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
  1. Install g++ through home-brew.
  2. Check out the version you just installed, probably 11th or 10th, (let's suppose it's the 11th version).
  3. Now, you can make a symbolic link from g++-11 to g++ (this is for being able to call g++-11 with just typing g++). In order to do it, just type in your terminal sudo ln -s $(which g++-11) /usr/local/bin/g++.
  4. You can do that also with gcc if you want to compile pure C code.

If you have another version of GCC, you should put it instead of 11th.

I've tested it in an intel MacBookPro; it should work in M1 too.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Just one correction, use this in the 3rd step /opt/homebrew/bin/gcc-10, instead of /usr/local/bin/g++ .

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      No. You didn't get it. /usr/local/bin/g++ is the path where I want to put g++. By other side, $(which g++-11) would be that path you mentioned.

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Actually, in M1 you need to make a different path, named /opt/homebrew/bin/gcc-10.

        Remaining is same as you said.

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Well, if you do exactly what I said, and then make sure you have /usr/local/bin/ in the PATH variable of your shell, then you should not worry about that different path you're saying.

          Just to make it clear:

          sudo ln -s pathSource pathDestination creates a symbolic link from pathSource to pathDestination.

          $(which g++-10) returns the path for the command g++-10, that in your case is /opt/homebrew/bin/g++-10. So, the command I typed would be equivalent to sudo ln -s /opt/homebrew/bin/g++-10 /usr/local/bin/, and that's perfectly fine because AFAIR /usr/local/bin/ is part of the PATH variable by default.

          • »
            »
            »
            »
            »
            »
            3 years ago, # ^ |
              Vote: I like it +3 Vote: I do not like it

            Ohh! I got your point.

            We can do it anyways, both will work. thanks!

      • »
        »
        »
        »
        11 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thank you very much! Do you, also, know how to force clangd-lsp to use gcc for diagnostics instead of clang?

        • »
          »
          »
          »
          »
          7 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I'm looking for the same! Did you find a solution?

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

First, you need to install HomeBrew from https://brew.sh/

Then, run brew install gcc in the terminal.

After installation completed, type g++-11 in the terminal to see if the installation is successful.

Finally, to use g++ instead of g++-11, you need to type the following 4 commands:

sudo ln -s /usr/local/bin/gcc-11 /usr/local/bin/gcc

sudo ln -s /usr/local/bin/g++-11 /usr/local/bin/g++

sudo ln -s /usr/local/bin/c++-11 /usr/local/bin/c++

sudo ln -s /usr/local/bin/cpp-11 /usr/local/bin/cpp

Now everything is OK.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Have you tried this method? The final commands are not working for me. Coz in my m1 mac, final destination of g++ file is /usr/bin instead of /usr/local/bin. Also operation to that directory is not permitted even after giving terminal full disk access.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hey,

I use the M1 version of CLion.

The steps to install gcc via homebrew are pretty much the same as they always were.

To use gcc instead of clang within CLion, you can go to CLion -> Preferences -> Build, Execution, Deployment -> Toolchains -> C++ Compiler, and select the gcc compiler you downloaded.

»
22 months ago, # |
  Vote: I like it 0 Vote: I do not like it

When compiling i simply give it a command of gcc-11, instead of just gcc.. This way it executes with the homebrew gcc verison

»
19 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Dear maverick_GOD and everone else who are using MAC, today I am gonna tell you the complete solution on how to use gcc compiler instead of using apple CLANG.

  1. Go to brew official website and copy the code below "Install Homebrew", it will be like /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

  2. Open Terminal from the launchpad and paste it in your terminal and press enter to run it.

  3. Now it will ask for the sudo password and then enter your mac password and then run it. Next it will ask press RETURN to continue and hit return. After then it will take take some time to download and install it on your mac.

  4. After some time, it will show: View this image. Then copy the first code and then run it and then copy the second code and then run it. If it doesn't run reopen the terminal and then run it. It will show something like this.

  5. Now reopen the terminal and type brew and press enter. It should show the list of all the brew commands

  6. Congratulations now you have successfully installed brew on your mac.

  7. Now its time to install GCC compiler in your MAC.

  8. Open terminal and type brew install gcc

  9. Now type g++-12 --version and it will show the current version of the gcc compiler installed.

  10. Now type g++ --version it will show apple clang instead of gcc. Something like this.

  11. That means the current default compiler is clang not gcc. So we have to set it to GCC compiler as the default compiler.

  12. Now type cd /opt/homebrew/bin, press enter and then type ln -s g++-12 g++ and press enter.

  13. Now again type g++ --version and it will show something like this. That means you have successfully installed gcc in your MAC and has been set as the default compiler.

I will be pleased if this comment is helpful to anyone. Please share this commment so if anybody is trying to install gcc can see this solution. Thank you. Have a nice day:)

  • »
    »
    16 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Thanks a lot bro :) Was very helpful. I was stuck with clang for months and could not use gcc. Also sometimes we've tried too many times , so in /opt/homebrew/bin, on doing ln -s g++-12 g++, we get output as ln: g++ already exists. So first I needed to remove already existing g++ file.

    for that on terminal, first i typed cd /opt/homebrew/bin , pressinng enter then i removed existing file by typing sudo rm g++ , pressed enter then I followed your steps from no. 12 from typing ln -s g++-12 g++ then enter And it worked ...

    Thanks again

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks it helped me

    • »
      »
      »
      7 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      thank you so much!! i wasn't aware that i had a g++ file already

    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      i already had g++ was so confused but your comment solved my problem alot of thanks to you

  • »
    »
    9 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    thank you!!

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    it was very helpful.. thanks!

»
15 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
  1. Install HomeBrew first.
  2. run brew install gcc in terminal.
  3. Type g++-11 to check if installation done succesfully.
  4. Now use those commands 1 by 1
  5. sudo ln -s /usr/local/bin/gcc-11 /usr/local/bin/gcc
  6. sudo ln -s /usr/local/bin/g++-11 /usr/local/bin/g++
  7. sudo ln -s /usr/local/bin/c++-11 /usr/local/bin/c++
  8. sudo ln -s /usr/local/bin/cpp-11 /usr/local/bin/cpp Hope it will help.
»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

When I try to write g++-11 in my terminal it shows "Command not found" What should I do now