Eclipse CDT language mappings
Eclipse/CDT fails to parse some file? You don”t see anything wrong with it, neither does the compiler? Still CDT underlines parts or all of a file to mark a parse error? The most probable cause is that your file has a file extension that CDT associates with the C language, but it contains some bits of C++.
To solve this problem the CDT offers a feature called language mapping which allows you to override the default for a given file type. This can be done for a single file, for the project or for the whole workspace.
For a single file, right click on a file in the project browser -> select Properties -> C/C++ General -> Language Mappings. The dialog also offers some shortcut to do it on project or workspace level.
Integrating the NetworkManager with aiccu
I am using the service of SixXS to connect my laptop with the IPv6 internet. They provide an utility called aiccu to setup the tunnel. Getting it to work is really painless. Just do an “apt-get install aiccu”, configure your SixXS username and password and your done.
The aiccu package provides a traditional startup script which is placed in /etc/init.d and is added to the different run levels. Files on the different levels are executed in alphabetical order. The name of the aiccu script ensures that it is run after the network has been brought up. So as long as the networking on your system is statically configured through /etc/network/interfaces everything is fine.
Unfortunately i am (as most probably other desktop users also) using the NetworkManager to handle my network connections. This means that at the time that the aiccu startup script is run there is no network available which will make the tunnel setup fail.
What i would like to do is to run the aiccu script every time the NetworkManager has established a connection so that the tunnel is re-established. Fortunately NetworkManager provides a way to do this. It will run (in alphabetical order) all the scripts placed in /etc/NetworkManager/dispatcher.d/. The scripts are passed two variables, the interface name and if it is going up or down. The scripts are run as root.
Copy this script to /etc/NetworkManager/dispatcher.d/99aiccu to integrate aiccu with the NetworkManager. It should work with KDE and Gnome.
#!/bin/bash
IF=$1
ACTION=$2
if [ "$ACTION" == "up" ]; then
/etc/init.d/aiccu restart
fi
if [ "$ACTION" == "down" ]; then
/etc/init.d/aiccu stop
fi
Kubuntu 9.10 DNS/network troubles
After installing Kubuntu 9.10 RC1 networking became kind of flaky. It somehow worked but everything was slow and unpredictable. It was most visible while browsing the web. Some pages where loading for ages (5+ minutes), some never finishing to load at all while others were just fine after some initial delay. This problem was not only visible in firefox but also in konqueror.
I started wireshark to have a look on what was actually going on. It showed that my computer was sending DNS queries to my local DSL router (SpeedPort W303V; an ISP provided blackbox). This DSL router also works as a local DNS-cache which it is advertising via DHCP. This explains why the DNS queries are sent to the DSL router. The traffic log showed further that requests for A records (IPv4) were properly answered but requests asking for AAAA records (IPv6) were never blessed with a reply. The missing answer was triggering some retry mechanism which was resending the AAAA record request a few times. Only after this mechanism timed out, the browser was actually starting to load a page.
This nicely explains why some pages were loading fine after some initial delay (all content on one server) while others were loading forever (complex sites were a lot of addresses need to be resolved to load all content). It also explains why firefox and konqueror were both affected, DNS resolving is typically handled by the libc.
The DNS cache in my DSL router is apparently not handling requests for IPv6 address records. So far i was not able to figure out what exactly has changed between Kubuntu 9.04 and 9.10. Just replacing the /etc/resolv.conf with a version pointing to an external DNS server solved the problem for now.
Apart from that Kubuntu 9.10 looks very promising. A lot of the KDE4 problems which plagued me in 9.04 are gone.
Adding new sites to the eclipse updater “hangs”
Just for the record: If adding a new site to your eclipse updater seems to hang/times out (entry is always “pending…”), its most probably not a problem with the update site itself but rather some firewall interfering. Make sure you have setup the appropriate proxies in eclipse (Window->Preferences->General->Network Connection).
Installing a gcc4.4 snapshot on Ubuntu
The upcoming gcc4.4 release adds a few more C++0x features (initializer lists, auto typed variables, defaulted and deleted functions, strongly typed enums, …) to the in this regard still very incomplete g++. To be able to play with it one has to compile a gcc-snapshot which turned out to be fairly easy.
- Download a copy of the gcc-core and the gcc-g++ snapshot from here. I used gcc-core-4.4-20090306.tar.bz2 and gcc-g++-4.4-20090306.tar.bz2
- Extract both archives into the same directory.
tar xvjf gcc-core-4.4-20090306.tar.bz2 && tar xvjf gcc-g++-4.4-20090306.tar.bz2 && cd gcc-4.4-20090306 - run
configure ./configure --prefix=$SOMEPLACEBe sure not to overwrite your default gcc-installation. If anything is missing configure will complain. - On my Ubuntu system i had to install libmpfr-dev to proceed.
sudo apt-get install libmpfr-dev - Now all it takes is time…
make && make install
To use it just put $SOMEPLACE in front of your $PATH. If you are using it to compile code which uses C++0x features (which is the whole point) you have to add -std=c++0x to your compiler command line. Bonus: If you are using Eclipse/CDT you can start it with the modified path and the builder will pickup the new gcc just fine.
Eclipse CDT 6 Milestone 5
The final release of CDT6 is still a few month ahead (scheduled for ~June) but there are already public milestone builds available. I haven’t played much with M5 but one thing that directly caught my attention is that you no longer have to manually setup all the include paths in your project. Eclipse will just use all the headers inside your workspace to resolve includes.
This is something i have been longing for for years. If you work inside a large projects tree with a lot of include directories (think public interfaces/private headers of N modules) its really painful to add all these to the project.
It will also enhance the out of the box experience for people importing their first project into eclipse. I know from experience that most people don’t know that they have to setup the include paths first (how should they? before CDT5 the default setup was not even outlining missing includes!). Without being able to resolve the includes the indexer will not work properly so all the advanced features are not available (go to definition, type hierarchy, global symbol lookup, macro expansion, include browser, etc…). Confronted with such a crippled project first timers might jump to the conclusion that eclipse has not much more to offer than their trusted editor of choice.
As always i am looking forward to the full release.
Elegant human readable enums
While reading the source of some FOSS project i noticed that the authors have come up with a nice way to get human readable enums through the use of the “stringification feature” of the C/C++ preprocessor. The trick is that the members of the enum are declared through a macro which can be expanded in different ways.
-
#include <iostream>
-
#include <cstdlib>
-
#include <cassert>
-
#include <time.h>
-
-
namespace MyStuff
-
{
-
enum Things {
-
#define THING(X) X,
-
#include "things.def"
-
MAX_THINGS
-
};
-
-
static const char* const ThingNames[] = {
-
#define THING(X) #X,
-
// ^ preprocessor stringification
-
#include "things.def"
-
NULL
-
};
-
-
const char *toString(Things t) {
-
assert(t >= 0 && t < MAX_THINGS);
-
return ThingNames[t];
-
}
-
}
-
-
int main(int argc, char **argv)
-
{
-
using namespace MyStuff;
-
std::srand(::time(NULL));
-
for (int i = 0; i < 4; ++i) {
-
Things aRandomThing =
-
static_cast<Things>(std::rand() % MAX_THINGS);
-
}
-
}
-
things.def:
-
-
#ifndef THING
-
#define THING
-
#endif
-
-
THING(CELLPHONE)
-
THING(NOTEBOOK)
-
THING(CUP_OF_TEA)
-
THING(MOUSE)
-
-
#undef THING
-
Time after time i am amazed what you can do with some of the advanced preprocessor features like “token pasting” or stringification.
Overload
I recently discover “Overload” - a publication done bi-monthly by the ACCU (Association of C & C++ Users). It’s focus is on C++ and software engineering in general. The overall quality of the articles is quite good and they also have some well known names like Kevlin Henney or Peter Sommerlad writing articles for them. In my opinion a worthy alternative to the dearly missed “C/C++ User Journal”. The complete archive can be found here. Highly recommended!
OpenWrt Gemini 0.2
Paolo Scaffardi just released version 0.2 of his OpenWrt port for SL3516 based devices. The main change from the 0.1 release is that he switched to the OpenWrt trunk as base. The great benefit of this decision is that there are a lot more packages available (over 940 new packages). The pre-compiled firmware images worked without problems with my NAS4220 device. YMMV!
Webif^2 tutorial part 02
Last time we created a simple “hello world” page. Not very useful. For our NAS box we need a way to input/select configuration data. Web interfaces implement user input using HTML forms. Webif^2 provides some features to make it easier to work with them.
The following example allows us to enter the name (text) and job description (selecting a predefined value) of the person you want to cheer. I start with my explanation at the end of the script.

On the lines 11 to 21 we use the function display_form to create the output you can see in the screen shot. display_form is at its heart an AWK script. AWK works on a line by line basis so every line till the EOF marker will be processed on its own. Processing means that the lines are expanded into fragments that will make up the final form.
start_form/end_form are doing exactly what you expect them to do. What about line 13? display_form is not only generating a form, it also embeds the form elements together with other elements into a table. “field” will put the text following it into a cell of this table. Line 14 creates a drop down box. The following two lines add two options to this drop down box. By passing the value of the variable FORM_job we can preselect a particular option. This allows us to remember user input between script invocations (albeit only on the browser side).
The input field to enter the name is created on line 18. As the last step before finishing the form we add a submit button so we can send the form back to the server for processing (line 19). The action associated with the form will be the embedding page.
-
#!/usr/bin/webif-page
-
<?
-
. /usr/lib/webif/webif.sh
-
-
header "Test" "Cheers" "Cheers" ” "$SCRIPT_NAME"
-
-
empty "$FORM_name" || empty "$FORM_job" || {
-
echo "cheers to $FORM_name the $FORM_job!</br>"
-
}
-
-
display_form <<EOF
-
start_form|Who is to be cheered?
-
field|Job description
-
select|job|$FORM_job
-
option|Code Simian|Code simian
-
option|Code Monkey|Code monkey
-
field|Name
-
text|name|$name
-
submit|button_set_name|Set
-
end_form
-
EOF
-
-
footer
-
?>
-
-
<!–
-
##WEBIF:name:Test:50:Cheers
-
–>
To wrap up this tutorial we now only have to do something with the data submitted through the form. On line 07 we check if $FORM_name or $FORM_job are empty. If both of them contain a value we display a customized greeting message.
Where do these variables come from? Haserl is parsing POST/GET requests and places form variables into the context of the script it currently exectues. The variables are always prefixed with “FORM_”.
This example wasn’t extensive of course. display_form provides a number of additional features to create radio buttons, check boxes, text areas, …. For a complete list you have to look up files/usr/lib/webif/form.awk.
The time we will extend this example to do some input validation.