So, I am off to new adventures. I have decided to start exploring the Eclipse IDE, while brushing up on some previous skills. For this series, I will explore the Eclipse IDE and utilize the command-line driven version of Git (previously only worked in the desktop and Visual Studio plug-in) to help my development of whatever it is I am going to work on.
This first post in the series will walk through how to set up Eclipse with Git for Windows. In future posts, Documentation and guide on setting up Eclipse: https://www3.ntu.edu.sg/home/ehchua/programming/howto/EclipseCpp_HowTo.html
Install Eclipse and basic preferences
Oxygen is the latest version, so I’ll guess we go with that. Upon start, Eclipse will ask where the workspace settings should be stored. For now, I use this standard path for my workspace.
We want to go to the perspectives button and make sure we have selected C/C++. Perspectives are pretty much different configurations of the layout suited for the specific languages you are working towards, whether it is C/C++ or markup languages such as XML.
On top of this, I am going to look at some additional settings.
1. In General > Workspace and have a look at the encoding at the bottom. By default, Eclipse uses the default platform encoding derived from the operation system settings. In my case, this is Cp1252. While UTF-8 is backward compatible with most western encodings, that is not the case for other encodings. There is a significant number of plain text files for which UTF-8 is not backward compatible.
2. In C/C++ > Editor > Folding, allow folding of preprocessor branches and control flow statements.
3. I do not know about you, but I am not a big fan of the white background…have never been. I would like to switch to a darker theme, much easier on the eyes.
Go to Help > Eclipse Marketplace > Search for Eclipse Color Themes and install it. Once it is installed go to Window > Preferences > General > Appearence > Color Theme. Here you now have a bunch of color themes to choose from, and you can add more by downloading them from http://www.eclipsecolorthemes.org/ or make custom changes yourself to create your own theme. That however, I am not going to go through now. I will go with the Obsidian theme and carry on.
Install compilers (Cygwin)
Documentation and guide on installing Cygwin: http://www3.ntu.edu.sg/home/ehchua/programming/howto/Cygwin_HowTo.html
Before we can actually do something, we have to download a compiler. GCC is a popular choice, so we go right ahead with that. On Windows, we could install either MinGW GCC or Cygwin GCC. It is recommended to pick MinGW if we are not sure, because MinGW is lighter and easier to install, to the price of having less features. I am more interested in the Cygwin, so we go with that.
First, we download the setup on its mother site. Select “Install Cygwin” and download the setup-x86_64.exe for 64-bit windows, setup-x86.exe for 32-bit.
- Select Install From Internet
- Select Directory (Default C:\Cygwin, avoid Program Files if possible due to the blank space)
- Choose Local Package Directory (I left it as default, normally the downloads folder in the user folder)
- Select Direct Connection and choose the mirror to download from (took the first available on the list)
- Now do not fall back over the chair as a new window pops up once the download is finished. It is time to select packages, and there are a few as you can see. A few. We must open the “Devel” category, or search immediately, and select the following packages:
- gcc and g++. Take most of them to be on the safe side.
- gdb (The GNU Debugger)
- make (The GNU version of the ‘make’ utility
- Install packages. If you missed any, you can always re-run the installation.
Once the installation is complete, we must add Cygwin to the PATH variable. If you have previously worked with Python, you know we must do the same for the interpreter in order to use it from the command prompt. Otherwise if this is a first time, this is generally how you do it:
Control Panel > System and Security > System > Advanced System Settings > Advanced Tab > Environment Variables > System Variables > Add “C:\cygwin64\bin;” in front of the existing PATH entry.
Now, was that it? Let us verify that. Go to the window command prompt and write “man gcc”.
If you see this, it means we have successfully installed Cygwin.
Despite having verified that Cygwin indeed is installed, there still might be trouble down the line. If the gcc and gcc g++ packages are installed with different versions, then we will notice that once we are trying to run our first program since the preprocessing will most likely fail. I used the 6.4.0 version, pick any that is available but just remember to be consistent with other gcc components. This happened to me the first time I made this setup and it was not so obvious at first, so really pay attention to that once you are selecting which version of the packages you would like to install.
Install Eclipse C/C++ Development Tool (CDT)
We go to Help > Install New Software > In the “Work with” drop-down, select the Eclipse version currently used. In my case at the moment this post is written, this is Oxygen. Now we will see a bunch of options appearing…we want to expand “Programming Languages” and check the “C/C++ Development Tools SDK”. Once that is installed, we can move on to create our first program. It is going to be a classic “Hello World” example.
Writing first C/C++ program in Eclipse
Go to File > New C/C++ Project > C++ Managed Build
In “Project Types”, select Executable with empty project
In “Toolchains”, select Cygwin compiler (or MinGW). Click next.
Check configuration for both Debug and Release. Click finish.
If we have unresolved inclusion error, it can be for two reasons that I am aware about:
- Broken Cygwin or MinGW installation, core packages are installed with different versions. The only way to solve this is to go back to the installation and check the package versions again.
- Incomplete include directories. This is not more complicated than in Visual Studios, see below for include paths.
Go to Project > Properties > C/C++ General > Paths and Symbols. Here you can add include paths. We need to set this up for both C and C++. Notice than I am using the 6.4.0 for the gcc, I will label this as
Add the following directories to “GNU C”, where
$CYGWIN_HOME is your Cygwin installed directory:
Then add the following directories to “GNU C++”:
Make sure to do this for both Debug and Release configuration. Now, add a new file and create a simple “Hello World” program. You can call it anything you want, just make sure it ends with the extension “.cpp”.
If we now test building this application, it should run.
Setup Git repository
The final thing I want to do before I wrap up this post is to setup a git repository. There are several options for this. I am going to try with the EGit Eclipse plugin, which makes the process really easy:
If you have not done that already, you must install git before proceeding.
1. Right click on the project, go to Team > Share Project. In the Configure Git Repository, you can enter the path where you want to store the local repository. I have already set up a project called Atlantis, but for the purpose of this guide I just copied it.
2. How do we now know it is shared? If it stands NO-HEAD in the same field as the project name in the outliner to the left, that means we have not committed anything yet. Also, you can view the Git Staging and Git Repositories windows by going to Window > Show View > Other > Git
3. The first thing we must do is to commit our project to our local repository. You can simply drag and drop your changed from the unstaged changes to staged changes. Write a message to inform what is included in this commit, then click Commit and Push.
4. Copy the url from your repository and insert it in the necessary field, then enter your login in the Authentication section. Click next.
5. Select the branch you want to push to and click next. Go to your github repository online and check that everything has been pushed.
That is all for now, this is the basic setup for Eclipse on Windows. Hopefully this small guide can clear out some confusions when starting to work in the Eclipse IDE for the first time. If you have previously worked with Visual Studio, it is not much more complicated. Next time, I will start coding on some smaller projects and maybe get some graphics up on the screen.