Category Archives: c

Problem with #define from other libraries

While building a qt project with libxdo xdo.h I faced some strange problems with qt enum names conflicting with X #define macros.

There is no standard way of including headers the one which works is the best. For Qt I think it is recommended to include qt headers first then your files and other library files. This worked for me. To begin with I started to do #undef before include for each terms for which compiler was complaining. It was becoming really messy as there were many conflicts.

Another issue was below error

undefined reference to `xdo_new(char*)'

Inline with xdo.h I was including it in C++ project. libxdo is C project and it doesn’t have extern “C” declaration as other libraries have.

To fix above just use below:

extern "C" {
#include <xdo.h>



Running application just with X11

I have ubuntu core on my system without any UI just the bare bone minimum ubuntu core. I want the system to run some qt application nothing else.

There was two ways to it either I can take the Ubuntu mate and remove useless stuff and make my process run at startup with auto-login or to use bare bone ubuntu core and then just add the things needed to run my application.

Actually I would not have needed anything else for qt application and just used –platform linuxfb flag to run the Qt app with gun on the frame buffer. I actually did that but the problem was frame buffer approach was too slow I can actually see the screen restoring from top to bottom and for my application there was some times where screen was updated quite frequently. So I needed some other windowing system library like Wayland or X11 to handle the slow Frame buffer approach.

I chose X11 first and with some hiccups I made it work woohoo!!

You just need to install xorg and then use startx. I have put this in startup script inside /etc/init.d I will cover application starting at boot up in some other post.

apt-get install  libqt5declarative5  
# for my qt application this will install qt5-default and other qt related dependencies

It was quite a great learning and good experience to make everything work. Now the ubuntu core boots with my splash and starts all three of my qt apps with Gui and works well. It is very fast and no extra processes consuming my processor or hogging the system’s RAM. No window manager no session manager no desktop manager.

You can start any application simply by

startx application_name

How to give permissions to devices

Many a time you face with this challenge that as a user you do not have permission to read write a device file in Linux without becoming sudoer.

Example: Let us say you have a USB device say a camera or any other device file let us say /dev/uinput the file for multi touch.
You want to read write these files in your program.

You can only run this program with sudo as the device files will not be accessible otherwise.

There is a workaround to make these files read/writable once and for all by doing something like:

sudo chmod 777 /dev/uinput

But You need to do above on each restart and also not a great way to handle this problem.

There is an easy way by creating rules file inside /etc/udev/rules.d.
For example:

# DVP sensor driver rule

KERNEL=="video0", SUBSYSTEM=="video4linux", ATTRS{name}=="vfe_0", GROUP="video", MODE:="766"

c++ STL map

dictionary data structure.


example with question answer :

Given names and phone numbers, assemble a phone book that maps names to their respective phone numbers.

After this dictionary is created you will then be given an unknown number of names to query your phone book for; for each queried, print the associated entry from your phone book (in the form name=PhoneNumber ) or  ( Not Found) if there is no entry for .

Input Format

The first line contains an integer,N , denoting the number of entries in the phone book.
Each of the subsequent lines describes an entry in the form of space-separated values on a single line. The first value is a friend’s , and the second value is an -digit .

After the lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a to look up, and you must continue reading lines until there is no more input.


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int N;
map<string,int> dict;
for(int i = 0; i < N; i++){
string name;
int number;
dict[name] = number;
string line;
vector<string> inp;
//remove top new line
getline(std::cin, line);
while (getline(std::cin, line)){
for(int i = 0; i < inp.size(); i++){
if(dict.find(inp[i]) != dict.end()){
cout<<"Not found"<<endl;
return 0;