Status of libc++ on Linux

Eventually it comes a time when you have to use a C++11 piece of software which, by default, doesn’t compile on Linux. This happens because Linux distributions use the GNU libstdc++ library as the default implementation of the C++ standard. Unfortunately the libstdc++ is not (yet) C++11 features-complete, because there are some C++11 features which are currently not implemented.
Consider for instance the following C++11 program:

#include <fstream>

int main() 
{
    auto stream = std::ifstream("~/test.txt");

    return 0;
}

Even such a trivial program cannot be compiled using gcc (gcc 4.9.1 at the time of writing). If you try, you’ll get this kind of error:

$ g++ -std=c++11 test.cpp 
test.cpp: In function ‘int main()’:
test.cpp:5:45: error: use of deleted function ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’
     auto stream = std::ifstream("~/test.txt");
                                             ^

A good solution to this problem is to switch to a C++ implementation which is C++11 complete: we are talking about the LLVM libc++ library.
This library has been created mainly due to license problems, since Apple and the BSDs don’t like the GPLv3 used by libstdc++. So, we can say that Linux is not a first class target of libc++ developers, and there are also some religious debates about whether Linux users should use libc++ in their systems.
But actually I’ve just tested libc++ on my Debian Sid and it works remarkably well. You just need to install these packages:

  • libc++1
  • libc++-dev
  • libc++abi
  • libc++abi-dev

You need also to install and use clang instead of gcc, since at the moment I don’t think it’s possible to use gcc togheter with libc++.
You can finally compile every C++11 program using: clang++ -std=c++11 -stdlib=libc++

Of course on other distributions different from Debian, the name of those packages may slightly change.
The great thing is that libc++ and libstdc++ are coinstallable and they can perfectly coexist. If you want you can use libc++ just for C++11 programs and libstdc++ for all the other stuff.

Kronometer 1.5 is out

Kronometer has just been released with the version 1.5.0. This release brings a couple of new features and an important bugfix.
First of all, you can take laps annotations: Kronometer now lets you place a custom text note over a certain lap time. This can be useful in many use cases. For example, if you are using Kronometer to time your presentation tests, you may want to time each single slide, or two by two slides.
Check the Note column in the right of the following screenshot to understand how it will works:

kronometer-notes

Another new feature, which has been implemented after a request, is the ability to completely disable the laps recording feature. This might be useful if you never use the laps and if you want a less crowded user interface. The following screenshot shows the resulting Kronometer UI: as you can see, the lap button and the lap table are gone.

kronometer-no-laps

To disaple the laps recording as showed, go to Settings -> Configure Kronometer -> Interface settings and uncheck the Enable laps recording checkbox.

The last new feature is the support of the AppData specification, whose content is already available in 14 languages, thanks to the KDE localization team.

Finally, Kronometer 1.5 has an important bugfix. Now if there are multiple Kronometer windows opened and if one or more Kronometer settings are changed, those changes will be applied to all those windows.

How to fix the broken shutdown on ThinkPad Edge E130

My current laptop is a ThinkPad Edge E130 and I really like it, except for a terribly annoying bug which affects lots of Linux distributions (possibly all of them): if you shutdown your distro, you might have your laptop rebooting instead of shutting down as expected. If you don’t even know that this bug exists, you might experience bad situations like shutting down your pc, put it in your bag and then realize, many hours later, that the battery of your laptop is gone. Of course this is something that actually happened to me.
It’s still not clear where the bug comes from, since it affects many different ThinkPad models and even some non-ThinkPad laptops (e.g. some Acer models). That’s why you may find lots of solutions in the web, but which one is the right one? Some people reports that the bug is defeated by installing the laptop-mode-tools package. According to other people there are some kernel modules to be removed just before the shutdown.
I’ve finally managed to find the right fix for the ThinkPad Edge E130 and I will share it in this post.
I’ve tested it on a Debian Sid system which uses systemd as init manager. It should also work for other systemd distributions.

Step 1: the solution

The solution that makes the ThinkPad always shutting down as requested is a simple bash script with the following content:

#!/bin/sh

for i in /sys/bus/*/devices/*/power/control; do
	echo on > $i
done

exit 0

This script has to be run just before the system shutdown in order to fix the bug. What does the script actually do? It’s hard to tell, but somehow the kernel takes a complete power-management control and is able to make a working shutdown. The value on written by the script overwrites many existing auto values, and probably one (or more) of them causes the bug.

Step 2: creating the solution

You can put the above script wherever you want. In this example we’ll use /usr/local/bin/shutdown-fix.sh. You just have to make sure to make it executable:

$ cd /usr/local/bin
$ sudo chmod +x shutdown-fix.sh

Step 3: installing the solution

The script has to be run before the system shutdown. In the systemd world, this means that you have to create a custom service which executes the script at the right moment.
You should create a shutdown-fix.service file in either /etc/systemd/system/ or in /usr/lib/systemd/system with the following content:

[Unit]
Description=Shutdown Fix Script

[Install]
WantedBy=multi-user.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/local/bin/shutdown-fix.sh

The ExecStart entry is executed on system startup and that’s why we use /bin/true, which does nothing. We are interested only in the ExecStop entry and that’s where you have to put the script from the previous step.

Finally, you have to enable the service and we’re done:

$ sudo systemctl --system enable shutdown-fix.service

So, this solution is systemd specific. If you still use the old sysvinit or Upstart or OpenRC, you have to use your distro specific’s way to run scripts on shutdown.

Kronometer 1.4.2 released and KF5 port completed

Kronometer 1.4.2 has been released.
It’s a minor release with just a couple of small fixes. The option to hide the statusbar, which was added in the 1.2.1 version, has been removed since it’s already provided by the standard KDE libraries: just go to the Settings menu and then click the Show Statusbar entry.
In the meantime, I have completed the port to KF5, introduced in my previous post. What I still have to do is decide how to release the ported version, since KF5 is not that much spread across distributions, at this time. Probably I’ll wait the other Extragear applications and I’ll see how they will handle the new releases after their ports.
Btw, if you want to have a look at the KF5 port, it’s just a matter of few git commands:

$ git clone git://anongit.kde.org/kronometer
$ cd kronometer
$ git checkout frameworks

Then you compile as always:

$ mkdir build && cd build 
$ cmake ..
$ sudo make install

You’ll find the compiled port installed at /usr/local/bin/kronometer.

Kronometer ported to KDE Frameworks 5

I’m very happy to announce that Kronometer has been ported to KDE Frameworks 5 (KF5), the new generation of KDE libraries provided to build KDE apps.

kronometer-kf5

As you can see in the screenshot, the frameworks version used is the 5.0, which has been released the past 7 July. However the Plasma version is still 4.13 and not the new Plasma 5.0, but I don’t think that will be a problem.
The kronometer version in the screenshot is 2.0.0 but only as a placeholder, I have not yet decided how to release the KF5 port. Currently all the work has been done in the frameworks branch on the git repository.

What’s working

The main use case, of course.You can start/pause/reset the stopwatch and record the lap times.
All the Kronometer Settings are working too.

What’s not working

The stopwatch persistence feature. So, at the moment is not possibile to restore a saved stopwatch or save a running stopwatch.
The motivation is that I was too eager to complete the port and this feature is the most boring to port.
Further, as you can see, the stopwatch digits are awfully “sides-cutted”, I still have to figure out what’s going on and how to fix it.

Kronometer 1.4 brings translations

I’m glad to announce that Kronometer 1.4 is now localized in over 12 languages. This is the first time that Kronometer brings translations to its users.
Thanks to the effort of the KDE translation team, Kronometer is completely localized in:

  • Brazilian Portuguese
  • Czech
  • Dutch
  • Finnish
  • French
  • German
  • Low Saxon
  • Polish
  • Portuguese
  • Spanish
  • Swedish
  • Ukrainian

Bosnian, Hungarian and Slovak translations are not completed, though.
Even the Kronometer Handbook is going to be translated and at the moment is available in Dutch, Swedish and Ukrainian.
The release 1.4 brings also a new feature and an important bugfix. The new feature is the automatic highlighting of the latest lap time recorded. This means that the latest lap time is always visible, while in the older releases it wasn’t. The bug fixed is the bug 336904, which was preventing the reset of the stopwatch under certain circumstances.
Finally, the release 1.4 is the first one hosted on the KDE servers, instead of this server. In this way you can use the KDE mirrors scattered all around the globe.

UPDATE (2014/07/09)

The release 1.4.1 fixes a small bug which was preventing the removal of the old upper toolbar (which was in the releases < 1.3.0) from the Kronometer UI.

Kronometer 1.3 joins KDE Extragear

The release 1.3 of Kronometer has been realized after a review made by the KDE team, which allows Kronometer to join the KDE Extragear repositories (in the Utils sub-module). The KDE Extragear is a collection of KDE applications which are released indipendently from the applications within the well-known KDE Software Compilation (KDE SC).
So, Kronometer is part of the KDE community. First of all, now Kronometer uses the official KDE bucktracking system. If you find a bug or if you want a new feature, feel free to report it here.
The next steps should be the text localization by the KDE guys and the packaging for the major linux distributions.

Speaking of the 1.3 release, the main change is a big redesign of the stopwatch display, based on the suggestions by the KDE team. In particular, a serious bug with many exotic fonts has been fixed. If you are interested, try to use an exotic non-monospace font in the display (on one of the previous releases), e.g. the Fontin font, and you will experience a very akward behaviour in the UI. In general the UI is now much more clean than before, as you can see in the following picture, where in the foreground there is the new release while in the background the previous one.

kronometer-comparison

Splitters between time components has been removed, since they were useless and even ambigous. There were the characters . and : used as digits dividers but they were useless too. Finally the upper toolbar has been removed since those actions are always available in the File menu.

What’s next?

Some small new features are in planning, like the system tray support, but of course the next big step in the kronometer development will be the port to KF5 (KDE Frameworks). This should help the porting of kronometer to other platforms, where sometimes the whole kdelibs package (which is required by kronometer) is hard to accept for such a simple application like kronometer.

Kronometer 1.2.2 released

A new bugfix release for kronometer is now available. The release 1.2.2 fixes a small bug that was preventing the compilation with older gcc versions. Since this bug was also well hidden I think that it’s worth a blog post.
The bug consisted in two trailing commas forgotten in two enum definitions.
Well, actually this is a bug only with certain versions of C and C++ and I didn’t know it.
A trailing comma in an enum declaration is a comma after the last enum member:

enum Color {
    RED,
    GREEN,
    BLUE, 
};

Let’s see where this code compiles and where it doesn’t (credits to this stackoverflow answer):

  • C89 doesn’t allow the trailing comma
  • C99 does allow it
  • C++98 and C++03 don’t allow it, since their C compatibility is based on C89
  • C++11 does allow it

The default compiler options used by kronometer are:
-Wall -Wextra -Werror -ansi -pedantic -std=gnu++0x.
Kronometer uses some small features of C++11, like nullptr. That’s why the flag -std=gnu++0x.
Here there was the problem: the flag -pedantic triggers a compilation error when it finds the trailing commas. This warning is ignored if C++11 is enabled, but only on recent enough versions of gcc. Since I use gcc 4.8.1 I didn’t find the bug. By removing those commas, now kronometer should be fully compilable also on older compilers.

Kronometer 1.2.1 is out

Kronometer 1.2.1 is now available. This release introduces the KDE compliant documentation in the DocBook format. To read the kronometer documentation just click the F1 button while kronometer is running. You can also open the Help menu and then click the Kronometer Handbook entry.
A new feature has also been added to kronometer. Now you can choose whether to show the Status Bar in the kronometer window. You can find this option in the Interface settings page in the kronometer settings dialog (Settings -> Configure Kronometer...).
You can see the difference in the following picture. The window in the foreground doesn’t show the status bar, while the one in the background does.

kronometer-statusbar

This option has been added to partially conform to a KDE design guideline. According to KDE, the applications should not provide a status bar to maximize vertical space for content.
While this is a valid general guideline, sometimes a status bar can be still useful and I think that kronometer is one of these exceptions.
The kronometer status bar is meant to tell the user about the current stopwatch state. Kronometer has three possible, self explanatory states,: inactive, running, paused.
The kronometer states can be recognized even without the status bar, thanks to a well designed UI. But I think that the presence of the status bar can help the user to quickly recognize the current state, reducing its effort. For this reason, I think that the decision is up the final user, since this is the KDE way of doing things. By default the status bar is enabled, but it’s a matter of one click to disable it.

Code Browser with KDE or Qt Projects

In my previous post I talked about the Woboq Code Browser.
While the instructions for compilation and usage are quite good, in this post I will try to summarize them for a specific use case: the generation of HTML files for a KDE/Qt application using the CMake build system, on the Linux platform. These instructions should also works for a generic CMake project using plain C++.

Requirements

This post assumes that you are a Qt/KDE developer on a Linux system. This means that you should already have all the needed stuff (git, cmake, etc.). Just make sure to have clang (and llvm-config) installed under /usr/bin. This makes the process easier.

Compilation

First of all, you need to compile the codebrowser.

  1. Clone the codebrowser source files:
    $ git clone https://github.com/woboq/woboq_codebrowser.git
    $ cd woboq_codebrowser/
  2. Start compiling:
    $ mkdir build && cd build
    $ cmake .. -DLLVM_CONFIG_EXECUTABLE=/usr/bin/llvm-config -DCMAKE_BUILD_TYPE=Release
    $ make

Now you have the two codebrowser components: build/generator/codebrowser_generator and build/indexgenerator/codebrowser_indexgenerator.

Installation

Since clang expects to find the system libraries in ../lib relative to the executable, you have two choices:

  1. Install the codebrowser components. In this way you can use the codebrowser everywhere:
    $ sudo install -m755 generator/codebrowser_generator /usr/bin
    $ sudo install -m755 indexgenerator/codebrowser_indexgenerator /usr/bin
  2. Link the system libraries to the build/ directory:
    $ ln -s /usr/lib .
    I don’t like the latter because you need to type the extended path when using the codebrowser; but it’s up to you.
  3. Finally, you need to copy the data/ directory to the directory that will contain the generated HTML files. The output directory in this example will be ~/public_html:
    $ cp -r ../data/ ~/public_html

Usage

Now you are ready to generate the HTML for your KDE or Qt foo project. Just make sure that you have a git local repository for foo.

  1. Generate the compile_commands.json. This will tell to the codebrowser what source files has to process:
    $ cd /path/to/foo/local/repo
    $ mkdir build && cd build
    $ cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
  2. Generate the source code HTML pages. The output directory in this example will be ~/public_html:
    $ cd ..
    $ git ls-files | egrep "\.cpp$" | xargs codebrowser_generator -b build/ \
    -p foo:$PWD:`git describe` -o ~/public_html/foo
    If you are interested, codebrowser_generator -help will explain the meaning of the used options.
  3. (Last step!) Generate the HTML index:
    $ codebrowser_indexgenerator ~/public_html/foo

Done. Simply open in your preferred browser ~/public_html/foo/index.html and enjoy it!

(Optional) Optimization

By default, for (e.g.) a KDE project the codebrowser generates not only the code for the project itself, but also for all the included libraries. This translates in (at least) the whole KDE, the whole Qt and many C++ standard headers. If you want to upload the generated HTML on your webserver, this might be a problem if you have storage concerns, for example if you are on a shared server.
Fortunately, the codebrowser gives you the option to link external generated libraries. Just use the -e option like in this way:

$ git ls-files | egrep "\.cpp$" | xargs codebrowser_generator \
-e include:/usr/include:http://code.woboq.org/kde -b build/ \
-p foo:$PWD:`git describe` -o ~/public_html/foo