Create and Debug C/C++ Programs with Eclipse and Cygwin in Windows

access_time 5 years ago visibility9206 comment 0

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.

https://eclipse.org/downloads/

Choose Eclipse IDE for C/C++ Developers in installation wizard. The version I installed is 4.5.2.

image

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:

https://cygwin.com/install.html

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.

image

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. 

image

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

image

Create C Project

Open Eclipse and create one C project. Remember to choose Cygwin GCC as Toolchains.

image

The perspective for C/C++ project looks like the following screenshot.

image

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

image

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.

image

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.

image

 

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.

info Last modified by Raymond 2 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to publish your article on Kontext?

Learn more

More from Kontext

visibility 1586
thumb_up 0
access_time 6 months ago

When I was building Hadoop 3.3.0 on Windows 10, I hit one error about OpenSSL: CMake Error at C:/Program Files/CMake/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:165 (message): Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable ...

visibility 8
thumb_up 0
access_time 9 days ago

Microsoft vcpkg is a C++ library managers for Windows, macOS and Linux. For .NET, nuget is the commonly used package manager; for Java,  Maven central and many other repositories are used by Maven or other build tools; for JavaScript, npm is a commonly used package manager and it is also the ...

visibility 3
thumb_up 0
access_time 8 days ago

To build Visual C++ projects on Windows that has dependency on Google protobuf library, we can use CMake. The executable of protobuf is not sufficient to compile C++ projects.  To specify a dependency on protobuf , the following line can be added into CMakeLists.txt file ...