Tuesday, May 8, 2018

Sublime Text as a C and C++ compiler or editor on windows.

When using Sublime Text as a C/C++ compiler on Windows 10, I come over %PATH% problem. In the beginning, I didn't know how to set the path for g++ or gcc. Later on, I learned it, so I share it here in case you have the similar problem.

Open Control Panel desktop app, you can do it by typing "Control Panel" on the start menu or clicking on Settings on Start menu. After Settings, I clicked on Apps in Settings, after that, I clicked on "Programs and Features" on the right side, just under the Related Settings. There opened somewhat old looking Programs and Features control panel, there I clicked Control Panel home on the top-left side. After that, you are in the real, old school, cool control panel home. There I clicked on User Accounts, then again User Accounts and now you can see "Change my environment variables" on a down-left menu. Click on that, now you can set gcc, gnu  c++ path on environment variables. When you click on environment variables a window related to that will open. You will see "Path" there, click on that. Now click on the "Edit", then click on "New" and then "Browse". Now go to the "bin"  folder where is your Cygwin or MinGW or MSYS are installed, generally they are inside of C: drive.

You can also use a different way to reach to Environment variables. Control Panel Home -> System and Security -> System, now you'll see "Advanced System Settings" on the left side menu down there, you'll see "Environment Variables" on the "Advanced" section, click here and you can now edit the "Path."

In order to install Cygwin just google it or click here: https://www.cygwin.com/setup-x86_64.exe for 64-bit or here for 32-bit: https://www.cygwin.com/setup-x86.exe. And then install it.

You can download and install MinGW, just google it.

After installing them, set your environment variables to their bin folders. In m,y case it was like that.
You can check confirm if everything is OK, by writing "gcc --version", "g++ --version", "gdb --version", "gcc -v", "g++ -v" or "gdb -v" on Command Prompt. If you don't know how to open Command Prompt, then click Windows button + r, it will open Run, then write there cmd, it will open Command Prompt.

Now, you can open Sublime Text and run a simple hello world of C or C++, write the code, and  Tools -> Build or Ctrl + B, then it will run successfully. Well, you might be surprised that your Sublime Text only shows Finished in 0.1s or something like that, but there is no "hello world" written there. Open Command Prompt and go to your directory where is your code is saved and compiled, you'll see there "a.exe", write "a" to your cmd(Command Prompt). It will show the output on Command Prompt. If you don't know how to navigate in cmd, then write "pwd" it is a "print working directory", you can change the drive by writing the name of the desired drive for example, "D:". And then write "dir" to see a listing of files and folders on that folder or drive, then change your current location by writing "cd [folder-name-here]" for example, if you want to enter into my-codes folder then write "cd my-codes". Similarly, "cd .." is going one folder upwards or backwards.

For additional resources: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
and https://www3.ntu.edu.sg/home/ehchua/programming/howto/Cygwin_HowTo.html 

Edit on 08 May 2018: Some MinGW64 or MinGW32 may not install at some sites like Sourceforge, etc. So, in order to download and install them correctly, run MSYS2 MSYS on your computer, look at Start Menu, you'll see it there. Write "pacman -Sy pacman" in the shell, close it, re-open and write "pacman -Syu", close it, re-open and "pacman -Su". Now, you can install MinGW64 or MinGW32 packages correctly by "pacman -S something_that_you_want_to_install", for example "pacman -S mingw-w64-x86_64-toolchain"

Edit on 10 May 2018:  I assume GCC is a compiler for C and G++ is a compiler for C++. So, by now, you may have already installed those on your computer through Cygwin, MinGW or MSYS. I tried to use GNU Emacs as a C++ editor on Windows. I wrote a simple C++ program and compiled it by writing "M-x compile". Its default is "make -k", but you can change it. I deleted "make -k" and wrote "g++ -Wall -g -o hello-world.out hello-world.cpp" and it will compile via g++. "-Wall" prints all warning messages, "-g" generates additional symbolic debugging information for use with gdb debugger, "-o" specifies the output file. Default output file is a.exe, so after compiling in order to see your output you can enter to cmd(command prompt) via windows run and see the output by entering to the folder where your hello-world.cpp is located, because generally the executable output file a.exe will be there, and in order to see the output write "a" to the cmd. Or if you have chosen hello-world.out then you can write "hello-world.out" in cmd. Via Visual Studio Code editor, you can open powershell and you may follow almost similar way. In GNU Emacs in order to see the output, "M-x shell" and navigate to the directory where your output executable file is located, and then write the output file. By default you don't need to write exe extension because in Windows it is recognized automatically, I guess, but you need to write other output files, e.g. "hello-world.out", etc. That's all for now, if you have anything to mention please do so in the comments section. You may also correct some codes or ways provided in this blog, or may add your own knowledge. Thanks.