GTKMMApplicationInNetBeans

GTKMM Application using NetBeans 6.1 in Windows XP and Windows Vista



NetBeans IDE

NetBeans IDE (Integrated Development Environment) is one of the top Java IDEs. NetBeans IDE was first a student project at Charles University in Prague. The project was bought by Sun in 1999. In 2000, Sun open-sourced Netbeans. Since then, NetBeans grows in usability, stability and features. Thousands of developers around the world use Netbeans. Netbeans was created as a Java IDE. It seems that NetBeans IDE is moving towards a more versatile IDE. Except for the three java platforms, Mobile, Desktop and Enterprise, NetBeans provides tools for creating C/C++ and Ruby projects.

Netbeans is created in Java language and Swing GUI. This combination is ideal for a multiplatform, multilanguage IDE.

System requirements

To comfortly use NetBeans IDE, we need enough memory. Using Netbeans is OK, unless we have little memory. 512 MB is minimum requirement. We must not use other memory hungry applications, when we run NetBeans. If we use more than 100% of our memory the system will do a lot of swapping. The performance of NetBeans will then degrade.


Previous steps before compile your first GTKMM application

1) Download the gtkmm package (gtk+ included). I use this installer:

  http://live.gnome.org/gtkmm/MSWindows

2) Download mingw compiler with MSYS console utility:

  http://wiki.netbeans.org/MinGWInCCDevelopmentPack

3) Download JDK from SUN homepage:

  http://java.sun.com/javase/downloads/index.jsp

4) Download netbeans for c++ (requires JDK installed):

  http://www.netbeans.org/features/cpp/


Creating a C/C++ project

There are several ways, how we can create a C/C++ project. From File menu, we select New Project option, or use the Ctrl+Shift+N shortcut.

We can also create the project by using the context menu, right click on the project window and select New Project from the pop up window.
The fastest way is to click on the new project button on the edit toolbar.



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/create_project_GTKMMApplicationInNetBeans.jpg

Figure: Create new project

Next we do two steps. Choose a type of a project and then provide a name and location for the project. From the Categories box, we select C/C++, from the Projects box, C/C++ Application.

http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/create_project_GTKMMApplicationInNetBeans.jpg

Figure: Create a new project

In the next dialog window, we provide a name and location for our C/C++ project. The is also a check box. t is checked by default. It means, that by default a new project is set to be the main project. We can have several projects opened in Netbeans. If we click on the build, debug or run button,

this is applied to the current main project in the IDE. We can set, unset a main project from the File menu, or by right 

clicking on the project icon in the projects window.
http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/create_project2_GTKMMApplicationInNetBeans.jpg
Figure: Name and location

We want create our first hello world application and therefore we need add the main file to the project.



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/create_main_file_GTKMMApplicationInNetBeans.jpg
Figure: Create the main file

Netbeans create a new c++ main file based in a own template. This template could be configured if we want also.



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/new_cpp_template_GTKMMApplicationInNetBeans.jpg
Figure: Main file for the project by default.


Simple GTKMM application

In the next example, we will create a simple GTKMM application in C++ language.


#include <gtkmm.h>

int main(int argc, char *argv[])
{
    Gtk::Main kit(argc, argv);

    Gtk::Window window;
    
    Gtk::Main::run(window);
    
    return 0;
}



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/copy_gtkmm_code_GTKMMApplicationInNetBeans.jpg
Figure: Hello world in GTKMM.


We need see if mingw compiler is configured. Remember in netbeans we need use the MAKE utility of MSYS nor the make build in mingw. Take a look of the next picture.




http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/tools-options-c-cpp_GTKMMApplicationInNetBeans.jpg
Figure: Go to options/tools and c/c++ tab.


In order to create even a simple GTKMM application, we must use a quite a few include files and libraries.

Fortunately, there is one handy tool called pkg-config, which will greatly help us settip up project settings.

The pgk-config returns metadata about installed libraries. Simply put, if we want to use a specific library, it will provide us necessary dependent libraries and include files, that we need. The pkg-config program retrieves information about packages from special metadata files. These files in Windows are in C:\GTK\lib\pkgconfig. The pkg-config tools was by default in c:\GTK\bin and search the pkgconfig files in a relative path (..\lib\pkgconfig)

In the last version fo gtk i found a problem with pgk file of the libpng13 version. No problem!!, edit the file by hand to use libpng12.

Take care of the warnings of the pkg-config because perhaps you need edit by hand some pkg-config files (It´s easy, don´t worry).

We want compile in netbeans, but we are going to to something similar to this command line:

g++ newmain.cpp -o newmain.exe -mwindows `pkg-config --libs --cflags gtkmm-2.4`



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/compiling_msys_GTKMMApplicationInNetBeans.jpg

Figure: Compiling the application the applications using MSYS console of Mingw.

In netbeans we need separate the --libs flags from the -cflags.


$ pkg-config --cflags gtkmm-2.4
-mms-bitfields -IC:/GTK/include/gtkmm-2.4 -IC:/GTK/lib/gtkmm-2.4/include -IC:/GT
K/include/glibmm-2.4 -IC:/GTK/lib/glibmm-2.4/include -IC:/GTK/include/giomm-2.4
-IC:/GTK/lib/giomm-2.4/include -IC:/GTK/include/gdkmm-2.4 -IC:/GTK/lib/gdkmm-2.4
/include -IC:/GTK/include/pangomm-1.4 -IC:/GTK/include/atkmm-1.6 -IC:/GTK/includ
e/gtk-2.0 -IC:/GTK/include/sigc++-2.0 -IC:/GTK/lib/sigc++-2.0/include -IC:/GTK/i
nclude/glib-2.0 -IC:/GTK/lib/glib-2.0/include -IC:/GTK/lib/gtk-2.0/include -IC:/
GTK/include/cairomm-1.0 -IC:/GTK/include/pango-1.0 -IC:/GTK/include/cairo -IC:/G
TK/include/libpng12 -IC:/GTK/include/atk-1.0

By providing the --cflags option, we get all necessary include files for a GTKMM application.

$ pkg-config --libs gtkmm-2.4
-LC:/GTK/lib -lgtkmm-2.4 -lgiomm-2.4 -lgdkmm-2.4 -latkmm-1.6 -lgtk-win32-2.0 -lp
angomm-1.4 -lcairomm-1.0 -lglibmm-2.4 -lsigc-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio
-2.0 -lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lpango-1.0 -lca
iro -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl

Similarly, we get all libraries with the --libs option. Notice that we might not need all libs.

Now we will continue by creating a source file for our GTKMM application. Create a new file, from Categories select C++ Files, from File Types select Empty C++ File. Provide a name and location for the file.


We create a small window in GTKMM. Before we can build and run the application, we must set up our project settings. We must specify the include files and libraries, that are needed to build the application.

To provide necessary include directories, right click on the project icon and select properties.



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/compiler_settings_GTKMMApplicationInNetBeans.jpg

Figure: Configure the libraries and headers for the application.


From the Categories box, select C/C++, C++ compiler, general node. Click on the button near Include Directories. We could use the pkg-config here.



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/compiler_options_GTKMMApplicationInNetBeans.jpg

Figure: Configure the headers paths and command line flags to the compiler.
We need add the -mwindows flags if we don´t want the ms-dos console for own applications. This is a special flag only for mingw.

-mwindows `pkg-config --cflags gtkmm-2.4`


Add necessary paths to the include directories. From the pkg-config tool, we got the list of the directories. 

To provide the libraries, we select Linker, Libraries node.

http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/linker_options_GTKMMApplicationInNetBeans.jpg

Figure: Linker configuration.

`pkg-config --libs gtkmm-2.4`


Now we have a problem. We can compile our first GTKMM application, but the autocompletion doesn´t work because netbeans need the path of headers in the classical way, nor by pkg-config --flags.

To avoid create by hand all includes path for gtkmm i have created this ruby script to read all includes paths in a format to netbeans:


text=`pkg-config --cflags gtkmm-2.4`

my_array=text.split(' ')

final_text=""
my_array.each do |include|
  if include=~/-I(.+)/
    final_text=final_text+$1+";"
  end
end

#delete last ';' char
final_text=final_text.chop
puts  final_text


This ruby script out this text.

C:/GTK/include/gtkmm-2.4;C:/GTK/lib/gtkmm-2.4/include;C:/GTK/include/glibmm-2.4;C:/GTK/lib/glibmm-2.4/include;C:/GTK/include/giomm-2.4;C:/GTK/lib/giomm-2.4/include;C:/GTK/include/gdkmm-2.4;C:/GTK/lib/gdkmm-2.4/include;C:/GTK/include/pangomm-1.4;C:/GTK/include/atkmm-1.6;C:/GTK/include/gtk-2.0;C:/GTK/include/sigc++-2.0;C:/GTK/lib/sigc++-2.0/include;C:/GTK/include/glib-2.0;C:/GTK/lib/glib-2.0/include;C:/GTK/lib/gtk-2.0/include;C:/GTK/include/cairomm-1.0;C:/GTK/include/pango-1.0;C:/GTK/include/cairo;C:/GTK/include/libpng12;C:/GTK/include/atk-1.0

Copy the text in the netbeans include entry box.



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/add_code_completion_GTKMMApplicationInNetBeans.jpg


Finally, we press F6 to build and run the project with the next source code.



Final word

NetBeans IDE provides excellent tools for C/C++ development that makes the programming more efficient.

UNIX/Linux provides a plethora of development tools for C/C++ projects. 

Some developers prefer the traditional Vi/Emacs/command line tools. Some prefer modern IDEs.

When creating large projects, IDEs like NetBeans gain momentum. NetBeans provides lots of features like code completion, code folding, syntax highlighting, code templates, integrated compiler and debugger. Just to name a few. Especially, it is much easier for a human to cope with multiple files when using a GUI, than with command line tools or traditional non GUI editors. Finally, choosing the tool for programming has always been a matter of subjective preference. I recommend at least trying this excellent application.

http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/full_autocompletion_GTKMMApplicationInNetBeans.jpg

Figure: Full Code completion in action


Code-blocks comparison.

If your find very difficult to configure Netbeans you could try Code-Blocks because don´t have dependencies and have a full mingw preinstalation. I recomend you use netbeans anyway because is a real professional IDE like Microsot Visual Studio c++.



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/project_build_options_compiler_GTKMMApplicationInNetBeans.jpg

Figure: Go to Project, Build options... in Code-Blocks to edit the compiler settings



http://wiki.netbeans.org/attach/GTKMMApplicationInNetBeans/project_build_options_linker_GTKMMApplicationInNetBeans.jpg

Figure: Go to Project, Build options... in Code-Blocks to edit the linker settings }}

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo