Developing programs in the Eclipse environment

Eclipse is an open-source, Java-based integrated development environment (IDE). With Eclipse it is possible to develop programs in various languages. The advantage of using Eclipse shows when developing demanding programs that use complex libraries requiring detailed knowledge of how they work and of the class hierarchy. Most libraries, including commercial ones, also have add-ons that make them easier to use with added help and wizards. Advantages show even with ordinary libraries, since Eclipse reads the header files and can expand on the fly and provide help for the arguments of the functions we use. It also has various built-in code browsers and debugger support. Various add-ons also provide improved integration with version control services (subversion, git, perforce, …) or web services (gForge, …)

Views:

1. Code editor

2. Folder and file browser

3. Perspective selection

4. Console

5. Run and debug

The two main groups of components in Eclipse are:

- perspectives

- views

1 Perspectives

A perspective is the environment in which the Eclipse screen currently operates. Generally speaking, each language has a different perspective: the C/C++ perspective, the Java perspective, the Python perspective, etc. There is also a debug perspective, or more precisely, one for each language.

Perspectives are add-on packages that are usually installed separately from Eclipse. A new perspective can be selected viaWindow | Open Perspective , or more quickly by selecting it on the right side of the toolbar if it is there, or by clicking the Open a Perspective icon at the same location.

2 Views

A perspective contains a number of sub-windows, the views. For example, for C/C++ code we would be in the C/C++ perspective while editing and running our code. We would have a browser for folders, files and projects, an editor view for each file currently being edited, an outline view showing all the main components of the code (functions, global variables, etc.), a console view where ordinary input/output takes place, etc. While working on C/C++ code we would occasionally also use the debugger, in which case we would temporarily switch to the Debug perspective, which also has a number of views. Most language perspectives include a navigator view where projects are listed. For C/C++ it is called C/C++, for Python Pydev Package Explorer, for Java Package Explorer, etc. The general browser called Navigator can also be used.

Views are often crowded and too small. To enlarge them, drag their borders horizontally or vertically, or click the maximize iconMaximize , which becomes the Restore icon, with which we can return to the previous state.

If a view is no longer on the screen, it can be brought back by selecting Window | Show View and then clicking the name of the view.

To reset the views within a perspective to their initial configuration, use Window | Reset Perspective.

3 Basic procedures

The following are the steps for creating, developing and debugging a project. Note that in certain cases there are special details for individual languages.

  • create a project (or select an existing one); see “Projects” below

  • create source code files in the Eclipse editor, or copy them from other files on the system; see “Editing files” and “Importing files” below

  • build (i.e. compile) the project, if appropriate for the language

  • if there are syntax errors, they will appear in the console view, in the editor, etc.; clicking on an error shows the line where it occurs in the editor.

  • The first time we run/debug our program, we need a run and debug configuration; it specifies the name of the executable (and the project it belongs to), any command-line arguments, its shell environment variables, the selected debugger, etc.; create both a run and a debug configuration, in this order:

    • create a run configuration:

      • select Run | Open Run Dialog

      • right-click to select the language, for example: C/C++ Local Applications, and select New

      • choose a name

      • select the Main tab and enter the name of the run configuration (Name), the project name and the executable name

      • to enter command arguments and environment settings, select the Arguments or Environment tab.

      • select Apply and Close to finish the run configuration

    • create a debug configuration:

      • start with Run | Open Debug Dialog

      • Eclipse may fill in certain data from the run configuration; if not, enter them yourself

      • select Apply and Close to finish the debug configuration

4 Tips for C/C++

  • the CDT plugin must be installed; by clicking Window | Open Perspective check whether C/C++ is in the list; it may also be under Other, right-click the arrow next to “C/C++”; if CDT is not there, see “Installing plugins” below.

  • when opening a new run/debug dialog, right-click C/C++ Local Applications and select New

  • check Connect process input and output to a terminal if we have terminal I/O

  • to compile:

    • the make file was created automatically when the project was set up; if we want to add something in the project’s ** Debug** directory, for example a link to a library, right-click the project name, select Properties, open the triangle at C/C++ Build, select Settings | Tool Settings; open the triangle at GCC C Linker, select Libraries | Add and set the library settings with –l as in –lm.

    • save the files

    • select Project | Build Project

5 Projects

As with most IDEs, Eclipse organises things into projects. A project is a directory within our workspace; if Eclipse was installed with defaults, it is located in the workspace directory inside the Eclipse directory.

To create a new project:

  • select File | New | Project

  • select the language (the triangle may need to be turned downwards)

  • choose a project name

  • if working in C/C++, select Executable

  • select Finish

To work on a project, existing or newly created, double-click the project name in the browser view. If it is not visible, switch the language perspective to our language.

6 Editing files

To create a new file that is a copy of a file outside the project, it can be imported with the Import command, see below. To create a new file using the Eclipse editor, or to modify an existing one, the procedure is as follows.

Eclipse tries to find an editor associated with the file type (.c, .py etc.), preferably part of the plugin for the language we are working in. It also performs other functions related to the file type.

To create a new file:

  • right-click the project name, select File | New | Source File for C/C++, File | New | File for Python or Java, File | New | Perl File for Perl, etc.

  • make sure the project name in Source Folder is correct; enter the file name; select Finish

  • the editor will now open an editing window for z.c; the file will also appear in the project

To edit existing files:

  • go to Navigator (or C/C++ Projects, for C/C++)

  • if the file does not appear, click the arrow at the project name

  • double-click the file name

To save modified files, right-click the displayed file and select Save, or use the shortcut ctrl-s.

Other operations:

  • copy-and-paste: on Linux it works with the usual mouse clicks, e.g. select by dragging with the left button, then paste with the middle button; on other platforms the text must be selected and then Edit | Copy and Edit | Paste

  • indentation: select the part of the text to be shifted left/right and then Edit | Shift Right/Left; the default amount of indentation can be set under Window | Preferences, where we select our language

  • if we move the mouse over a variable or function, Eclipse opens a pop-up with its definition

  • the editor has certain features related to the selected language, for example for C/C++, if we select a part of the text, right-click and select Comment, that part of the text will be commented out

7 Importing files

Files from other folders can be imported into our project as follows:

  • File | Import

  • The triangle next to General must point downwards; select File system and Next

  • select Browse and the folder containing our file

  • fill in the Into Folder field with the name of our project

  • select Finish

  • our new file will be visible in the navigator (the project may need to be expanded)

8 Debugging

Keep in mind that the Debug perspective will be different for each language. However, the common points are as follows:

  • for debugging we must be in the Debug perspective

  • to run in Debug, select Run | Debug or the shortcut

  • to set a breakpoint, double-click the margin to the left of the source code

  • to make a breakpoint conditional, right-click the Breakpoint symbol to the left of the desired line and select Breakpoint Properties… | Common

  • to remove or temporarily disable a breakpoint, right-click the Breakpoint symbol to the left of the desired line; then Toggle to remove it and Disable/Enable to enable it

  • if we want a breakpoint to apply only in certain cases, right-click the Breakpoint symbol to the left of the desired line and select Breakpoint Properties… | Filter; uncheck the cases in which we do not want the breakpoint.

  • breakpoints are preserved from session to session

  • the current source code line (the next line to be executed) is highlighted in green; at startup this will be the first line in main() (or a similar location depending on the language); in other words, a breakpoint is automatically set there.

  • to continue execution (or to start, if we are on the first line), click the resume icon on the left side of the debugger toolbar.

  • the usual step-in/step-over/step-return operations are available

  • the Variables view is very useful:

    • this view shows the values of our variables

    • local variables are shown according to the current location

    • global variables are not shown by default, which can be changed by right-clicking and selecting Add Global Variables

    • links of pointer variables can be followed by clicking the triangle next to the variable; this can be done repeatedly for a linked structure, etc.

    • if a pointer points to a dynamically created array, the whole array (or a sub-array) can be displayed by right-clicking the variable and selecting Display As Array

    • the value of a variable can be changed by right-clicking and selecting Change Value

  • the Breakpoints view helps manage all breakpoints

  • to restart the program, first click theRestart icon in the Debug toolbar

  • if we want to stay in the debugger and pause the program, click the Pause icon in the Debug toolbar; clicking the Terminate icon would require starting a new debug session.

  • in the Debug perspective we still have the code editor, where we can make changes as if we were in the development perspective. Compiling cannot be done directly; we must return to the development perspective.

9 Outline view

While editing, the Outline view shows where the main objects are in our code, for example: functions, classes, etc. Clicking one of them makes the editor jump to where it appears in the code. We can do the same for debugging if we want a breakpoint at a certain function.

10 Error messages

  • a frequently reported error is stopped due to shared libraries; in this case several debug sessions may be open; they can be removed with Terminate All Launched, or each individually by clicking the red square

11 Resetting

The following reset mechanisms can help:

  • to clear the Console and Problems views, etc., select Project | Clean | Clean Selected Projects

  • to close everything within Eclipse while still remaining in Eclipse, select File | Close All

Source: http://heather.cs.ucdavis.edu/~matloff/eclipse.html

Razvoj programov v okolju Eclipse

Eclipse je odprtokodno okolje za razvoj programov (IDE), ki temelji na Javi. Z Eclipse je mogoče razvijati programe v različnih jezikih. Prednost uporabe Eclipse se pokaže pri razvoju zahtevnih programov, ki uporabljajo kompleksne knjižnice za katere je potrebno podrobno poznavanje delovanja in hierarhija razredov. Večina knjižnic, tudi komercialnih, ima tudi dodatke, ki omogočajo lažjo uporabo z dodano pomočjo in čarovniki. Tudi pri običajnih knjižnicah se pokažejo prednosti, saj Eclipse prebere zaglavne datoteke (header) in zna sproti razširjati in pripraviti pomoč za argumente funkcij, ki jih uporabljamo. Vgrajeno ima še različne brskalnike kode in podporo za razhroščevalnike. Z različnimi dodatki se da doseči tudi izboljšano integracijo s servisi za nadzor kode (subversion, git, perforce, …) ali spletnimi servisi (gForge, …)

Pogledi:

1. Urejevalnik kode

2. Brskalnik po mapah in datotekah

3. Izbiranje perspektiv

4. Konzola

5. Run in debug

Glavni skupini komponent v Eclipse sta:

- perspektive

- pogledi

1     Perspektive

Perspektiva je okolje pod katerim zaslon Eclipse trenutno deluje. Splošno rečeno imamo za vsak jezik drugačno perspektivo, C/C++ perspektivo, Java perspektivo, Python perspektivo, itd. Obstaja tudi debug perspektiva, ali bolj natančno rečeno, za vsak jezik svoja.

Perspektive so paketi dodatkov, ki jih običajno naložimo ločeno od Eclipse. Novo perspektivo lahko izberemo, če izberemoWindow | Open Perspective , ali hitreje, če jo izberemo na desni strani v orodni vrstici, če se tam nahaja, oz če kliknemo na  Open a Perspective ikono na isti lokaciji.

2     Pogledi

Perspektiva vsebuje določeno število podoken, poglede. Naprimer, za C/C++ kodo bi bili v C/C++ perspektivi, medtem ko bi urejali in zaganjali svojo kodo. Imeli bi brskalnik po mapah, datotekah in projektih in pogled z urejevalnikom kode za vsako datoteko, ki bi jo trenutno urejali, pogled povzetek, ki bi prikazoval vse glavne komponente te kode (funkcije, globalne variable, itd.), konzolni pogled, kjer bi se izvajal običajni input/output, itd. Med delom na C/C++ kodi, bi občasno uporabili tudi debugger, v tem primeru bi začasno preklopili na Debug perspektivo, ki bi tudi imela določeno število pogledov. Večina jezikovnih perspektiv vključuje pogled navigator, kjer so navedeni projekti. Za C/C++, je imenovan C/C++, za Python je imenovan Pydev Package Explorer, za Javo Package Explorer ,itd. Lahko pa uporabimo običajen brskalnik imenovan Navigator.

Pogledi so pogosto natlačeni in premajhni. Če jih želimo povečati, povlečemo njihove meje horizontalno oz. vertikalno, ali kliknemo ikono za maksimiziranjeMaximize , ki postane ikona Restore, s katero se lahko vrnemo v prejšnje stanje.

Če se pogled več ne nahaja na našem ekranu, ga lahko nazaj prikličemo, tako da izberemo Window | Show View in potem kliknemona ime pogleda.

Če želimo znotraj perspektive poglede resetirati v njihovo začetno konfiguracijo, to storimo z Window | Reset Perspective.

3     Osnovni postopki

Sledijo koraki za ustvarjanje, razvoj in debugging projekta. Omeniti je potrebno, da so v določenih primerih posebne podrobnosti za posamezne jezike.

  • ustvarimo projekt  (ali izberemo obstoječega); glej “Projekti” spodaj

  • ustvarimo datoteke z izvorno kodo v Eclipse editorju, ali jih prekopiramo iz drugih datotek na sistemu; glej “Urejanje datotek” in “Uvoz datotek” spodaj

  • zgradimo (oz. compilamo) svoj projekt, če je to primerno za ta jezik

  • če so prisotne napake sintakse, se bodo te pojavile v konzolnem pogledu, v urejevaniku, itd; kliknemo lahko na napako in pokazala se nam bo vrstica, kjer se ta pojavi v urejevalniku.

  • Prvič ko zaženemo/debuggamo svoj program, potrebujemo run and debug konfiguracijo; ta navaja ime zagonske datoteke (in v katerem projektu se nahaja), njene morebitne argumente ukazne vrstice, njene variable glede na okolje lupine, izbrani debugger, itd; naredimo tako run kot debug konfiguracijo, v tem vrstnem redu:

    • naredimo run konfiguracijo:

      • izberemo Run | Open Run Dialog

      • z desnim klikom izberemo jezik, naprimer: C/C++ Local Applications, in izberemo New

      • izberemo ime

      • izberemo zavitek Main in vnesemo ime svoje run konfiguracije (Name), ime projekta in zagonske datoteke

      • če želimo vnašati ukazne argumente in nastavitve okolja, izberemo zavihek Arguments ali Environment.

      • izberemo Apply in Close in s tem končamo run konfiguracijo

    • naredimo debug konfiguracijo:

      • začnemo z  Run | Open Debug Dialog

      • Eclipse lahko vnese določene podatke iz run konfiguracije, če ne jih sami

      • izberemo Apply in Close in s tem končamo debug konfiguracijo

4     Napotki za C/C++

  • imeti moramo inštaliran CDT plugin, s klikom na  Window | Open Perspective preverimo, če je  C/C++ na seznamu; lahko se nahaja tudi pod  Other desni klik na puščico zraven “C/C++”; če se CDT tam ne nahaja, glej  »Inštalacija pluginov« spodaj.

  • ko odpremo nov run/debug dialog, desno kliknemo na C/C++ Local Applications in izberemo New

  • preverimo Connect process input and output to a terminal če imamo terminal I/O

  • za compile:

    • make datoteka je bila avtomatično ustvarjena ob času nastavitve projekta, če želimo kaj dodati v ** Debug** direktoriju projekta, naprimer povezavo na knjižnico, desno kliknemo na ime projekta, izberemo Properties, odpremo trikotnik pri C/C++ Build, izberemo Settings | Tool Settings; odpremo trikotnik pri GCC C Linker in izberemo Libraries | Add in nastavimo nastavitve knjižnic z –l v –lm.

    • shranimo datoteke

    • izberemo Project | Build Project

5     Projekti

Kot pri večini IDEjev, tudi Eclipse ureja stvari v projekte. Projekt je direktorij znotraj našega workspace-a; če smo Eclipse inštalirali privzeto, se nahaja v workspace direktoriju znotraj Eclipse direktorija.

Če želimo ustvariti nov projekt:

  • izberemo File | New | Project

  • izberemo svoj jezik (trikotnik bomo morda morali usmeriti navzdol)

  • izberemo ime projekta

  • če delamo v C/C++, izberemo Executable

  • izberemo Finish

Če želimo delati na projektu, obstoječem ali na navo ustvarjenem, dvojno kliknemo na ime projekta v brskalnem pogledu. Če tega ne vidimo, zamenjamo jazikovno perspektivo v naš jezik.

6     Urejanje datotek

Če želimo ustvariti novo datoteko, ki je kopija datoteke zunaj projekta, jo lahko uvozimo z Import ukazom, glej spodaj. Če želimo ustvariti novo datoteko s pomočjo Eclipse urejevalnika, ali spemeniti obstoječo je postopek sledeč.

Eclipse poizkuša najti urejevalnik, ki se navezuje na vrsto datoteke (.c, .py itd.), po možnosti del plugina za jezik v katerem delamo. Opravlja tudi druge funkcije, ki se navezujejo na vrsto datoteke.

Da ustvarimo novo datoteko:

  • desno kliknemo na ime projekta, izberemo File | New | Source File za C/C++, File | New | File za Python ali Javo, File | New | Perl File za Perl, itd.

  • prepričamo se da je ime projekta v Source Folder pravilno; vnesemo ime datoteke; izberemo Finish

  • urejevalnik bo sedaj odprl okno za urejanje z.c; prav tako se bo datoteka pojavila v projektu

Za urejanje obstoječih datotek:

  • gremo v Navigator (ali C/C++ Projects, za C/C++)

  • če se datoteka ne pojavi, kliknemo puščico pri imenu projekta

  • dvojno kliknemo na ime datoteke

Če želimo spremenjene datoteke shraniti, desno kliknemo na prikazano datoteko in izberemo Save, ali pa uporabimo bljižnico ctrl-s.

Ostale operacije:

  • copy-in-paste: na Linux-u, deluje z običajnimi kliki miške, recimo: označimo z vlečenjem z levo tipko, nato prilepimo z srednjo tipko; na ostalih platformah je potrebno tekst označiti in nato Edit | Copy in Edit | Paste

  • odstavki: izberemo del besedila, ki ga moramo premakniti levo/desno in nato Edit | Shift Right/Left; privzeto količino zamika lahko nastavimo pod Window | Preferences,kjer izberemo naš jezik

  • če premaknemo miško na mesto variable oz. funkcije, bo Eclipse odprl okence z definicijo tega dela

  • urejevalnik bo imel deločene značilnosti navezujoč se na izbrani jezik, naprimer za C/C++, če izberemo del teksta desno kliknemo in izberemo Comment,  bo ta del teksta zajemal komentar

7     Uvoz datotek

V svoj projekt lahko uvozimo datoteke iz drugih map tako:

  • File | Import

  • Trikotnik poleg  General mora biti usmerjen navzdol, izberemo File system in Next

  • izberemo Browse ter mapo, ki vsebuje našo datoteko

  • vnesemo polje Into Folder z imenom našega projekta

  • izberemo Finish

  • v navigatorju bo vidna naša nova datoteka (morda bo potrebno razgrniti naš projekt)

8     Debugging

Moramo se zavedati, da bo Debug perspektiva drugačna za vsak jezik. Vendar sledijo skupne točke:

  • za debuging moramo biti v  Debug perspektivi

  • za zagon v Debug, izberemo Run | Debug ali bližnjico

  • za nastavitev breakpoint-a, dvojno kliknemo na rob, levo od izvorne kode

  • da breakpoint naredimo pogojen, desno kliknemo na simbol za Breakpoint, levo od želene vrstice, in izberemo Breakpoint Properties… | Common

  • za odstranitev ali trenutno onemogočenje breakpoint-a, desno kliknemo na simbol za Breakpoint, levo od želene vrstice; nato Toggle za odstranitev in Disable/Enable za omogočanje

  • če želimo, da breakepoint velja samo v določenih primerih, desno kliknemo na simbol za Breakpoint, levo od želene vrstice, in izberemo Breakpoint Properties… | Filter; odklikamo primere v katerih ne želimo breakpoint-a.

  • breakpoint-i bodo ohranjeni od seje do seje

  • trenutna vrstica izvorne kode (naslednja vrstica, ki bo pognana) bo označena z zeleno; pri zagonu bo to prva vrstica v main() (ali podobni lokaciji glede na jezik); z drugimi besedami, tam bo avtomatično postavljen breakpoint.

  • za nadaljevanje zagona (ali za začetek, če smo na prvi vrstici), kliknemo na resume ikono  na levi strani debugger orodne vrstice.

  • na voljo so običajne step-in/step-over/step-return operacije

  • pogled Variables je zelo uporaben:

    • ta pogled prikazuje vrednosti naših variabel

    • lokalne variable so prikazane glede na trenuteno lokacijo

    • globalne variable privzeto niso prikazane, kar lahko spremenimo z desnim klikom in izbiro Add Global Variables

    • povezavam pointer variablah lahko sledimo, če kliknemo na trikotnik poleg variable, to lahko naredimo večkrat če imamo povezan sklop, itd.

    • če pointer kaže na dinamično ustvarjen array, lahko prikažemo celoten array (ali pod array) z desnim klikom na variablo in izbiro Display As Array

    • vrednost variable lahko spremenimo z desnim klikom in izbiro Change Value

  • pogled Breakpoints pomaga urediti vse breakpoint-e

  • za ponoven zagon programa najprej kliknemo ikonoRestart v  Debug orodni vrstici

  • če želimo ostati v debuggerju in zaustaviti program, kliknemo ikono Pause v Debug orodni vrstici, s klikanjem na ikono Terminate bi morali zagnati novo debug sejo.

  • v Debug perspektivi imamo še vedno urejevalnik kode, kjer lahko izvajamo popravke, kot če bi bili v development perspektivi. Compile ne moremo izvajati neposredno, ampak se moramo vrniti v development perspektivo.

9     Outline pogled

Med urejanjem nam bo Outline pogled prikazoval, kje v naši kodi so glavni objekti, naprimer: funkcije, razredi, itd. S klikom na enega od njih bo urejevalnik skočil na mesto, kjer se ta pojavi v kodi. Podobno lahko delamo za debugging, če želimo breakpoint pri neki funkciji.

10 Sporočanje  napak

  • pogosto javljena napaka je stopped due to shared libraries; v tem primeru imamo morda odprtih več debug sej; z Terminate All Launched jih lahko odstranimo, ali pa vsakega posebej s klikom na rdeč kvadratek

11 Resetiranje

Pomagamo si lahko z naslednjimi reset mehanizmi:

  • za zbris Console in Problems pogledov, itd., izberemo Project | Clean | Clean Selected Projects

  • da zapremo vse znotraj Eclipse, a še vedno ostanemo v Eclipse, izberemo File | Close All

Vir: http://heather.cs.ucdavis.edu/~matloff/eclipse.html