Create and Debug C/C++ Programs with Eclipse and Cygwin in Windows
In this post, I am going to demonstrate how to use Eclipse to create and debug C/C++ programs for Unix/Linux in Windows. I am going to use Cygwin GCC as toolchains. Cygwin GDB will also be installed for debugging purpose. I am using Windows 10 and JRE 1.8 in the following steps.
Install Eclipse IDE for C/C++ Developers
Before installing Eclipse, please install JRE first (https://java.com/en/download/manual.jsp).
And then go to Eclipse official site to download the installer.
Choose Eclipse IDE for C/C++ Developers in installation wizard. The version I installed is 4.5.2.
Install Cygwin
Cygwin is a large collection of GNU and Open Source tools which provide functionality similar to a Linux distribution on Windows. The functionality we will mainly use includes GCC (GNU compiler) and GDB (GNU debugger).
Install Cygwin by following the instructions in the following link:
You don’t need to select any package in the installation wizard as we will install the required ones later on. Please keep the setup file (setup-x86_64.exe) when exiting the wizard.
After installation, you will be able to use the Terminal as you would do in Linux/Unix.
Install Required Packages
Open one Command Prompt windows (using Administrator account) and run the following command line to install the packages.
setup-x86_64.exe -q -P wget -P gcc-g++ -P make -P diffutils -P libmpfr-devel -P libgmp-devel -P libmpc-devel –P gdb
In the above command line, –q means quite mode; –P to specify the package we need to install. The installer will resolve the dependencies automatically.
(I have installed these packages before and there are no details in the standard output.)
You can use the Terminal to validate whether those packages are installed successfully. For example, input “man gcc”.
Create C Project
Open Eclipse and create one C project. Remember to choose Cygwin GCC as Toolchains.
The perspective for C/C++ project looks like the following screenshot.
In this sample, I am print out one string “Hello World!” and also create one test file.
1: /*
2: ============================================================================
3: Name : CProgramTest.c
4: Author : Raymond Tang
5: Version :
6: Copyright : Your copyright notice
7: Description : Hello World in C, Ansi-style
8: ============================================================================
9: */
10:
11: #include <stdio.h>
12: #include <stdlib.h>
13:
14: int main(void) {
15: printf("Hello World!\n"); /* prints !!!Hello World!!! */
16:
17: /*Create a file*/
18: FILE *fp;
19:
20: fp = fopen("/home/Raymond/test.txt", "w+");
21: fprintf(fp, "This is testing for fprintf...\n");
22: fputs("This is testing for fputs...\n", fp);
23: fclose(fp);
24:
25: return EXIT_SUCCESS;
26:
27: }
Before you build the project, please ensure the project PATH environment includes the Cygwin paths (which includes the C standard libraries).
Build the project and then click Run button to run the program.
Once the program runs, one test file is created in my Cygwin user home folder.
Debug The C Program
As we already installed GDB, you can easily debug C/C++ program. For example, add breakpoints and variable watch list and etc.
Microsoft Visual C++
If you are targeting Windows platforms, you can choose Microsoft Visual C++ as Toolchains. In this way, you don’t need to install Cygwin but you have to install Visual C++ instead.
On the other hand, Visual Studio is definitely better tool to use for this case especially you are a Microsoft technology related developer.