DEV Community

Cover image for Guide : How to Setup C++ on MacOS with Sublime Text and gcc (without homebrew)
DracoRX
DracoRX

Posted on

Guide : How to Setup C++ on MacOS with Sublime Text and gcc (without homebrew)

Step 1

Install MacPorts for your respective version of MacOS.
Installation process is as simple as installing the .pkg file and opening it.

Step 2

Search for the version of gcc you wish to install.

Image description

Do as it says and copy the command given and paste it in the terminal. The installation process will take a few minutes, but it's much faster than installing from homebrew.

Step 3

Install Sublime Text.
Create a new file with cmd + N and save it as test.cpp (or anything you want).

Go to View -> Layout -> Colums: 3

Image description

Go to View -> Groups -> Max Columns: 2

Image description

Your window should now look something like this.

Image description

Now save test.cpp, input.txt and output.txt in the respective sections as shown in the image.

Image description

Paste this code as your main function. This will take you inputs from input.txt and print the outputs in output.txt

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin); 
    freopen("output.txt","w",stdout);
    #endif

    //start your code from here

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Step 4

Now for the last step we will create a build system.

Go to Tools -> Build System -> New Build System

Image description

Now open finder and find the path where gcc was installed. Usually it is installed in /opt/local/bin. (use cmd + G)

Now search for the unix executable file. It will look like g++-mp-(version you installed)

In the case of this guide it is g++-mp-13

Now copy the file path. In the case of this guide : /opt/local/bin/g++-mp-13

Go back to Sublime Text. And copy and paste the given code. Replace "/opt/local/bin/g++-mp-13" if you have a different file path.

Now save with cmd + S. Name the file to whatever you like (ex - C++14) but make sure the file extension is .sublime-build.

{
    "shell_cmd": "/opt/local/bin/g++-mp-13 \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "/opt/local/bin/g++-mp-13 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Now go back to you C++ code and use cmd + shift + B to build and run the code.

bits/stdc++.h

If you wish to use bits/stdc++.h follow these steps.
Go to finder and use cmd + G and paste /Library/Developer/CommandLineTools/usr/include

Inside the folder create "bits" folder and inside that folder create a stdc++.h file.

Image description

Inside the file pastethese lines

(Credits to take U forward)

Now save the file. Now you can use bits/stdc++.h

Thanks For Reading

This is my first article. Please give feedback. I appreciate positive and negative feedback.

Top comments (4)

Collapse
 
pauljlucas profile image
Paul J. Lucas

If you install Apple's Command Line Tools, you don't need to install gcc yourself. The whole point of CLT is to install the entire development environment except the XCode GUI app. And you shouldn't need to copy any stdc++.h files around.

Collapse
 
dracorx profile image
DracoRX

When I tried to do this for myself a while back I couldn't find any good tutorial. That's why I made this.
Anyways thanks for the comment (You're the first comment I ever received). Also Happy New Year!

Collapse
 
pauljlucas profile image
Paul J. Lucas

I couldn't find any good tutorial.

That might be because it’s too trivial to need a tutorial. You install either XCode or the Command Line Tools and you’re done. Doing either makes /usr/bin/gcc (and g++) “just work.” There is no step 2. So if anybody follows your tutorial, they’d be doing the unnecessary.

Copying system files around like that tends to break whenever you do a software update.

FYI, I also don’t use Homebrew. I find it annoying that it installs stuff in /usr/local — which is meant for your own local stuff that you install manually, not a 3rd-party package manager. MacPorts (that I also use) gets it right by installing in /opt/local.

Thread Thread
 
pauljlucas profile image
Paul J. Lucas

I should have mentioned that the "gcc" that Apple installs is really clang in drag. (I don’t know why Apple doesn’t just call it clang.) So if you want a genuine gcc, then, yes, you can install it via MacPorts.