...making Linux just a little more fun!

2-Cent Tips

2-cent tip - rpath

Oscar Laycock [oscar_laycock at yahoo.co.uk]


Thu, 8 Oct 2009 02:17:44 -0700 (PDT)

This all started when I wanted to run the latest version of Firefox. I decided to build my own from source. But my kernel, assembler, compiler and C library were too old - in fact, nine years old. So I built new ones under the /usr/local directory. I used the Linux From Scratch book as a guide.

Now when I build new programs, I set the GCC compiler's "rpath" option to point to the libraries in /usr/local rather than in the usual /lib and /usr/lib. The rpath is a list of libraries at the start of a program that can tell Linux where to look for shared libraries when Linux runs a program. A program called the "dynamic linker" does the job. On my system it is "/lib/ld-linux.so.2". You can see a program's rpath by running a command such as "readelf -a /bin/ls". Of course, normally there isn't one. Also you can watch the dynamic linker at work using the "ldd" command. I set GCC's rpath by including it in the CFLAGS environment variable when configuring programs before building them. (You typically type "configure", "make" and "make install" to build a program.) I found a small number of programs ignore CFLAGS, so I made the gcc program a shell script, which then calls the real gcc with the right rpath option.

So I can now run old commands such as "ls" and "find" alongside new programs such as the KDE suite. The now eleven-year-old commands run fine on top of the recent kernel. I also put /usr/local/bin at the start of my path. This may be a security risk but my PC is not connected to the internet or a network.

There is a bit more too it. So here is the CFLAGS setting I used only few days ago:

export CFLAGS="-O2 -I. -I.. -I/usr/local/myglibc27/include -I/usr/local/include
-L/usr/local/myglibc27/lib -L/usr/local/lib -L/usr/local/lib/gcc/i686-pc-linux-gnu/4.2.3 -Wl,-rpath=/usr/local/myglibc27/lib:/usr/local/lib:/usr/local/lib/gcc/i686-pc-linux-gnu/4.2.3,-dynamic-linker=/usr/local/myglibc27/lib/ld-linux.so.2 -specs=/home/oscar/tmp/glibc/myspecs08scr -march=pentium2"

I also similarly set these environment variables: LDFLAGS, CXXFLAGS, CPPFLAGS, LIBCFLAGS, LIBCXXFLAGS. You can see that the include file path (-I's) and libraries path (-L's) match the rpath. The "-I. -I.." is there because some programs need to look at the header files in the build directory first - a bit of a quick fix. Notice how I now have two separate dynamic linkers on my PC. I had to edit the compiler specs file a little. Here is a section to really confuse you:

*startfile:
%{!shared: %{pg|p|profile:/usr/local/myglibc27/lib/gcrt1.o%s;pie:/usr/local/myglibc27/lib/Scrt1.o%s;:/usr/local/myglibc27/lib/crt1.o%s}}    /usr/local/myglibc27/lib/crti.o%s %{static:/usr/local/lib/gcc/i686-pc-linux-gnu/4.3.2/crtbeginT.o%s;shared|pie:/usr/local/lib/gcc/i686-pc-linux-gnu/4.3.2/crtbeginS.o%s;:/usr/local/lib/gcc/i686-pc-linux-gnu/4.3.2/crtbegin.o%s}

I think this is choosing which C runtime code to put at the start of the program.

And here is the shell script that stands in for gcc:

[ ... ]

[ Thread continues here (2 messages/4.00kB) ]


Two-cent Tip: bash script to create animated rotating mark

Mulyadi Santosa [mulyadi.santosa at gmail.com]


Sat, 24 Oct 2009 16:34:04 +0700

During my boring saturday, I was thinking to create simple animated cycling mark. Here's the script:

$ while(true); do for a in \\ \| \/ -; do echo -n $a; sleep 1 ; echo
-n -e \\r ; done; done

Notice the usage of escaped "\r" (carriage return) and "-n" option to display continous marks at the same line and at the same column

-- 
regards,
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com

[ Thread continues here (5 messages/5.54kB) ]


2-cent Tip: Load Python modules at Startup

Amit Saha [amitsaha.in at gmail.com]


Thu, 22 Oct 2009 18:49:49 +0530

Hello:

I have been using CPython as a calculator, while I do all those number crunching in C. SO, 'import math' is a must.

This is what I did:

- Create a file: .pythonrc in my $HOME and place this line:

   import math

- Now in your BASH, .bashrc or similar: export PYTHONSTARTUP= $HOME/.pythonrc

Everytime you start Python interactively, you should have the 'math' module already imported.

$ python
Python 2.6.4rc1 (r264rc1:75270, Oct 10 2009, 02:40:56)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> math.pi
3.1415926535897931

Hope this helps.

-- 
Journal: http://amitksaha.wordpress.com,
µ-blog: http://twitter.com/amitsaha


Two-cent Tip: Detexify

Jimmy O'Regan [joregan at gmail.com]


Wed, 14 Oct 2009 11:45:32 +0100

http://detexify.kirelabs.org/classify.html

Nifty website: draw the TeX symbol you were thinking of, and it tells you which one it (probably) is. Source (MIT license) here: http://github.com/kirel/detexify

[ Thread continues here (3 messages/1.80kB) ]


2-cent Tip: Piping to GNU Plot from C

Amit Saha [amitsaha.in at gmail.com]


Sun, 4 Oct 2009 16:03:05 +0530

Hello TAG:

Can this be a possible 2-cent tip?

Couple of things first up:

* GNU plot supports piping, So, echo "plot sin(x)" | gnuplot will plot the sin(x) function.

* However, the plot disappears even before you could see it. For that echo "plot sin(x)" | gnuplot -persist , is useful. It persists the GNU plot main window

The usefulness of the second point is that, if you have a "pipe descriptor" describing a pipe to the open GNU plot instance , you can plot more plots on the first plot, without opening a new GNU plot instance. We shall be using this idea in our code.

#include <stdio.h>
#define GNUPLOT "gnuplot -persist"
 
int main(int argc, char **argv)
{
        FILE *gp;
        gp = popen(GNUPLOT,"w"); /* 'gp' is the pipe descriptor */
        if (gp==NULL)
           {
             printf("Error opening pipe to GNU plot. Check if you have it! \n");
             exit(0);
           }
 
        fprintf(gp, "set samples 2000\n");
        fprintf(gp, "plot abs(sin(x))\n");
        fprintf(gp, "rep abs(cos(x))\n");
        fclose(gp);
 
return 0;
}

The above code will produce a comparative plot of absolute value of sin(x) and cos(x) on the same plot. The popen function call is documented at http://www.opengroup.org/pubs/online/7908799/xsh/popen.html. This code/idea should work on GCC and Linux and any other language and OS that supports piping.

Utility: If you have a application which is continuously generating some data, which you will finally plot, then you can plot the data for every new set of data- that gives a nice visualization about how the data is changing with the iterations of your application. This is a perfect way to demonstrate convergence to the best solutions in Evolutionary Algorithms, such as Genetic Algorithms.

Best, Amit

-- 
Journal: http://amitksaha.wordpress.com,
µ-blog: http://twitter.com/amitsaha

[ Thread continues here (4 messages/7.26kB) ]



Talkback: Discuss this article with The Answer Gang

Copyright © 2009, . Released under the Open Publication License unless otherwise noted in the body of the article. Linux Gazette is not produced, sponsored, or endorsed by its prior host, SSC, Inc.

Published in Issue 168 of Linux Gazette, November 2009

Tux