Posts Tagged ‘linux’

GCC — 64bits addressing: function returns 32bits pointer

Today, it was the second time I stepped on an interesting problem with GCC  (well, it is actually a behavior). I created a function which returns a void* in a .c file. This C file was then compiled and added in a library (.a). When I used this function in an application, I was getting a void* pointer were the 32 most significant bits were either zeroed (0x00000000...) or set to 1 (0xFFFFFFFF...). My application is 64bit!!

For example, the debug prints I added would return:

[lib] allocated 0x7f756d6fa048
[app] allocated 0x6d6fa048

where you can see the “conversion”.

What was the problem? After some time of debugging, I realized that I had forgotten to include the aforementioned function in the corresponding header file :|. So, although GCC could find the function in the library I was linking the application to, I guess it was assuming a wrong return value/header for that function.

The conclusion: Be more careful 😉

PS. I am using the following version of GCC:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' 
--with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ 
--prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib 
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix 
--with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ 
--enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes 
--enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror 
--with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu 
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

Netbeans 7.0 on Ubuntu Installation – OpenJDK problem

Installation

The installation process is simple:

  1. Download the version you want to install from here.
  2. Navigate to the folder were the .sh file was downloaded.
  3. Give executable permissions to the .sh file.
  4. Execute the installer (.sh) file either pretending sudo command (in order to install it in the default /usr/local/netbeans-7.0 location) or without (in order to install it in the /home/Username/netbeans-7.0/ location)
  5. Use the Graphical Installer to do the installation.

For example:

[~] $ cd ~/Desktop/                                                     # Step 2
[~/Desktop] $ ls -l netbeans-7.0-ml-cpp-linux.sh 
-rw-r--r-- 1 *** *** 45967360 2011-05-03 09:21 netbeans-7.0-ml-cpp-linux.sh
[~/Desktop] $ chmod +x netbeans-7.0-ml-cpp-linux.sh                     # Step 3
[~/Desktop] $ ls -l netbeans-7.0-ml-cpp-linux.sh 
-rwxr-xr-x 1 *** *** 45967360 2011-05-03 09:21 netbeans-7.0-ml-cpp-linux.sh
[~/Desktop] $ sudo ./netbeans-7.0-ml-cpp-linux.sh                       # Step 4
Configuring the installer...
Searching for JVM on the system...
Extracting installation data...
Running the installer wizard...

OpenJDK Problem

The problem with OpenJDK

Configuring the installer...
Searching for JVM on the system...
Extracting installation data...
Running the installer wizard...
null

while installing Netbeans 6.9.1 on Ubuntu is the same for Netbeans 7. Netbeans needs Sun JDK to work, otherwise even the installation wizard (GUI) does not appear.

Read more details about the problem and how to solve it in the post about Netbeans 6.9.1.

Selecting a random file/folder of a folder in Linux

I always had the following problem: “How to select which movie (from the ones in my collection) to watch“. In the past, I had used the random (pseudo-random to be precise) capabilities of C, Java, Erlang, and Javascript to select the movie.

Today I wrote a simple script that selects a random content (file or folder) within a folder:

#! /bin/sh
 
if [ $# -gt 0 ]; then
    cd "$1";
fi
 
ls -1 | awk 'BEGIN {srand()}
	{x[NR] = $0}
	END {print "Selected", x[1 + int(rand() * NR)]}'

Save it in a file (lets say srandom, make it executable:

$ chmod +x srandom

and execute it either with 0 or 1 arguments. Without an argument, the selection is done from the contents of the srandom‘s container, else the path given as an argument is used.

$ ./srandom.sh
Selected lrandom.html
$ ./srandom.sh /bin
Selected gzexe

Update

A much more elegant solution:

$ ls -1 | shuf -n1

Removing the SVN metadata files (.svn/) in Linux

If you ever want to remove a folder from SVN control (clean the folder by removing the .snv metadata folders) in Linux, you can simply run the following command:

rm -rf $(find path/to/the/CORRECT/folder -type d -name .svn)

where:

  • rm -rf is remove recursive force. Recursive for deleting the directories and their contents recursively and force for no prompting (for example, for write protected files)
  • $(..) is the command substitution, that takes the output of a command and uses it as input for another
  • find path/to/the/CORRECT/folder -type d -name .svn is searching (recursively) for folders (d for directory) with the name .svn1 in the directory pointed by path/to/the/CORRECT/folder

Be careful to use the correct path, else you can remove the svn metada for the wrong folder! A good approach can be to execute the find command without the rm, check the output, and then proceed to the remove :-).

1case sensitive, use -iname for insensitive

Extracting citations from a BibTex file using Linux terminal

I had a big (around 40 entries) BibTex file with the references of some papers I studied and I wanted to extract the citations in the format used for citing in Latex (\cite{AuthorYear}). Just today I read some tutorials about awk, so I thought “Let’s use it!!”.

An example BibTex file:

@article{Kotselidis2010,
author = {Kotselidis, Christos and Lujan, Mikel and Ansari, Mohammad and Malakasis,
    Konstantinos and Kahn, Behram and Kirkham, Chris and Watson, Ian},
doi = {10.1109/IPDPS.2010.5470460},
isbn = {978-1-4244-6442-5},
journal = {2010 IEEE International Symposium on Parallel \&
    Distributed Processing (IPDPS)},
pages = {1--12},
publisher = {Ieee},
title = {{Clustering JVMs with software transactional memory support}},
url = {http://ieeexplore.ieee.org/lpdocs/epic03/wrapper.htm?arnumber=5470460},
year = {2010}
}
@phdthesis{Zhang2009c,
author = {Zhang, Bo},
keywords = {cache-coherence,contention manager,distributed transactional memory},
title = {{On the Design of Contention Managers and Cache-Coherence Protocols for
    Distributed Transactional Memory}},
year = {2009}
}

Solution

awk 'BEGIN{FS="[{,]"} /@/ {print "\\cite{"$2"}"}' filename.bib

\cite{Zhang2009c}
\cite{Kotselidis2010}

In order to save the output in a file named cites.txt:

awk 'BEGIN{FS="[{,]"} /@/ {print "\\cite{"$2"}"}' filename.bib > cites.txt

Hint: Use “>>” if you want to append the output. Single > creates a new file (if not existing), or empties the existing one and then appends the content..

If you want to know my “implementation” process, continue reading 😉
Read the rest of this entry »

Netbeans 6.9.1 on Ubuntu 10.10 Installation – OpenJDK problem

I wanted to install Netbeans IDE 6.9.1 (C/C++ version, but I believe that the problem is generic) on my Ubuntu 10.10 laptop, so after downloading the latest version from netbeans.org, I tried to install it:

$ ls -l netbeans-6.9.1-ml-cpp-linux.sh
-rw-r--r-- 1 ** ** 36699136 2011-02-06 00:58 netbeans-6.9.1-ml-cpp-linux.sh
$ chmod +x netbeans-6.9.1-ml-cpp-linux.sh
$ sudo ./netbeans-6.9.1-ml-cpp-linux.sh

and got the following output:

Configuring the installer...
Searching for JVM on the system...
Extracting installation data...
Running the installer wizard...
null

The installation wizard (GUI) did not appear at all.

Luckily, I “immediately” thought “hmm, I re-installed Ubuntu quite recently, did I install JRE?”
So, I checked if I had “Java” installed and checked that everything is “good”:.

$java -version
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.5) (6b20-1.9.5-0ubuntu1)
OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)

As you can see, I had OpenJDK installed..
For some reason, which I don’t know, I had already installed the Sun JDK, but not removed the OpenJDK, or at least set the java to point to the Sun version.

So, using synaptic, I unistalled OpenJDK (you can (un)install OpenJDK and Sun JDK from the Software Center also):

$java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Server VM (build 17.1-b03, mixed mode)

And then, everything worked fine..

Linux Commands: a useful article

Yesterday, I found an article called “A Practical Guide to Linux Commands“; a compact guide to several linux command use cases, such as backup and compression, searching the filesystem, etc. I have to admit that it was one of the most interesting/useful articles I have ever read, thus I share it with you.

The article is posted on linuxconfig.org and you can access it here.

Really simple batch renaming in Linux

While working on the photos section of trigonakis.com, I wanted to batch rename some photos, so after the renaming their new name would be prefix_i, where i is a incremental value. The following (stupidly) simple bash script provides the desired functionality:

#!/bin/bash
 
INDEX=1
 
for i in *.$1; do
  echo item $INDEX: "$i" renamed to $2_$INDEX.$1
  mv "$i" $2_$INDEX.$1
  let INDEX=INDEX+1
done

Usage:

#make the script executable
chmod +x scriptname.sh
#move the folder containing the files to be renamed
mv from_path/scriptname.sh to_path/
#run the script
./scriptname.sh FILE_EXTENSION NAME_PREFIX
#example:
./rnm.sh JPG photo