Guide : How to Setup C++ on MacOS with Sublime Text and gcc (without homebrew)
Tags: cpp, macos, programming, datastructures
Published: 2023-12-31 07:50:33

##Step 1

Install [MacPorts ](https://www.macports.org/install.php)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. ](https://ports.macports.org/search/)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tw0dpm4zotkig7lqk236.png)

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.](https://www.sublimetext.com/)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0dppg1ztgkqco3qxy9i.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhtyahys8gtjpxtzdmcu.png)

Your window should now look something like this.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kauqipkq8vui1rny431x.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9b6zgfinah2v6zfutlsb.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0sthdipz4b1ul8ywo27.png)


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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1p3p39mflu2are7ejkb.png)

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

(Credits to [take U forward](https://www.youtube.com/watch?v=Fql_b-xZYwQ))

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.