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

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

·

2 min read

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.

For this guide I'll be installing gcc13.

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

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

Your window should now look something like this.

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

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;
}

Step 4

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

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

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}\""
        }
    ]
}

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.

Inside the file paste these lines:
https://drive.google.com/drive/folders/1jwe6XPP4riWKvzUubH2QyhHqRr7_ywdD

(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.