Short solutions of 2015

Back to Blog

In this section I will collect short things that have taken me some time to figure out. The intent is that I would be able to avoid wasting time in the future after I have forgotten about them.

Clone a sub-directory from a Mercurial repository

18.06.2015 (03.06.2016)

Problem

Suppose you have stored all of your papers inside the same Mercurial repository allpapers as follows:

allpapers/
    paper-a/
        paper-a.tex
        ...
    paper-b/
        paper-b.tex
        ...
    ...

You want to move the paper-a sub-directory to its own, new, repository just-paper-a, while preserving its revision history:

just-paper-a/
    Want the contents of allpapers/paper-a here, with all of its revision history!

Solution

This solution is from here.

The task is handled by the Convert extension of Mercurial. The caveat is that this is a one-way operation: it is not possible to commit changes from the new repository into the old repository. However, at least in my case this wasn’t a problem since I just wanted to remove paper-a from the old repository afterwards.

Enable Convert extension

The Convert extension is enabled by editing the Mercurial settings to

[extension]
(existing stuff...)
hgext.convert=

On TortoiseHg, you would do

Create a mapping file

Suppose you want to create the new repository into the directory just-paper-a. Into the parent directory of just-paper-a, create a file map.txt, which reads

include "paper-a"
rename "paper-a" .

The first line instructs the Convert extension to move the paper-a sub-directory into the new repository at just-paper-a/paper-a. However, now there would be an extra sub-directory paper-a inside the new repository.

Therefore, the second line instructs the convert extension to rename the new paper-a sub-directory to ., which means that the contents of the paper-a sub-directory inside the old repository are moved into the contents of the just-paper-a directory inside the new repository.

Perform the conversion

Go to the parent directory of just-paper-a using command-line. Then perform the conversion by entering

hg convert --filemap map.txt allpapers just-paper-a

After updating to the tip-revision, you are done.

Matlab gfortran library on Mac OS X

14.06.2015

Problem

In Matlab, on Mac OS X, you have built a mex which by some route links to the gfortran library — perhaps because you use Lapack and/or BLAS. When you call the mex, Matlab gives an error which goes something like this:

Invalid MEX-file

'your_mex_file.mexmaci64':
dlopen(your_mex_file.mexmaci64, 6):

Symbol not found:
__gfortran_transfer_array_write

Referenced from:
(some library)

Expected in:
/Applications/MATLAB_R2015a.app/sys/os/maci64/libgfortran.3.dylib

in (some library)

Cause

Matlab has its own version of gfortran library in

/Applications/MATLAB_R2015a.app/sys/os/maci64/libgfortran.3.dylib

The dynamic linker in the operating system resolves the gfortran reference to this file. However, the mex library is linked against the version of gfortran library in

/usr/local/lib/gcc/5/libgfortran.3.dylib

(or something similar). Since these libraries are not compatible, there is an error.

Solution 1: Replace the libraries

This solution is from here.

Replace

/Applications/MATLAB_R2015a.app/sys/os/maci64/libgfortran.3.dylib

with gcc’s library of the same name. On my computer, the latter is located in:

/usr/local/lib/gcc/5/libgfortran.3.dylib

Solution 2: Modify the library search path

This solution is from here.

Running

getenv('DYLD_LIBRARY_PATH')

on the Matlab command prompt gives the following:

/Applications/MATLAB_R2015a.app/sys/os/maci64:
/Applications/MATLAB_R2015a.app/bin/maci64/../../Contents/MacOS:
/Applications/MATLAB_R2015a.app/bin/maci64:
/Applications/MATLAB_R2015a.app/extern/lib/maci64:
/Applications/MATLAB_R2015a.app/sys/java/jre/maci64/jre/lib/./native_threads:
/Applications/MATLAB_R2015a.app/sys/java/jre/maci64/jre/lib/./server:
/Applications/MATLAB_R2015a.app/sys/java/jre/maci64/jre/lib/./lib/jli

This is a list of directories which Matlab gives to the operating system to search for dynamic libraries. Since the preceding paths have priority over the following ones, the first idea is to prepend gcc’s path to this list as

setenv('DYLD_LIBRARY_PATH', ['/usr/local/lib/gcc/5:', getenv('DYLD_LIBRARY_PATH')]);

on the Matlab command prompt. However, this change comes too late; the library has already been loaded. To get to an earlier point in Matlab initialization, we instead modify the file

/Applications/MATLAB_R2015a.app/bin/.matlab7rc.sh

The documentation in this file provides the following information:

LDPATH_PREFIX (path(s) that appear at the start of LD_LIBRARY_PATH)

Guided by this, we find LDPATH_PREFIX= — under section mac|maci|maci64 — and replace it with

LDPATH_PREFIX="/usr/local/lib/gcc/5"

After a restart of Matlab, writing

getenv('DYLD_LIBRARY_PATH')

provides

/usr/local/lib/gcc/5:
(and the rest...)

as expected (LDPATH_PREFIX is empty). The library will now be correctly resolved.

CMake looping with foreach

14.06.2015

Problem

In CMake, you want to iterate over a, b and c, and write:

foreach(element "a;b;c")
endforeach()

Hoever, this does not work.

Solution

This works

set (elementSet "a;b;c") 
foreach(element elementSet)
endforeach()

This is from here.

CMake passing variables

14.06.2015

Problem

Consider the following in CMake:

macro (Test x)
endmacro()

set (x "")
Test(${x})

Then CMake errors on the Test call having too few arguments.

Cause

The call expands to

Test()

which causes the error.

Solution

The solution is to use quotation marks:

Test("${x}")

Then the call expands to

Test("")

Windows 8.1 startup after Yosemite OS X update

09.06.2015

Problem

After the Yosemite OS X update on my Mac Mini 2012, Windows 8.1 (via Bootcamp) started to sometimes freeze at the loading screen; the Windows logo appeared, but the dots did not circle around.

Solution

09.06.2015

The solution was to unplug-plug USB devices one by one. Windows 8.1 immediately continued loading on the offending USB device. The offending device was either my HP Laserjet P2055d printer (USB 2.0), or my USB hub (USB 2.0) — or both. Perhaps USB 2.0 devices are the problem here.

14.06.2015

This time I slowly unplugged-plugged the USB devices to see which it is. Unfortunately, nothing worked. I tried different combinations, printer on and off, USB hub on and off, and different positions for the USB plugs. I also tried to unplug-plug monitor cables. I ended up hard-rebooting the computer.

23.06.2015

The offending USB device was the USB 2.0 D-link hub. Plugging it off resumed Windows starting. It is possible that the same bug alternates between the devices based on the starting order.

Matlab debug builds

04.06.2015

Problem

You have built a C++ library in CMake’s debug mode, and try to link it together with other C++ code in Matlab using the -g debug flag. The linker raises the warning

warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library

Cause

Under a Windows debug build, CMake defaults to dynamically-linked debug-mode C and C++ standard libraries (/MDd on Visual Studio), while Matlab (under -g) defaults to dynamically-linked release-mode standard libraries (/MD on Visual Studio). This mixture of standard library versions causes the warning and the errors.

Solution

The solution is to match the versions of the standard libraries.

Option 1: Change CMake’s default

To match Matlab, force CMake to use release-mode standard libraries even in debug-mode:

string(REPLACE "/MDd" "/MD" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
string(REPLACE "/D_DEBUG" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})

This is best made as an option for the build, so that it is only done when the library is to be built from Matlab’s mex.

Option 2: Change Matlab’s default

To match CMake, force Matlab to use a debug-mode standard libraries in debug mode. Edit the compiler option file:

However, I am not sure whether this is correct, since Matlab’s components have been built with the release-mode standard libraries; Matlab does not come with debug libraries.

Windows 8.1 mounting problems

03.06.2015

Problem

When trying to mount an ISO-image, Windows 8.1 throws the error

Sorry, there was a problem mounting the file.

Solution

Copy the ISO-image anywhere. After that Windows mounts it correctly. This is from here.

Command-line color

22.05.2015

Problem

You would like to reset the color of Windows command-line to white.

Solution

Type in the command-line:

color 07

This is from here.

Building a single file under CMake

22.05.2015

Problem

Given Makefiles produced by CMake, you wish to build only a single file, to quickly test whether your changes are correct. However, if you run make from the root, then it will build every file affected by the change. This takes time.

Solution

CMake produces one Makefile for each directory. Change the directory to where the file resides, and then run make file.obj. Note that while the source is .cpp, the target is .obj.

C++ abs may not be in std namespace

22.05.2015

The standard abs function may not be contained in the std namespace. This compiles in Visual Studio 2015 RC and gcc 4.9.3:

#include <cmath>

int main()
{
    return abs(0);
}

Latex overfull boxes due to hyphenation

24.04.2015

Problem

In Latex, you get a lot of warnings of the form

Overfull \hbox (17.52579pt too wide) in paragraph

When you look at the corresponding text in the PDF, the text leaks over the marginals.

Cause

The leakage probably happens because the text contains compound words with hyphens, such as quasi-norm. Since Latex does not by default hyphenate explicit hyphens, the compound word becomes a big element which Latex cannot break into pieces and has problems positioning.

Solution

To solve, add this package:

% For hyphenating dashed words (e.g. sub-composability)
\usepackage[shortcuts]{extdash}

Then, in the text, replace - with \-/. For example, quasi\-/norm.

Latex PDF bookmarks

24.04.2015

Problem

Suppose you use the hyperref package to generate internal links, and write the following section title:

\section{Convergence in the $m$:th mean}

Then you will get the following warning:

Token not allowed in a PDF string (PDFDocEncoding):(hyperref) removing `math shift'

Cause

The section name is also used to name a PDF bookmark, for which math symbols are not allowed.

Solution

Use the \texorpdfstring macro to provide alternative texts for math-symbols. For example:

\section{Convergence in the \texorpdfstring{$m$}{m}:th mean}

The first argument is used for the section name, and the second argument is used for the PDF bookmark.

Latex document type

24.04.2015

Problem

Suppose you use the environ package, and change the type of Latex document from article to book. This results in obscure errors for the \NewEnviron macro, which do not go away even after reverting the change.

Solution

Installing Kubuntu in VirtualBox

11.03.2015

The installation of Kubuntu fails right on the start with a garbled screen. To resolve, push Right Ctrl + F1, followed by Right Ctrl + F7. This is from here.

Clang in CMake

11.03.2015

To make CMake use Clang under Linux, set

export CC=/usr/bin/clang
export CXX=/usr/bin/clang++

Only the second is needed for C++. This is from here.

Boost compiler

04.03.2015

When using preview-versions of Visual Studio, Boost library emits an error where it says that it does not recognize the used compiler. To get around this, open

boost\config\compiler\visualc.hpp

Navigate to the bottom, where it says

#if (_MSC_VER > 1800 && _MSC_FULL_VER > 190022129)

or something similar. Replace 190022129 with a proper version number, which you can obtain by running:

#include <iostream>
#include <string>
int main()
{
    std::cout << _MSC_FULL_VER << std::endl;
    std::string tmp;
    std::getline(std::cin, tmp);
    return 0;
}

Ricoh Aficio MP C4502 problems

06.02.2015

The printer-scanner Ricoh Aficio MP C4502, that we have at the university, produced every scanned page in landscape, no matter how I set the settings. This was a bug in the printer firmware, which appeared only when the scanning was done in grayscale. The solution was to set the scanner to automatically set the output-type, or to scan in color.

The same printer also had problems printing the document it had just scanned. Printing 11 pages of scanned documents, using a PS-driver, took 12 minutes 30 seconds. I tried printing the same document at home with my HP Laserjet P2055d, using a PCL5 driver, and it did the job in 1min 50 seconds. I wonder whether the problem is in using a PS-driver.

HP Laserjet P2055d problems in Windows 8.1

31.07.2015

Once I upgraded to Windows 10, the printer worked out of the box.

06.02.2015

After I upgraded from Windows 7 to Windows 8.1, my HP Laserjet P2055d printer stopped working. I was able to fix it.

Observations

Solution

    PCL XL Error
        Subsystem: KERNEL
        Error: IllegalTag
        Operator: 0x
        Position: 26967