Top linux Questions

List of Tags
530
chmurli

I bumped into this strange macro code in /usr/include/linux/kernel.h:

/* Force a compilation error if condition is true, but also produce a
   result (of value 0 and type size_t), so the expression can be used
   e.g. in a structure initializer (or where-ever else comma expressions
   aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))

What does :-!! do?

Answered By: John Feminella ( 648)

This is, in effect, a way to check whether the expression e can be evaluated to be 0, and if not, to fail the build.

The macro is somewhat misnamed; it should be something more like BUILD_BUG_OR_ZERO, rather than ...ON_ZERO. (There have been occasional discussions about whether this is a confusing name.)

You should read the expression like this:

sizeof(struct { int: -!!(e); }))
  1. (e): Declare an expression e.

  2. !!(e): Negate it twice. This produces 0 if e was 0 originally, or a nonzero positive number if it wasn't.

  3. -!!(e): Multiply the value by -1. This results in 0 if step 2 was 0, or a negative number if it wasn't.

  4. struct{int: -!!(0);} --> struct{int: 0;}: If it was zero, then we declare a struct with an integer bitfield that has width zero. Everything is fine and we proceed as normal.

  5. struct{int: -!!(1);} --> struct{int: -1;}: On the other hand, if it isn't zero, then it will be some negative number. Declaring a bitfield with negative width is a compilation error.

So we'll either wind up with a bitfield that has width 0 in a struct, which is fine, or a bitfield with negative width, which is a compilation error. Then we take sizeof that field, so we get a size_t with the appropriate width (which will be zero in the case where e is zero).


Some people have asked: Why not just use an assert?

keithmo's answer here has a good response:

These macros implement a compile-time test, while assert() is a run-time test.

Exactly right. You don't want to detect problems in your kernel at runtime that could have been caught earlier! It's a critical piece of the operating system. To whatever extent problems can be detected at compile time, so much the better.

365
Lakshman Prasad

Which is the best gui client on Linux for Git.

Update: After checking out all of the GUIs mentioned here,

git cola seems to work well for committing/pushing

gitk seem to work the best for examining history and

giggle is awesome for watching the diffs.

I use command line for committing, web trac interface for viewing history, in a rare occasions, and accept giggle as giggle is what, I think, one needs as a desktop git gui, with occasional git cola and gitk.

Answered By: Jakub Narębski ( 337)

Take a look at "Graphical Interfaces" section of InterfacesFrontendsAndTools page on Git Wiki. There you have mentioned:

  • gitk - graphical history browser, in Tcl/Tk, distributed with Git (usually in gitk package)
  • git gui - graphical commit tool, in Tcl/Tk, distributed with Git (usually in git-gui package)
  • QGit - uses Qt toolkit
  • Giggle - uses GTK+ toolkit
  • git-cola - uses PyQt4
  • gitg - GTK+/GNOME clone of GitX
  • tig - text mode interface for git, is GUI and pager, uses ncurses
328
ign

I got addicted to Notepad++ on Windows and I've recently moved to Ubuntu. I'm missing a tool as such. What is a good alternative?

Answered By: Konrad Rudolph ( 97)

Doesn't the bundled editor GEdit offer much the same functionality? If you need a richer functionality, use a “true” Linux editor such as GVim or Emacs.

To harness the full power of GEdit, have a look at this blog posting entitled Pimp my Gedit. Notepad++ is no match to this.

260
Bill the Lizard

I've used the following script to see if a file exists:

#!/bin/bash
FILE=$1

if [ -f $FILE ];
then
   echo "File $FILE exists."
else
   echo "File $FILE does not exist."
fi

What's the correct syntax to use if I only want to check if the file does not exist?

#!/bin/bash
FILE=$1

if [ $FILE does not exist ];
then
   echo "File $FILE does not exist."
fi
Answered By: John Feminella ( 422)

Bash has a "not" logical operator, which is the exclamation point (similar to many other languages). Try this:

if [ ! -f /tmp/foo.txt ];
then
    echo "File not found!"
fi
247
Sasha

I have just installed Ctags (to help with C++ development) with my Vim (or rather gVim), and would like to find out your favorite commands, macros, shortcuts, tips that go along with it...

Share your best arsenal. What other Vim add-ons you would recommend for C++ on Vim development?

EDIT What other add-on you would use in conjunction with Ctags?

EDIT2 What version of gVim you use with tags? Does it make a difference?

EDIT3 How do you enhance your programming experience for both big and small projects?

Answered By: Amjith ( 187)

C-] - go to definition
C-T - Jump back from the definition.
C-W C-] - Open the definition in a horizontal split

Add these lines in vimrc
map <C-\> :tab split<CR>:exec("tag ".expand("<cword>"))<CR>
map <A-]> :vsp <CR>:exec("tag ".expand("<cword>"))<CR>

C-\ - Open the definition in a new tab
A-] - Open the definition in a vertical split

After the tags are generated. You can use the following keys to tag into and tag out of functions:

Ctrl-Left_MouseClick - Go to definition
Ctrl-Right_MouseClick - Jump back from definition

232
javac

I've heard that you need to get a Mac if you want to develop iPhone apps. Is this true?

Is it possible to develop iPhone apps using Linux? If yes, what do I need and where do I download the necessary tools?

Answered By: Chaos ( 116)

To provide a differing response, I'm running OS X and Xcode on a virtualised (VMware) machine on Linux. CPU is a Core2Quad (Q8800), and it is perfectly fast. I found a prebuilt VM online (I'll leave it to you to find) Xcode/iPhone development works perfectly, as does debugging via USB to the phone itself.

It actually surprised me a lot - but I've had no issues at all.

168
c++, c, api, gui, linux
David C

I hope this doesn’t come across as a stupid question but it’s always something I have wondered. Both Windows (Win32 API) and OS X (Cocoa) have their own APIs to handle windows, events and other OS stuff. I have never really got a clear answer as to what Linux’s equivalent is.

I have heard some people say GTK+, but GTK+ being cross platform, how can it be native?

Answered By: Joni ( 285)

In Linux the graphical user interface is not a part of the operating system. The graphical user interface found on most Linux desktops is provided by software called the X Window System, which defines a device independent way of dealing with screens, keyboards and pointer devices.

X Window defines a network protocol for communication, and any program that knows how to "speak" this protocol can use it. There is a C library called Xlib that makes it easier to use this protocol, so Xlib is kind of the native GUI API. Xlib is not the only way to access an X Window server; there is also XCB.

Toolkit libraries such as GTK+ (used by GNOME) and Qt (used by KDE), built on top of Xlib, are used because they are easier to program with. For example they give you a consistent look and feel across applications, make it easier to use drag-and-drop, provide components standard to a modern desktop environment, and so on.

How X draws on the screen internally depends on the implementation. X.org has a device independent part and a device dependent part. The former manages screen resources such as windows, while the latter communicates with the graphics card driver, usually a kernel module. The communication may happen over direct memory access or through system calls to the kernel. The driver translates the commands into a form that the hardware on the card understands.

166
ksuralta

How do you measure the memory usage of an application or process in Linux?

From the blog article of Understanding memory usage on Linux, "ps" is not an accurate tool to use for this intent.

Why ps is "wrong"

Depending on how you look at it, ps is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running. Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported by ps are almost definitely "wrong".

Answered By: ypnos ( 99)

With ps or similiar tools you will only get the amount of memory pages allocated by that process. This number is correct, but:

a) does not reflect the actual amount of memory used by the application, only the amount of memory reserved for it

b) can be misleading if pages are shared, for example by several threads or by using dynamically linked libraries

If you really want to know what amount of memory your application actually uses, you need to run it within a profiler. For example, valgrind can give you insights about the amount of memory used, and, more importantly, about possible memory leaks in your program.