The purpose of this tutorial is to outline the basic process for creating an C++ application with MonoDevelop, and also provide some tips for getting started. Our example for this tutorial will be a simple console application with two classes and some compile options.
From the file menu, select "New Solution/Project" to open the "New Solution" window. Select "C"->"C++" from the language list and "Console Project" from the templates, then give your application a name.
After clicking "Forward" you can enable some features like Packaging and Translations, though these can be added to the project at any time. When you are finished, click the "New" button. This creates a new directory for your solution in the Projects directory of your home directory. The "Console Project" template provides an already buildable application. You can test it by selecting "Run" from the "Run" menu or simply pressing F5. This will build the application, and provide the output in the "Application Output" pad.
MonoDevelop provides the Class Pad, which shows the hierarchy of classes and other symbols in the solution. The class pad is fully supported for C/C++ (via exuberant ctags), and can be used to browse the defined symbols and jump to their declaration with a simple double-click.
We add the following code to main.cpp:
class SimpleTextClass
{
public:
string text1;
string text2()
{
return text1+"world!";
}
};
class AdvancedTextClass : public SimpleTextClass
{
public:
AdvancedTextClass()
{
text3="";
}
AdvancedTextClass(string text)
{
text3=text;
}
string text3;
};
When the file is saved, the Class Pad shows the new symbols:
MonoDevelop also provides limited code completion in the text editor. Parameter completion works well, and completion also works for the '::' operator on classes, structs and namespaces. However, completion does not yet work for class instance members using the '.' and '->' operators.
Let's try it out for that function:
int complex_function(int x, int y, double z, float a, char b, char name);
In Project->Options->Configurations you can fully adjust compile commands. You might want to use Debug(Active) or Relase -> Code Generation tab. You can set here warning level, optimization level, targets, symbols and extra compiler and linker options and libraries for your project.
For our sample application, we do not want to include all of our functionality in the executable, so we want to create a library. To do this, right click on the Solution icon in the Solution Pad. Select "Add->Add New Project". From the "New Solution" window, select "C++" as the language and "Shared Library" as the template. Name it "TestC++Library" and click "New". This will create a new project within your solution.
Now your solution has a new library which initially does absolutely nothing.
Let's add simple functionality.
Add to main.h in your Library Project line:
int test();
And to main.cpp in your Library Project lines:
int test()
{ return 1337; }
Now, your Library is ready. You have to right-click on Packages in your Application in Solution tab and select Edit Packages. On the second tab named "Project Packages" you have a list of available libraries in your solution. Let's check it. And click "OK".
Now you have to add line to our Application's main.cpp line:
And now you can use test() function in your application. We can test it by adding line to our Application's main.cpp: cout << test() << endl; Press "F5" button and the "1337" should appear in the Application Output tab.
From the file menu, select "New Solution/Project", this will open up the "New Solution" window. Select "C"->"C++" from the language list and "Console Project" from the templates. Give your application name "SimpleC++GTKApp".
You can use some features like Packaging and Translations if you want. When you are finished, click the "New" button. This creates a new directory for your solution in the Projects directory of your home directory.
To enable GTK+ you have to right-click on Packages in your Application in Solution tab and select Edit Packages. On the first tab named "System Packages" you have a list of available libraries in your system provided by pkg-config, which provides a unified interface for querying installed libraries for the purpose of compiling software from its source code. Let's check GTK+-2.0. And click "OK".
Note: If you doesn't have gtk+-2.0 on your install gtk+2-devel package.
Now copy and paste to your main.cpp file <source lang=cpp>
using namespace std;
static void
on_destroy (GtkWidget * widget, gpointer data)
{ gtk_main_quit ();
}
int main (int argc, char *argv[])
{ GtkWidget *window; GtkWidget *label; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_container_set_border_width (GTK_CONTAINER (window), 20); gtk_window_set_title (GTK_WINDOW (window), "TEST"); gtk_window_set_default_size (GTK_WINDOW (window), 200, 50); g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (on_destroy), NULL); label = gtk_label_new ("Hello, World"); gtk_container_add (GTK_CONTAINER (window), label); gtk_widget_show_all (window); gtk_main (); return 0;
}
</source>
Just click the Run button or press "F5". Here's a shot of the final product in action:
This tutorial has helped you to make C++ projects in MonoDevelop and given some starting points on how to set up and begin developing your C++ applications.
C/C++ projects can be included in Mono projects, by Platform Invocation Services. This might be useful to access to access C++ libraries or C libraries for better performance.
As of MonoDevelop version 0.9, it is possible to build your solution from the command line. Using the above example, we could do:
mdtool build --f --buildfile:Projects/ConsoleCPP/ConsoleCPP.mds
Which will produce:
MonoDevelop Build Tool Loading solution: Projects/ConsoleCPP/ConsoleCPP.mds Loading project: /home/users/entereczek/Projects/ConsoleCPP/ConsoleCPP/ConsoleCPP.mdp Building Solution ConsoleCPP
| Images 0 | ||
|---|---|---|
| No images to display in the gallery. |