Sie sind auf Seite 1von 12

Using the C/C++ Tool Collection Descriptor - NetBeans IDE 6.

9 Tutorial
Contributed by Alexander Simon Alexander Simon and maintained by Susan Morgan September 2010 [Revision number: V6.9-1] Contents

y y y y y y y

Introduction Tool Collections Editing Tool Collection Descriptors Creating a Custom Tool Collection Details for the Tool Collection XML File Creating a Custom Compiler Provider Creating a Custom Compiler Error Handler

To follow this tutorial, you need the following software. Software NetBeans IDE (including C/C++ support) Java Development Kit (JDK) C and C++ compilers, make, gdb Version Required Version 6.9 Version 6 C/C++ Tool Collections Tested with NetBeans IDE

See the NetBeans IDE 6.9 Installation Instructions and Configuring the NetBeans IDE for C/C++/Fortran for information about downloading and installing the required software.

Introduction
Do you want to change compiler flags that the NetBeans IDE uses by default for compiling C/C++ files? Do you want to add a compiler that is not supported by the NetBeans IDE? This article can help you to do this! This tutorial demonstrates how to edit existing tool collections or create a custom tool collection in NetBeans IDE 6.9.

Tool Collections

A tool collection is a set of compilers, make utility, and debugger. Every time you compile, make, or debug your code, the NetBeans IDE runs an executable file described in the tool collection descriptor. Code Assistance features use include paths and macros taken from the tool collection. The tool collection descriptor is an xml file located in the module
${NB}/cnd/modules/org -netbeans-modules-cnd-toolchain.jar in the folder org/netbeans/modules/cnd/toolchain/resources/toolchaindefinition/ . You

should not edit these original NetBeans tool descriptor xml files. This article tells you how to create duplicates that you can edit.

Editing Tool Collection Descriptors


Editing a tool collection descriptor is a bit tricky since the NetBeans IDE does not provide a GUI for editing descriptors. But here we describe how to make dupluicate "shadow" copies of the tool collection descriptors to enable you to edit them to change the way the IDE uses the tools. 1. Exit the IDE if it is running. 2. Add the following flag to the netbeans_default_options entry in the file ${NB}/etc/netbeans.conf :
-J-Dcnd.toolchain.personality.create_shadow=true

3. Start the IDE, choose Tools > Options, and select C/C++ in the top pane of the Options window. The IDE creates "shadow" versions of the tool collection descriptors in your user directory:
${userdir}/config/ CND/ToolChain

The list of descriptors created should be similar to the following:


Cygwin.xml Cygwin_4.x.xml SunStudioExpress.xml GNU.xml GNU_mac.xml GNU_solaris.xml Interix.xml SunStudio_12.xml MinGW.xml MinGW_TDM.xml SunStudio.xml SunStudio_10.xml SunStudio_11.xml SunStudio_12. SunStudio_8.x SunStudio_9.x

4. You can edit the descriptor for the toolchain you are using, and the IDE will use the descriptor after you restart. See the Details for the Tool Collection XML File for information about the tags used in these descriptor XML fies. Remove the -J-Dcnd.toolchain.personality.create_shadow=true flag before you start IDE next time. The IDE needs only once to create the shadow descriptors.

Creating a Custom Tool Collection


You can create your own tool collection. The following example creates a new NetBeans module that contains a new tool collection. For general information about creating NetBeans modules, see NetBeans Plugin Quick Start.

1. Make sure your NetBeans installation includes the NetBeans Plugin Development module by choosing Tools > Plugins and clicking the Installed tab. If the NetBeans Plugin Development module is not installed, click the Available Plugins tab and install the module. You can find it quickly by typing "plugin" in the Search box. 2. Create a new NetBeans module by choosing File > New Project. On the Choose Project page of the New Project wizard, select the category NetBeans Modules and the project Module in the first step of the wizard. Click Next. 3. On the Name and Location page, type the project name, for example mytoolchain and leave all other fields as is. Cick Next. 4. On the Basic Module Configuration page, type the code name base, for example org.myhome.mytoolchain , and select the check box Generate XML Layer. Click Finish. The IDE creates a new project. 5. Find the file layer.xml in the Projects window, under the Source Packages node. Double-click the layer.xml file to open it in the editor. 6. Copy and paste the following text inside the filesystem tag:
7. <folder name="CND"> 8. <folder name="Tool"> 9. <file name="GNU_tuned_flavor" url="toolchain/GNU_tuned_flavor.xml"> 10. <attr name="extends" stringvalue="GNU_flavor"/> 11. </file> 12. <file name="GNU_tuned_cpp" url="toolchain/GNU_tuned_cpp.xml"> 13. <attr name="extends" stringvalue="GNU_cpp"/> 14. </file> 15. </folder> 16. <folder name="ToolChains"> 17. <folder name="GNU_tuned"> 18. <attr name="position" intvalue="5000"/> 19. <attr name="SystemFileSystem.localizingBundle" stringvalue="org.myhome.mytoolchain.Bundle"/> 20. <file name="flavor .shadow"> 21. <attr name="originalFile" stringvalue="CND/Tool/GNU_tuned_flavor"/> 22. </file> 23. <file name="c.shadow"> 24. <attr name="originalFile" stringvalue="CND/Tool/GNU_c"/> 25. </file> 26. <file name="cpp.shadow"> 27. <attr name="originalFile" stringvalue="CND/Tool/GNU_tuned_cpp"/> 28. </file> 29. <file name="fortran.shadow"> 30. <attr name="originalFile" stringvalue="CND/Tool/GNU_fortran"/> 31. </file> 32. <file name="assembler.shadow"> 33. <attr name="originalFile" stringvalue="CND/Tool/GNU_assembler"/> 34. </file> 35. <file name="scanner.shadow"> 36. <attr name="originalFile" stringvalue="CND/Tool/GNU_scanner"/> 37. </file> 38. <file name="linker.shadow">

39.

<attr name="originalFile" stringvalue="C ND/Tool/GNU_linker"/> 40. </file> 41. <file name="make.shadow"> 42. <attr name="originalFile" stringvalue="CND/Tool/GNU_make"/> 43. </file> 44. <file name="debugger.shadow"> 45. <attr name="originalFile" stringvalue="CND/Tool/GNU_debugger"/> 46. </file> 47. <file name="qmake.shadow"> 48. <attr name="originalFile" stringvalue="CND/Tool/GNU_qmake"/> 49. </file> 50. <file name="cmake.shadow"> 51. <attr name="originalFile" stringvalue="CND/Tool/GNU_cmake"/> 52. </file> 53. </folder> 54. </folder> 55. </folder>

56. Open the Bundle.properties file and add the following string: 57.
CND/ToolChains/GNU_tuned=My GNU Tuned Tool Collection Create the subpackage toolchain by right-clicking the mytoolchain package

under Source Packages and selecting New > Java Package. 58. Create a new file in the toolchain package by right-clicking the toolchain subpackage and selecting New > Empty File. Name the file GNU_tuned_flavor.xml and click Next. 59. Copy and paste the following text in GNU_tuned_flavor.xml :
<?xml version="1.0" encoding="UTF -8"?> <toolchaindefinition xmlns="http://www.netbeans.org/ns/cnd toolchain -definition/1"> 62. <toolchain name="GNU_tuned_flavor" display="GNU_tuned" family="GNU" qmakespec="${os} -g++"/> 63. <platforms stringvalue="linux,sun_intel,sun_sparc"/> 64. </toolchaindefinition> 65. Create another new file in the toolchain package by right-clicking the toolchain subpackage and selecting New > Empty File. Name the file GNU_tuned_cpp.xml and click Next. 66. Copy and paste the following text in GNU_tuned_cpp.xml : 67. <?xml version="1.0" encoding="UTF -8"?> 68. <toolchaindefinition xmlns="http://www.netbeans.org/ns/cnd toolchain -definition/1"> 69. <cpp> 70. <compiler name="g++"/> 71. <development_mode> 72. <fast_build flags=""/> 73. <debug flags=" -g3 -gdwarf-2" default="true"/> 74. <performance_debug flags=" -g -O"/> 75. <test_coverage flags=" -g"/> 76. <diagnosable_release flags=" -g -O2"/> 77. <release flags=" -O2"/> 78. <performance_release flags=" -O3"/> 79. </development_mode> 80. </cpp> 60. 61.

81.

</toolchaindefinition>

Notice the debug flags are set to -g3 and -gdwarf-2, which are different from the flags set in the default GNU tool collection description. The project tree looks like the following:

82. In the Projects window, right-click the mytoolchain project node and choose Run. The module is built and installed in a new instance of the IDE, which is the default target platform of modules. The target platform opens so that you can try out the new module. 83. In the running module, choose Tools > Options, select C/C++ from the top pane of the Options window, and select the Build Tools tab. 84. If the new tool collection (GNU_tuned) is not shown, click Restore Default. Click Yes to continue when prompted to rescan your environment. The IDE shows the new tool collection:

85. Create a new C/C++ sample project Welcome by selecting File > New Project > Samples > C/C++ > Welcome. 86. Right-click the project node and choose Properties. In the Project Properties dialog box, select the Build node, set the Tool Collection to your GNU_tuned tool collection and click OK. 87. Build the project. Note that the compiler has flags -g3 -gdwarf-2
g++ -c -g3 -gdwarf-2 -MMD -MP -MF build/Debug/GNU_tuned -Solarisx86/welcome.o.d -o build/Debug/GNU_tuned -Solaris-x86/welcome.o welcome.cc

88. In the Project Properties dialog box, select the GNU tool collection and click OK. 89. Build the project again, and compare the compile line in the output window:
g++ -c -g -MMD -MP -MF build/Debug/GNU -Solaris-x86/welcome.o.d o build/Debug/GNU -Solaris-x86/welcome.o welcome.cc

So in the GNU_tuned tool collection, we have managed to change compiler flags for debug, Development Mode for the GNU compiler. If you want to use the new module for the GNU_tuned tool collection in your regular IDE, you can create a binary (a .nbm file) and add it as a plugin: 1. Right-click the mytoolchain module project and select Create NBM. The .nbm file is created in the build subdirectory of the project, which you can see in the Files tab. 2. Select Tools > Plugins, then click the Downloaded tab in the Plugins dialog box. 3. Click Add Plugins, navigate to the build directory, select the module .nbm file, and click Open. The plugin module is added to the list in the Downloaded tab. 4. Click the checkbox to select the module in the Downloaded tab, then click the Install button. The NetBeans Plugin Installer opens. 5. Click Next to advance through the installer, and continue until the installer is finished. 6. Restart the IDE, and then choose Tools > Options, select C/C++ from the top pane of the Options window, and select the Build Tools tab. 7. If the new tool collection (GNU_tuned) is not shown, click Restore Default. Click Yes to continue when prompted to rescan your environment.

Details for the Tool Collection XML File


This is a description of most important tags in the tool collection xml file. For the scheme of all supported tags and attributes of toolchain xml files, you can refer to the file toolchaindefinition.xsd in the NetBeans IDE 6.9 source tree. Tool collection definition tags Tags toolchain name display family platforms stringval ue Attribut es Description Name of tool collection Name of tool collection Display name of tool collection Group name of tool collection Supported platforms List of supported platforms separated by comma. Possible values are:
y y

linux unix

y y y y y

sun_intel sun_sparc windows mac none

makefile_write r class drive_letter_pr efix

Custom makefile writer. Class name of custom makefile writer. It should implement org.netbeans.modules.cnd.makeproject.spi.configurations.Ma kefileWriter. Special prefix for file names

stringval "/" for unix ue "/cygdrive/" for cygwin on Windows Container for base_folder tags. base_folders One or more base_folder tags are contained in one base_folders tag. Description of base directory for compilers. base_folder This tag can contain the following tags: Windows registry key of the tool. Note that the XML tag regestry must be spelled "regestry" although this is a mispelling. Regular expression that allows NetBeans IDE to find pattern compiler in registry suffix Folder with executable files Regular expression that allows NetBeans IDE to find path_pate compiler by scanning paths. Note that the XML tag must be rn spelled "path_patern" although this is a mispelling. Container for command_folder tags. command_fold One or more commander_folder tags are contained in one ers command_folders tag. Describes the directory where UNIX-like commands are located. command_fold Only needed for MinGW compiler on Windows. The er command_folder tag can contain the following tags: Windows registry key of commands. Note that the XML tag regestry must be spelled "regestry" although this is a mispelling. Regular expression that allows NetBeans IDE to find the pattern commands folder in the registry suffix Folder with executable files Regular expression that allows NetBeans IDE to find path_pate commands. Note that the XML tag must be spelled rn "path_patern" although this is a mispelling. Name of error parser service, see Creating a Custom scanner Compiler Error Handler id Name of error parser service

Compiler flags This table lists the tags used to describe the compilers and specify compiler flags for the toolchain. Description Example for GNU compiler Set of compiler flags are c,cpp located in following sub nodes For GNU under cygwin on Regular expression that allow recognizer Windows to find compiler .*[\\/].*cygwin.*[\\/]bin[\\/]?$ Compiler name (name of compiler gcc or g++ executable file) version Version flag --version Flags to get system include system_include_paths -x c -E -v paths system_macros Flags to get system macros -x c -E -dM user_include Flag to add user include path -I user_macro Flag to add user macro -D Groups of flags for different development_mode development modes Groups of flags for different warning_level warning level Groups of flags for different architecture architecture Flag for stripping debug strip -s information Flags for dependency dependency_generation -MMD -MP -MF $@.d generation precompiled_header Flags for precompiled header -o $@ Groups of flags for multithreading multithreading support Groups of flags for different standard language standards Groups of flags for different language_extension language extensions Tags

Creating a Custom Compiler Provider


NetBeans IDE has a default compiler provider: org.netbeans.modules.cnd.toolchain.compilers.MakeProjectCompilerProvider This provider works well enough for most toolchains, but you can also create own provider. To create your own compiler provider:

y y y y y y y

extend abstract class org.netbeans.modules.cnd.api.compilers.CompilerProvider define class as service and put it before default provider:
@org.openide.util.lookup.ServiceProvider(service = org.netbeans.modules.cnd.spi.toolchain.CompilerProvider.class, position=500) public class CustomCompilerProvider extends CompilerProvider { ... }

The position=500 attribute guarantees that the custom provider will be invoked before the default provider. The custom provider should return not NULL "Tool" in method "createCompiler()" for tool that should be overridden.

Creating a Custom Compiler Error Handler


NetBeans IDE has a two default compiler error handlers.
y y

for GNU compiler for Sun Studio compiler

The GNU compiler handler works well enough for any GNU compiler, but if you want you can define your own compiler error handlers. To create your own compiler error handler:
y y y y y y y y y

extend abstract class org.netbeans.modules.cnd.spi.toolchain.CompilerProvider define class as service:


@org.openide.util.lookup.ServiceProvider(service = org.netbeans.modules.cnd.spi.toolchain.CompilerProvider.class) public class CustomCompilerProvider extends ErrorParserProvider { ... @Override public String getID() { return "MyParser"; // NOI18N } }

y y y

link error scanner and tool collection description by ID:


</scanner id="MyParser"> ... </scanner>

*********************

Fun with stored procedures: insert only if unique. (Transact-SQL/MS SQL) ProgPal

Configuring Cygwin C/C++ compiler for Netbeans 6.5 (under Windows)

Posted by Roy Triesscheijn on Friday 20 March, 2009

Note: since the 1st of December 2009, this blog was moved to www.royt.nl, all content here should be considered archived, new content, updates, comments, etc will no longer be released here. A fresh copy of this article exists at the new website, here you can post comments and ask questions which I will try to answer asap. Sincerely, Roy Triesscheijn
Today I tried setting up Netbeans as a C IDE, it has built in support for C, but unfortunately enough you have to manually configure a compiler so that you can actually debug / build your C/C++ programs. Fortunately there is this helpful page at Netbeans.org to help you install Cygwin, a very popular UNIX/Windows C/C++ compiler. However, this helpful page isnt as helpful as Id hope at all! It will point you in the right direction to download Cygwin, and will tell you what packages to select for download, it will even tell you to set up your PATH environment variable for Cygwin, but it will assume Netbeans auto detects the correct settings, which it unfortunately doesnt do. (Well at least at my pc, and Ive seen a few threads with the same problems around). So here is my attempt at a more complete overview on installing Cygwin for Netbeans 6.5. Go to http://www.cygwin.com/setup.exe and download the small setup program. Run it (if your using Vista, set the compatibility options to XP SP2, and run it as administrator). Follow the pretty standard steps until you get to choose the installation packages. If you thought just pressing next would install the most common Cygwin apps, like the compiler (gcc.exe) and the make implementation, unfortunately Cygwin, is not just a C/C++ compiler, it even includes a java compiler, games, documentation, text editors etc. . Ok so just install everything, well that will install the compiler etc., but also 3GB of (for us) useless data. So dont make the same mistake I did there. We are going to search for the few packages that we actually need. According to the Netbeans.org these are: select gcc-core: C compiler, gcc-g++: C++ compiler, gdb: The GNU Debugger, and make: the GNU version of the make utility. Unfortunately these arent easy to find. For example there is no core package directly visible (we do have base and development though). It took me a while but I think Ive nailed it down. Select the following packages by clicking the weird refresh icon next to them until it says install: -The entire base package -In the development package select: binutils

gcc core gcc g++ gcc g77 gcc mingw core gcc mingw g++ gcc mingw g77 gdb make mingw runtime (note Im not sure about the mingw packages, this seems to be a seperate C compiler but it doesnt seem to harm) After that go to windows configuration screen->advanced->environment variables. And add C:\Cygwin\Bin to the PATH variables (or wherever you have located your Cygwin\bin folder, (make sure to separate it from the last one with a ;). Start Netbeans, navigate to tools->options->C/C++. Check to see if Cygwin is in the list on the left panel. Select it, and then fill in the options as following: (I assume that youve installed it in C:\Cygwin) Base Director: C:\Cygwin\bin C compiler C:\Cygwin\bin\gcc.exe C++ Compiler: C:\Cygwin\bin\g++-3.exe* Fortran Compiler: C:\Cygwin\bin\g77-3.exe* Make Command: C:\Cygwin\bin\make.exe Debugger: C:\Cygwin\bin\gdb.exe (* marks optional) Now make a new C project. And add a new main file to it by right clicking the source directory and selecting New->Main C file. There is an odd chance that the include directives will be underlined with red. This is not a problem, as you will see the program will compile and run fine, but you cant use intellisense this way so we are going to fix it. (First make sure your PATH variable was correctly set!). Right click on your project and select properties. Go to build->C compiler (or C++ compiler if you are doing C++). Select the button after Include Directories. And add the C:\Cygwin\usr\include directory to the include directories. Save your settings and reload your project. The red lines shouldve disappeared now, leaving you behind with a fully functional C/C++ IDE and compiler in Netbeans. *Yay*!

(I wish someone else wouldve written this before me, so that I wasnt busy uninstalling a couple of gigabytes of C/C++ tools/compilers/utilities/fonts and text editors!)

Das könnte Ihnen auch gefallen