Category: Programming

Multiple Streams Write to the Same File with GNU C Library

Types of Channels

(1) You can have multiple file descriptors and streams (let’s call both streams and descriptors “channels” for short) connected to the same file. [1]

(2) There are two cases to consider: linked channels that share a single file position value, and independent channels that have their own file positions. [1]

(3) It’s impossible for two channels to have separate file pointers for a file that doesn’t support random access. Thus, channels for reading or writing such files are always linked, never independent. [2]

(4) Append-type channels are also always linked. For these channels, follow the rules for linked channels. [2]

Independent Channels

(1) You should clean an output stream after use, before doing anything else that might read or write from the same part of the file. [2]

This statement implies that it is OK for multiple independent channels each write to different parts of the same file at the same time as long as they clean themselves after use.

(2) You should clean an input stream before reading data that may have been modified using an independent channel. Otherwise, you might read obsolete data that had been in the stream’s buffer. [2]





Resolving xinclude for saxon

Saxon seems to have trouble with resolve relative xinclude paths, even with the -xi option turned on.
We can use xmllint resolve the paths first, and then feed the file to saxon and taking advantage of saxon’s extension.
xmllint –xinclude input.xml > resolved.xml
saxon -s:resolved -xsl:style.xsl.

BASH: pipe to functions


function ProcessOneEntry() {

while read entry # Use the “read” function to do the trick
# Code here to do the job


# Application 1:
cat AnListFile | ProcessOneEntry

# Or
# Application 2:

find InputDirectory -name “Pattern” | ProcessOneEntry

Build Matlab mex files with CMake

To use CMake to manage the build and install process of your mex files, you need to change the compilation rules of CMake. CMake does not support “mex” compiler naturally, so you need to insert the following commands in your CMakeLists.txt.


# Suffix and Prefix of the output target file
SET( CMAKE_SHARED_LIBRARY_SUFFIX .mexa64 )	# set suffix to .mexa64
SET( CMAKE_SHARED_LIBRARY_PREFIX )		# remove the "lib" prefix

# Variables controlling the build-phrase
SET( CMAKE_CXX_FLAGS "-cxx -largeArrayDims CXXFLAGS='$$CXXFLAGS -std=c++11'" )
SET( CMAKE_SHARED_LIBRARY_CXX_FLAGS )		# remove the -fPIC option. mex does not accept the "-fPIC" option

	"<CMAKE_CXX_COMPILER> <DEFINES> <FLAGS> -outdir <OBJECT_DIR> -c <SOURCE>; mv <OBJECT_DIR>/$$(basename <SOURCE> .cxx ).o <OBJECT>"

# Variables controlling the linking-phase
SET( CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS )	# remove -shared options. mex does not accept the "-shared" option


# Variables controlling the installation RPATH
# CMake will reset RPATH at the installation phase, so we need to specify CMAKE_INSTALL_RPATH

MESSAGE( STATUS "mex.cmake is loaded.\n" )

## To add an mex file, you can:
#  ADD_LIBRARY( myprogram SHARED myprogram.cxx )
## Make sure you specify it as SHARED, and make sure you use the cxx extension.  

Setting RPATH for Matlab mex compilation

Platform: Linux
A mex file seems to be compiled successfully, but at run-time Matlab issues error because dependent libraries cannot be found.

At run-time, Matlab failed locate the correct dependent library for the mex file.

(1) Set environmental variable LD_LIBRARY_PATH in Matlab, OR
(2) Set RPATH in the mex file during the linking procedure.

Use the setenv and getenv command in Matlab.

You can insert the following option in the command line invokation of mex:
  LDFLAGS=’$LDFLAGS -Wl,-rpath=Your_RPATH’
For example:
  mex LDFLAGS=’$LDFLAGS -Wl,-rpath=Your_RPATH’ mycode.cxx
  LDFLAGS=’$LDFLAGS …’ appends “…” to LDFLAGS after mex has setup its compilation variables, so Matlab’s setup is automatically get involved.
  -Wl,aaa,bbb (comma separated, not blanks) will be passed to ld as options aaa bbb (blank separated).

If you want to permanently add this to your mex compilation, you can modify the script:
  A. Locate the script, and locate the lines setting “RPATH”.
  B. Insert “-rpath-line,Your_RPATH,-rpath=Your_RPATH” to the value of RPATH. For example,
The original value might be: RPATH=”-Wl,-rpath-link,$TMW_ROOT/bin/$Arch”.
You can replace with:

If you want to use relative paths, then use ‘\\\$ORIGIN’ to stand for the path of the mex file. For example:
in the command line
  mex LDFLAGS=’$LDFLAGS -Wl,-rpath=\\\$ORIGIN’ mycode.cxx
Or in the
In the compiled mex, you will find “$ORIGIN” is listed in the rpath of the mex file.
Comment: “\” is the escape character. After twice variable expansion, \\\$ORIGIN will become ‘$ORIGIN’

Inspect the RPATH of the mex file with “chrpath” command, with the “-l” option. For example,
chrpath -l myfunc.mex