Eclipse 4.4.1 (Luna) for C + + programming

Source: Internet
Author: User
Tags gdb debugger

Eclipse 4.4.1 (Luna) for C + + programming directory

      • Eclipse 441 Luna for CC programming
        • Directory
        • Eclipse 441 Luna Installation cc development TOOLCDT
        • Write the first CC program in Eclipse
          • C Program
          • C Program
          • C Program with Makefile
        • Read the documentation
        • Debug CC programs in Eclipse CDT
        • Eclipse Usage Tips
          • Using GCC for CC development
        • The file IO in Eclipse
        • Resources

Eclipse is an open source integrated development environment (IDE). The official website is here. Eclipse is primarily used for Java development. It can also support the development of C + +, PHP, Python, Perl, and other Web projects in a plug-in way. Eclipse is also a cross-platform development environment that can run on Windoes, Linux and Mac OS.

View English text

Eclipse 4.4.1 (Luna) installs the C + + development Tool (CDT)
  • Step 0: Install MinGW gcc or Cygwin gcc

    If you want to use Eclipse for C/D + + development, you first need a C + + compiler. On Windows, you need to install MinGW gcc or Cygwin gcc. If you're not sure where to go, choose MinGW, because it's lighter and easier to install, but there are fewer features to support.

    1. MinGW gcc: Read the MinGW Installation Guide
    2. Cygwin gcc: Read the Cygwin Installation Guide. Make sure that gcc , g++ , gdb , and make< are selected in the Devel (development) directory at the time of installation. /strong> These packages because the default installation is not selected for these packages.
  • Step 1: Install the Eclipse C + + development Tool (CDT)

    There are two ways to install the CDT, depending on whether the eclipse was previously installed on this machine:

    ol>
  • If you have previously installed an eclipse for Java developers or other Eclipse installation package, you will need to install the CDT plugin as follows

    Open Eclipse = Help + Install New software = in W Ork the drop-down box of the With , select Luna-http://download.eclipse.org/releases/luna (or Eclipse 4.3 select Kelper; Or Eclipse 4.2 select Juno; or ecplise 3.7 select Helois).
    in the Name check box, expand programming Language = Check C + + development tools = Next = Finish .

  • If you have not previously installed any eclipse, you can download the Eclipse IDE for C + + developers here and unzip to the selected folder.

  • Step 2: Configure

    As long as the bin folder of Cygwin or MinGW is sufficient in the system's PATH environment variable, no additional configuration is required. The CDT will find the C + + compiler by searching the PATH.

Writing the first C + + program in Eclipse C + + programs
  • Step 0: Open Eclipse

    1. Running Eclipse.exe in the Eclipse installation directory
    2. Choose the right directory for your workspace (i.e. where you want to put the code)
    3. If the welcome Word appears, click Close
  • Step 1: Create a C + + project

    For each C + + application, you need to create a project to hold all source codes (sources), object files (. o files), Execu Table files (executables) and relevant resources (resource files).
    steps to create a C + + project:

    1. Select File menu = New + Project ... = c/c++ = C + + project
    2. Popup C + + Project dialog box +
      in project name box, enter firstproject and
      in the Project Types check box, select executable = Empty Project +
      in Toolchains check box, select the compiler, such as Cygwin gcc or MinGW GCC = Next
    3. popup Select Configurations Dialog box, select Debug and Release = Finish .
  • Step 2: Write a Hello-world C + + program

    1. in Project Explorer (leftmost panel) =&G T Firstproject Right (or click the File menu) + = New = Source file
    2. pops up New Source file dialog box
      1. in the Source file box, enter hello.cpp
      2. Click Finish
    3. source file Hello.cpp appears in the Panel. Enter the following code:
#include <iostream>usingnamespacestd;int main() {   cout"Hello, world!" << endl;   return0;}
If unresolved inclusion error occurs

If unresolved inclusion errorappears, the description include paths for headers is not set correctly.
Select the Project menu? Properties ? C + + general? Paths and Symbols ? In the includes tab:

  • For Cygwin GCC :

    1. ADD The following directories to GNU C , $CYGWIN _home is Cygwin GCC installation directory:
      • $CYGWIN _home\lib\gcc\i686-pc-cygwin\4.5.x\include
      • $CYGWIN _HOME\LIB\GCC \i686-pc-cygwin\4.5.x\include-fixed
      • $CYGWIN _home\usr\include
      • $CYGWIN _home\usr\include\w32api
    2. Add The following directory to GNU C + + , $CYGWIN _home is the CYGWIN GCC installation directory:
      • $CYGWIN _home\lib\gcc\i686-pc-cygwin\4.5.x\include\c++
      • $CYGWIN _home\lib\gcc\i686-pc-cygwin\4.5.x\include\c++\i686-pc-cygwin
      • $CYGWIN _home\lib\gcc\i686-pc-cygwin\4.5.x\include\c++\backward
      • $CYGWIN _home\lib\gcc\i686-pc-cygwin\4.5.x\include
      • $CYGWIN _home\lib\gcc\i686-pc-cygwin\4.5.x\include-fixed
      • $CYGWIN _home\usr\include
      • $CYGWIN _home\usr\include\w32api
  • For MinGW GCC :

    1. ADD The following directories under GNU C , $MINGW _home is the MINGW GCC installation directory:
      • $MINGW _home\lib\gcc\mingw32\4.6.x\include
      • $MINGW _home\include
      • $MINGW _home\lib\gcc\mingw32\4.6.x\include-fixed
    2. ADD The following directories under GNU C + + , $MINGW _home is the MINGW GCC installation directory:
      • $MINGW _home\lib\gcc\mingw32\4.6.x\include\c++
      • $MINGW _home\lib\gcc\mingw32\4.6.x\include\c++\mingw32
      • $MINGW _home\lib\gcc\mingw32\4.6.x\include\c++\backward
      • $MINGW _home\lib\gcc\mingw32\4.6.x\include
      • $MINGW _home\include
      • $MINGW _home\lib\gcc\mingw32\4.6.x\include-fixed

Note : In order to find the path to the header file, you need to search for header files such as stdio.h (c) and iostream (c + +) in the Cygwin or MinGW installation directory.

Note : If you load a shared library while the link is in the error while loading shared libraries errors, please libmpfr4Cygwin >

    • Step 3:compile/build

      In project Explorer (the leftmost panel), Firstproject right (or click the Project menu) = + Select Build proje CT Compilation links the entire program.

    • Step 4:run

      In Project Explorer (leftmost panel) = + Firstproject right (or click on the run menu) = + Run as = = Local C + + Application = (if prompted, choose Cygwin's GDB debugger)? In the Console panel, output the result Hello, world!

Note : You need to create a C + + project for each program. This is for the toy program (refers to the small program usually written), it is more annoying!

C Program

Similar to the previous steps, create a C project (instead of C + + project). Then compile the link to run the Hello-world program (hello.c).

#include <stdio.h>int main() {   printf("Hello, world!\n");   return0;}
C + + program with Makefile

In the previous example, we used the so-called Managed-make Way to build the program (in this way Eclipse will automatically generate a makefile). We can also manually write makefile to control the entire build process.

    • Step 1: Create a C + + Makefile Project

      File menu? New ? Project ... ? C + + ? C + + project ? In the Project name box, enter hellocppmakefile ? In the project type box, select Makefile Project , Empty project ? Toolchains box, select Cygwin gcc or MinGW gcc . Ignore the warning message.

    • Step 2: Write a C + + program

      Right-click a project and New ? Source File ? In the Source file box, enter Hello.cpp ? Enter the following code:

#include <iostream>usingnamespacestd;int main() {    cout"Hello, world!" << endl;    return0;}
    • Step 3: Writing makefile

      Right-click a project and the New = File = + file name box, enter makefile = and enter the following code. It is important to note that indentation must be Tab rather than Spaces

all: Hello.execlean:    rm Hello.o Hello.exeHello.exe: Hello.o    g++ -g -o Hello.exe Hello.oHello.o: Hello.cpp    g++ -c -g Hello.cpp
    • Step 4:build Project

      Right-click project? Build Project

    • Step 5:run The program

      Right-click project? Run as? Local/C + + application

[Unfinished] Write a makefile for a number of toy programs under a project

Read the documentation

At the very least, you should browse Eclipse's Workbench User Guide and C through Eclipse's Welcome page or Help menu /c++ Development UserGuide. This will avoid many annoying problems in the future and save you a lot of valuable time.

Debugging a C + + program in Eclipse CDT

Learning to debug a program in a debugger with a graphical interface is an important skill. It will save you a lot of time, so you don't have to guess where the bug is.

    • Step 0: Write a C + + program

      The following program calculates and prints the factorial of n (=1*2*3*...*n). However, this program has a logic error, and when n = 20 it will come up with a wrong value ("The factorial of is-2102132736"-Are you sure? A negative number?! ”)

#include <iostream>using namespace STD;intMain () {intn = -;intfactorial =1;//n! = 1*2*3...*n    for(inti =1; I <= N;   i++) {factorial *= i; }cout<<"the factorial"<< N <<"is"<< factorial << Endl;return 0;}//OutputThe factorial of -IS-2102132736

We use the debugger to debug this program.

  • Step 1: Set breakpoints

    breakpoints can cause the program to Hang in a number of times without going down, so you can check the internal state of the program (for example, the value of a variable, etc.). Before you start debugging, you need to set at least one breakpoint in your program. Double-click on the left blank bar of the edit panel to break the point. A small blue circle indicates that the breakpoint was set successfully.

  • Step 2: Start commissioning

    Right-click the project (or tap the Run menu)? Debug as? Local/C + + application ? Select Yes to switch to Debug view (the view is for a specific development task, such as editing or debugging, but also the layout of the tool panel). The program starts running and then pauses at the breakpoint .
    As shown, certain (the line indicated by the blue arrow) indicates the statement to be executed in the next step.

  • Step 3: Step through, view variables and outputs

    Click the Step over button (or choose Stepover from the run menu) to perform a single complement operation. Each step, check the value of the variable (in the Variable panel) and the output of the program (in the Console panel), if necessary, you can put the mouse on any variable, look at the contents of the variable.
    stepping, viewing variables and outputs This is the Ultimate Secret of program debugging-because it shows exactly how the program runs on the computer.

  • Step 4:breakpoint, Run-to-line, Resume and Terminate

    As mentioned earlier, breakpoints Pause the program's operation and let you check the internal state of the program. Double-click on the left side of a row to set breakpoints (or select Toggle breakpointunder the Run menu).

    Resume lets the program continue running until the next breakpoint, or the end of the program.
    Stepping through a large number of cycles is time-consuming, where you can set a breakpoint outside the loop and click Resume to end the loop.

    Alternatively, you can put your cursor on a special line of statements and then click run-to-lineon the run menu to have the program run directly to this line.

    Terminate button to end this commissioning. Try to end this debug every time by Terminate or Resume to the end of the program.

  • Step 5: Cut back to the C + + view

    In the upper-right corner, click the C + + view icon to cut back to C + + view (or Window menu?) Open perspective ? C + +).

    In a word, debugging is important to explore more features of the debugger!

  • Other Debugger ' s Features

    Modify the variable value: You can enter a new value in the Variable panel to modify the value of the variable. In the case of not modifying the source code, this temporary modification of the way the program behaves is very convenient.

    Step-into and Step-return: To debug a function, you need to use the step-into button to enter the first statement of the function. You can also use the step-return button to exit the function at any point in the function and return to the caller. Also, you can set breakpoints inside the function.

Note : If the Can ' t find a source file at/cygdrive/c ...is present during the debugging process, then you need to /cygdrive/c and c:/ Do a mapping between (assuming your program is under C-disk):
Window ? Preferences ? C + + ? Debug ? Common Source Lookup Path, select Add ? Path Mapping

Eclipse Usage Tips

Read the Java version of Eclipse usage tips. All the same.

using GCC for C/D + + development
  • You can view the commands for the build project in the Eclipse CDT under $workspace \.metadata\.plugins\org.eclipse.cdt.ui\global-build.log.

  • set Include-paths, library-paths, libraries: When build (build) a program, the compiler needs a header file to compile the source file ; The linker requires a library file to resolve external references. The compiler searches for include-paths to find #include the header file that the directive contains. The linker searches library-paths to find the libraryies (library) required by the program.

    In GCC,include-path is specified by the -idir option or the environment variable cpath . Library-path is specified by the -ldir option or the environment variable library_path . Library file libxxx.a specified by the -lxxx option (lowercase ' l ', without lib prefix and . a extension)

    In the Eclipse CDT, you can set up include Paths, library paths , and libraries:

    Right-click Project ? Properties ? C + + general? Paths and Symbols ? Under includes , the Library Paths and Libraries tabs, this setting only works for the selected project.

    If you want to set the include Paths and library pathsfor all projects, set the environment variables cpath and Library_path

    Select the Window menu? Preferences ? C + + ? Build ? Environment.

    Alternatively, you can copy your head files and library files to the system directory so you don't have to worry about include-paths and library-paths .

  • OpenGL with GLUT: For Cygwin, you need to install GCC, g++, GDB, make (in the Devel directory) as well as OpenGL, Freeglut (in the graphics directory). Header files Gl.h, glu.h, Glut.h are under $cygwin\usr\include\w32api\gl. Use the #include <GL/gl__.h> include header file. library file libopengl32.a, LIBGL u32.a and libglut32.a are both in the $cygwin \lib\w32api. To use these libraries when linking, use the -lopengl32-lglu32-lglut32 option (no Li b prefix and. a extension).

  • OpenGL with SDL: Download SDL from here. Choose Development Libraries ? Win32 ? mingw32 ? Unzip. Copy header file directory SDL (in the Include directory) to $cygwin\usr\include\w32api; Copy all library files under the Lib directory to $cygwin\lib\w32api; Copy the run-time library file SDL.dll (in the bin directory) to Windows/system32. In your program, the following two header files are included: #include \<SDL/sdl.h\> and#include <SDL/sdl_opengl.h>

    To link through, add the following two libraries:-lsdl-lsdlmain.

  • #pragma note Directive : pragma annotation directives, such as #pragma comment (lib, "Opengl32.lib"), are often used to contain specific libraries (as with the-l option). This platform-dependent directive is available on Windows machines, but not in GCC.

  • multiple target patterns error message: GNU make ($Cygwin \bin\make.exe) has a problem. Use this instead of covering.

  • fflush (stdout): GCC libraries Use cached I/O (consistent with the specification of C). Only the output will be written to the output stream if only one line break is available. You can use the Fflush () function to flush the cache immediately, writing to the output stream.

  • the project name should not contain "Setup": If your C + + project name contains the word setup , then the resulting file ***setup***.exe will trigger UAC (User Authorization Control) and requires administrator privileges to run (it took me a few hours to figure it out!) You can try to change the hello.exe to mysetup.exe.

File I/O in Eclipse

Please read the file I/O in the Java version of Eclipse.

Resources
    1. The Eclipse Development Tool UserGuide is visible through the Help menu of Eclipse.
    2. GCC (GNU compilers) website; GCC Brochure
    3. An Introduction to GCC

Eclipse 4.4.1 (Luna) for C + + programming

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.