Use the eclipse platform for C/C ++ development [reprint]

Source: Internet
Author: User
Tags pc world
Http://tech.ccidnet.com/pub/article/c980_a110331_p1.html

We will outline how to use the eclipse platform in C/C ++ development projects. Although eclipse is mainly a Java development environment, its architecture ensures support for other programming languages. In this article, you will learn how to use the C/C ++ Development Toolkit (C/C ++ development toolkit, CDT ), it is the best C/C ++ toolkit for eclipse.

C and C ++ are the most popular and commonly used programming languages in the world. Therefore, it is not surprising that the eclipse platform provides support for C/C ++ development. Because the eclipse platform is only a framework used for developer tools, it does not directly support C/C ++; it uses external plug-ins to provide support. This article will show you how to use CDT-a set of plug-ins for C/C ++ development. The CDT project is committed to providing the fully functional C/C ++ integrated development environment (IDE) for the eclipse platform ). Although this project focuses on Linux, it is available in all environments where GNU developer tools are available (including Win32 (WIN 95/98/ME/NT/2000/XP) QNX neutrino and Solaris platforms.

CDT is an open-source project fully implemented in Java (licensed according to the common public license) and serves as a set of plug-ins for the eclipse SDK platform. These plug-ins Add the C/C ++ perspective to the eclipse workbench, now, the latter can support C/C ++ development with many views and Wizard and advanced editing and debugging support.

Due to its complexity, CDT is divided into several components, all of which adopt the form of independent plug-ins. Each component operates as an independent project with its own set of submitter, error category, and email list. However, all plug-ins are necessary for the normal operation of CDT. The following is a complete list of CDT plug-ins/components:

  • The primary CDT plug-in (primary CDT plug-in) is the "Framework" CDT plug-in.
  • CDT function eclipse (CDT feature eclipse) is a CDT function component (feature component ).
  • The CDT core provides the core model, cdom, and core component ).
  • Cdt ui is the core UI, view, editor and wizard.
  • CDT launch provides a boot mechanism for external tools such as compilers and debuggers.
  • The CDT debugging core provides debugging functions.
  • The CDT debugging UI provides a user interface for the CDT debugging Editor, view, and wizard.
  • CDT debug Mi is an application connector used for the mi-compatible debugger.

    Now, let's look at how to use these components in real applications. Figure 1 shows the C/C ++ project in Eclipse:

    Figure 1. Edit the C/C ++ project in eclipse with the CDT plug-in

    Install and run CDT

    Before downloading and installing CDT, make sure that the gnu c compiler (gnu c compiler, GCC) and all accompanying tools (make, binutil, and GDB) are available. If you are running Linux, you only need to install the development package by using the Package Manager for your distribution. On Windows, you will need to install the cygwin toolkit. Cygwin is a UNIX-like environment for windows. It includes GCC porting and all necessary development tools, including automake and GNU Debugger (GNU Debugger, GDB ). Cygwin is built on the cygwin1.dll library. The alternative solution for cygwin is minimalist GNU for Windows (mingw ). This tool is a set of Windows-specific header files and imported/imported files that can be freely obtained and distributed for free, these header files and imported libraries are combined with the GNU tool set (which allows you to generate local Windows programs that do not depend on any third-party DLL. If you want to create a POSIX-compatible windows application, mingw is the best choice. Mingw can even work on cygwin installation. Solaris and QNX require you to download and install their specific GCC, GNU make binutils, and GDB ports from the Internet.

    Assume that you have installed the appropriate Java SDK/JRE and eclipse Platform SDK, and they all run properly. CDT can be used in either of the following ways: stable release and test run (nightly build ). The trial version has not been fully tested, but they provide more features and correct the current error. Before installation, check whether the previous version of CDT exists on the disk. If so, ensure that it is completely removed. Because CDT does not have an available uninstall program, you need to manually remove it. To check whether the previous version exists, go to the directory where the CDT plug-in resides: Eclipse/plugins. Then, remove all directories starting with org. Eclipse. CDT. The last thing to do is remove the CDT metadata directory or. Eclipse. CDT. * From workspace/. Metadata/. plugins and features .*.

    The next step is to download the CDT binary file. Note: Download the correct CDT suitable for your operating system. Unfortunately, even if the CDT is written in Java, It is not platform-independent. Next, extract the archive file to the temporary directory, and move all the plug-in contents from the temporary directory to the eclipse plugins subdirectory. You also need to move the content of the features directory to the eclipse features subdirectory. Now, restart eclipse. After eclipse is started again, the update manager will tell you that it has detected changes and ask if you are sure of these changes. Now you can see two available new projects: C and C ++.

    Create a new project

    After installing CDT in eclipse, browse to file => New => project, where you will find three new available project types: C ("Standard C make project ") c ++ ("Standard C ++ make Project") and "convert to C or C ++ projects ". Create source code files for your project from "standard make C ++ project. In the C/C ++ projects view, right-click and choose new => simple => file. Name your file and save it. You may use this method to create many header files and C/C ++ implementation code files. The last step is makefile. GNU make will use it to build binary files. Use the common GNU make syntax for this makefile (see references ). Remember: makefile requires that you use TAB characters instead of spaces to produce reduction.

    You usually import the existing source code to eclipse (see figure 2 ). CDT provides a convenient way to perform this operation: Use the import Wizard to copy files from the file system directory to the workbench. Go to the main menu bar and select file => Import => file system. Click Next to open the source directory and select the directory of the file you want to add. Click Select All to select all resources in the directory, and check from start to end. deselect the resources you do not want to add. Specifies the workbench project or folder to be imported as the target. You can also drag folders and files from the file system and put them into the navigator view, or import folders and files by copying and pasting them.

    Figure 2. Import existing source code to the CDT Project

    Key CDT ide Functions

    Cdt ide is built on the general extensible editor provided by the cdt ui plug-in. However, this module is still in the development stage, so it still lacks some important utilities, such as class browsers or language document browsers. The main functions of cdt ide are:

  • Syntax highlighted:CDT ide recognizes C/C ++ syntax and provides fully configurable code coloring and code formatting for syntax highlighting:

    Figure 3. syntax error mark highlighted after unsuccessful Compilation

  • Outline:The outline window module provides a quick view of the processes, variables, declarations, and functions that appear in the source code. With outline, you can easily find the appropriate reference in the source code, or even search for the source code of all projects.
  • Code assistance:This Code Completion function is similar to a feature that can be found in Borland C ++ builder or MS Visual Studio. It uses code templates and only helps avoid stupid syntax errors:

    Figure 4. Code assistance that helps you use the correct language syntax

  • Code Template:The Code Template used by the Code auxiliary function is the definition of the standard C/C ++ syntax structure. You can also define your own code template to expand your own shortcut keys, such as the shortcut keys for author or date keywords. In window => preferences => C/C ++ => code templates, you can add a new template and view the complete template list. You can also export and import templates as XML files.

    Figure 5. pre-defined C/C ++ Code Template

  • Code history:You can track local changes in the project Source Code even if you do not use CVs or other source code version management software. Right-click the selected file and choose compare with => local history... from the context menu ...:

    Figure 6. Use the local history function to check changes in the source code

    Build and run the project

    CDT provides an easy way to set project building options. CDT depends on three GNU tools: GCC, GDB, and make. Therefore, the dependencies between GDB for debugging or GCC and make for compilation require that these applications can be used on platforms that users want to use. Most Linux (usually and POSIX-like) source code packages use Autoconf scripts to check the Build Environment. Therefore, you must run the configure command, which creates a "makefile" before compilation ". CDT does not provide methods to edit Autoconf scripts, so you must write them manually. However, you can configure build options to call the configure command before compilation.

    If you call the make command to build a project, the default setting is correct. However, if you use a more complex method to build a project, you must enter the appropriate command (for example, make-F make_it_all) in the build command text box ). In the C/C ++ projects view, right-click the C/C ++ project and select rebuild project. All compilation messages from make, compiler, and linked programs are redirected to the console window:

    Figure 7. Console window with compiler output

    After compilation, you may want to run your application. All the options for running and debugging are located under the run menu of the main eclipse menu. However, the options used to run the project must be defined earlier. You can go to the run... in the main menu (where there are different profiles used to run the application... option to complete this step; for example, you can use a profile for testing purposes, and use another profile for running the final version. In addition, you can define the parameters you want to pass to the application, or you can set environment variables. Other options are used to set debugging options, such as which debugger (gnu gdb or cygwin GDB) is used ). Figure 8 shows the running profile being created for the project ).

    Figure 8. Create a running summary file for the project

    In the C/C ++ projects view, right-click your project and select Properties on the build settings tab, you can use more common options for building projects. These options mainly affect the occurrence of build stops due to a compilation error.

    Debug C/C ++ Projects

    CDT extends the standard Eclipse debug view to enable debugging of C/C ++ code. The debug view allows you to manage program debugging or running in the workbench. To start debugging the current project, as long as you switch to the debug view, you will be able to set (and change at any time during execution) Breakpoints/monitoring points in the Code and track variables and registers. The debug view displays the stack framework of the suspended threads for each target being debugged. Each thread in the program appears as a node in the tree, and the debug view displays the processes of each target running.

    Eclipse uses the CDT debugging mi (CDT debug mi) Plug-in (one of its components) to support a debugger compatible with machine interfaces (MI. But what is the MI debugger? Generally, third-party GUI debuggers such as DDD and xxgdb depend on the command line interface (CLI) of GDB for debugging ). Unfortunately, it is proved that this interface is very unreliable. GDB/MI provides a new machine-oriented interface, which is very suitable for programs that want to directly parse GDB output.

    Conclusion

    The C/C ++ Development Toolkit (CDT) provides a set of powerful plug-ins that help you develop C/C ++ applications using eclipse. Although CDT is still in the development stage, you can use its existing functions.

    About the author

    Pawel Leszek is a writer of Studio B. He is an independent software consultant and writer specializing in Linux/win/Mac OS system architecture and management. He has many experience in operating systems, programming languages and network protocols, especially Lotus Domino and DB2. Pawel is also the author of a series of articles on LinuxWorld and a Linux columnist for PC world in Poland. You can send an email (pawel.leszek@ipgate.pl) directly to the author ).

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.