C/C++ Programming

GCC predefined macros

To see all the system defined macros, use "cpp -dM /dev/null".

Compile/link OpenGL in Mac OS X

In Mac OS X, the GLUT header file is "<GLUT/glut.h>" instead of "<GL/glut.h>". You can use the following lines in your source codes:

To link a OpenGL program, you should: "gcc -framwork openGL -framework glut opengl.c -o opengl".

Statically and dynamically linking

Sometimes we intend to statically link to one library and dynamically link to another one. To achieve this (on my Linux virtual machine), you can, for example, "gcc a.o b.o -Wl,-Bstatic -lglut -Wl,-Bdynamic -lGL -lGLU". Basically, "-Wl" make s gcc transfer "-Bstatic" and "-Bdynamics" to the linker "ld". This tip comes from this page.

Writing to a memory mapped file

To write to a memory mapped file, we need to first ftruncate() the file to the right size and then put the data; otherwise a seek() error will occur. On some operating systems, ftruncate() does not create a file of actual size when it is called. We may see a large file, but du only reports a very small size.

Accelerate malloc() under Linux

We should set the following environmental variables if we want to accelerate malloc() calls: If the program in use frequently malloc()/free() a chunk of memory larger than 128k, this tweak may bring much better performance. The trade-off is some memory will be wasted.

See also this page.

Perl Programming

Profiling Perl

Profiling a Perl program can be achieved by providing an alternative Perl debugger. The most popular one is DProf, the Perl profiler. It is implemented in the Devel::DProf module. I am not sure whether this module has become part of the standard, but at least on my laptop, it is installed by default.

With DProf, a perl program can be profiled by: File tmon.out will be dumped which records all the statistics. You can then run dprofpp to view the results.


Sorting huge files with GNU sort

GNU sort, which is one of GNU Core Utilities, can sort huge files as long as there is enough disk space. It works by spliting the file into several trunks, sorting each trunk separately and then merging the sorted trunk together. Sorted trunks are dumped to disk as temporary files. Each temporary file is actually a part of the original file. And therefore the required disk space is always smaller than the original file.

In sorting huge files, the most useful options for GNU sort are "-S size" and "-T tmp_dir". The first option specify the largest memory sort should use. Sort is more efficient with larger memory. The second option indicates the temporary directory used by sort. It is helpful if there is not enough disk space in the "/tmp" partition.

An example: