printf to stderr in C/C++

Problem:

You want to use printf to print something to the stderr stream (instead of stdout)

Continue reading →

Posted by Uli Köhler in Allgemein

Compiling LevelDB as LLVM binary on Linux

Some time ago I wrote a guide on how to compile and install LevelDB on Linux.

Recently I’m desperately trying to get into LLVM and a tutorial series on how to use LLVM with C/C++ is coming shortly.

As I’m using LevelDB in many of my projects I’d like a way of generating a LLVM IR (intermediate representation) of the LevelDB C++ source – I could link a LLVM program to the native binary, but in order to profit from LLVMs features I suppose using IRs for as many dependencies as possible is the way to go.

Generally there are two ways to go:

  1. Use the g++ LLVM backend
  2. Use clang++

I usually tend to use clang++ for LLVM tasks because even with colorgcc and some recent improvements in gcc error message generation I prefer the clang++ error messages, even if I have way more experience with gcc error messages. Additionally the g++ with LLVM backend does seem to have some bugs, including interpreting -emit-llvm as -e -m -i …, plus recent distribution versions don’t work too well with the LLVM gold plugin and it has proved difficult to tell GCC reliably that it shall use llvm-ld as linker.

Continue reading →

Posted by Uli Köhler in C/C++, Databases, LLVM

How to fix “The name org.freedesktop.Hal was not provided by any .service files”

Problem:

You want to use the pmi tool from the powermanagement-interface package, e.g. to standby your Linux computer using the command line, but you get an error message like this:

Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Hal was not provided by any .service files

Continue reading →

Posted by Uli Köhler in Allgemein

How to solve LT_INIT command not found on Linux

Problem:

You are running a configure script on Linux. It exits with a message like this:

./configure: line 3638: LT_INIT: command not found

Continue reading →

Posted by Uli Köhler in Linux

NodeJS: Get IP addresses of local interfaces

Problem:

You want to get the IPv4 and/or IPv6 addresses of the local computer’s network interfaces using NodeJS in an OS-independent manner.

Continue reading →

Posted by Uli Köhler in Allgemein

GCC error: declaration of … shadows a parameter

Problem:

You encounter a GCC error message of the form

error: declaration of ... shadows a parameter

Continue reading →

Posted by Uli Köhler in Allgemein

Efficiently encoding variable-length integers in C/C++

Using fixed width integers is space-inefficient in many cases, especially if the majority of values are low and only use the less-significant bytes.

This guide describes the basics of varint (varying-length integer) encoding while focusing on C++ as programming language, but the basic concepts apply to any language.

Varint encodings use only the bytes that are needed to represent you integer value appropriately. A varint algorithm can represent the number 10 in only one byte while using 4 bytes to encode 800000000 (800 million). In many application this yields a significant overhead reduction since you would need to use larger integers if there is a slight change that your values grow beyond the boundary of the integer type that is applicable for the majority of your values. Additionally, you usually can only use 8,16,32 or 64 bit integers while 48 bit integers need to be coded manually in most languages. For example, if most of your values are between 0 and 100, but a few might be larger than 16384 (for unsigned integers), you would usually use a full 32-bit integer, even if most values could be represented by a single byte.

Continue reading →

Posted by Uli Köhler in Allgemein

Creating a highly compressed SquashFS from a folder

Problem:

You have a large compressible read-only folder that eats up a lot of disk space. You want to compress it using SquashFS.

Continue reading →

Posted by Uli Köhler in Linux

C++: Check if file exists

Problem:

In C++ you want to check if a given file exists, but you can’t use stat() because your code needs to work cross-plaform.

Continue reading →

Posted by Uli Köhler in C/C++

Enable Menu-Style Autocompletion in ZSH

Problem:

You have installed zsh but you don’t see the menu-style autocompletion (where you can navigate the suggestions using the arrow keys on the keyboard)

Continue reading →

Posted by Uli Köhler in Shell

Move Minimize, Maximize and Close to the right in Ubuntu Unity

In more recent Ubuntu Versions, the minimize, maximize and close icons have moved to the left upper corner of the window.

If you want them to show up on the right side instead, follow this guide:

  1. Open a terminal (e.g. click on Ubuntu Dashboard and type Terminal, then click on Terminal)
  2. Copy and paste this text into the terminal (Ctrl+V doesn’t work here, use right-click -> insert)
    gconftool-2 -s /apps/metacity/general/button_layout —type=string “menu:minimize,maximize,close”
  3. Press Return / Enter
  4. The icons should shift to the right immediately
Posted by Uli Köhler in Allgemein

A text-to-Brainfuck/RNA converter in ANSI C99

Brainfuck encoder

The following small ANSI C99 program reads a String from stdin and prints out a Brainfuck program that prints the same String on stdout.

Compile using gcc -o bf bf.c

Use it like this:

cat my.txt | ./bf > my.bf

Source code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
    unsigned char c;
    unsigned char curval = 0;
    //Initialize reg+1 with 8
    while(1) {
        c = getchar();
        if(feof(stdin)) {break;}
        while(curval != c) {
            if(curval < c) {
                putchar('+');
                curval++;
            } else if(curval > c) {
                putchar('-');
                curval--;
            }
        }
        putchar('.');
    }
}

How does it work?

Basically it uses just one of the registers of the Brainfuck Turing machine and incremets or decrements the register to be able to print out the next byte. It doesn’t use any of the more ‘advanced’ features in Brainfuck like loops.

Continue reading →

Posted by Uli Köhler in C/C++, Fun

How to resolve ADB Sideload “error: closed”

Problem:

You are using adb sideload to communicate with your Android device (e.g. to flash the Nexus 4 using Clockworkmod recovery), but every time you try to execute `adb sideload` you get this error message:

error: closed

Continue reading →

Posted by Uli Köhler in Android

Compiling & Installing LevelDB on Linux

Update: Please also take a look at this followup article for an automatic compilation script that builds Ubuntu DEB packages!

Problem:

You want to compile and install LevelDB (including development headers) on your Linux computer. ./configure && make && make install does not work so you don’t know how to do this.

or:

You have successfully compiled LevelDB, but make install doesn’t work (there is no official installation procedure yet) and you don’t know how to install it to your system

Continue reading →

Posted by Uli Köhler in Databases

Scalar vs packed operations in SSE

If you look at any SSE instruction table, you might notice that there are two basic types of operations:

  • Packed instructions (the assembly instruction ends with PS)
  • Scalar instructions (the assembly instruction ends with SS)

For most operations, there are two versions, one packed and one scalar.

What’s the difference between them? It’s pretty simple:

  • Scalar operations operate on only one element, for example a single integer.
  • Packed operations operate on any element in the vector in parallel, e.g. they multiply 4 32-bit integers in a single instruction.

SSE gains it performance from using packed operations implementing the SIMD paradigm (using a single instruction, multiple values are processed). However, it is occasionally useful to avoid expensive copying by using scalar operations operation on the SSE registers.

Also see the Original source

Posted by Uli Köhler in Performance

Encoding a Video for HTML5 using ffmpeg

Problem:

You have a video file and want to encode it to support all three HTML5 codecs currently available (WebM, Theora and H.264) on the majority of devices.

Continue reading →

Posted by Uli Köhler in Allgemein
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPTPrivacy &amp; Cookies Policy