Tuesday, 29 January 2013

Vala #4: Using makefiles with Geany

In the last article we learnt how to set up Geany for compiling your Vala applications. Now we are going to see how we can create a makefile for building the entire project.

The valac "%f" command that we assigned to the Build button in the previous article works only on the current file that is open in the editor. This is enough if your project has a single .vala source file. However, most projects usually consist of multiple source files and we need a way to compile and link all those files to generate the final executable.

This is where the make utility comes into the picture. make is a classic Unix utility that is used for building executable files from source code. It reads instructions from a makefile which contains commands for building and installing the project.

Running the make command from the project directory will build the project using the commands in the makefile. It can also be used for installing and uninstalling the executable (and related files).

Creating a makefile

Lets create a makefile for our project. We will be using the same example that we used in the last article.

Add a new text file to the project and paste the following text:

all:
    valac --pkg gtk+-3.0 *.vala -o app1

clean:
    rm -rf *.o app1

install:
    cp -f app1 /usr/bin

uninstall:
    rm -f /usr/bin/app1

Save it with the file name makefile in the project folder.

The all section contains commands for building the project. We will simply execute the valac compiler for compiling the .vala files.

The install section contains commands for installing the application. We will use the cp command to install the executable file to the system folder /usr/bin. Files placed in this folder are available to all users and can be executed from any directory by typing the executable name in a terminal window.

The uninstall section contains commands for uninstalling the application. We will use the rm command to remove the executable file from /usr/bin.

Test the makefile from terminal

Open a terminal window (CTRL+ALT+T) and cd to the project folder.

  • Run 'make all' from the project directory to build the project

This will compile the .vala files to create the executable file app1 in your project directory.

  • Run 'sudo make install' to install the application

This will install the app1 executable on your system. Any user on your system can now run the app1 application by typing app1 in a terminal window.

  • Run 'sudo make uninstall' to uninstall the application

This will remove the app1 executable from your system.

Note: Since the install and uninstall commands copy or remove files from the system directory /usr/bin, we need to run 'make install' and 'make uninstall' using sudo

Using makefile with Geany

Change the build command from:

valac --pkg gtk+-3.0 "%f"

to:

make all

Clicking the Build button in the IDE toolbar will now build your entire project.

No comments:

Post a Comment

If you are reporting an issue and commenting as an anonymous user, please leave your email address so that I can get in touch with you.