C++ Standard Library in MacOS 10.9.4

Today, I’ve got some errors compiling and older C++ code, which surprised me a lot. The error message was

Undefined symbols for architecture x86_64:
     "std::string::compare(char const*) const", referenced from: ...

It seems that something went wrong with the standard library… so I’ve done some investigations in the internet and the culprit was the newer version of my Mac OS. In Mac OS 10.9.4. Apple changes the name of the standard library from “stdlib” to “libstdc++”. You can fix some kind of errors by adding a CXX-Linker flag, i.e.

CXXFLAGS = -stdlib=libstdc++

This should work for all makefiles.

Command Line Tools in Mac OS 10.9.1

A few days ago I’ve updated my MacOS to version Mavericks 10.9.1. While coding as usual today, I’ve tried to compile an older code with a Makefile and obtain the following error message:

fatal error: stdio.h: No such file or directory

I thought WTF… this is a standard library… Why doesn’t find my compiler the header? In fact, the command line tool of the old Xcode 4 doesn’t work in Mac OS 10.9. such that you have to updated them as well as Xcode (to Version 5.0.2). The solution of this problem is simple: just execute the following command

$ xcode-select -p

and install the new command line tool for Mac OS 10.9.1! Now the compiler should find all standard libraries.

Set Environment Variables Permanently in MacOS

Today, I’ve asked myself how to set environment variables permanently in Mac OS 10.8.5, such that I can omit include flags (e.g. -I/usr/local/include). One solution is to create or edit a file called “launched.conf”. Every time you start your system it will be executed. So open or create this file via

 sudo vi /etc/launchd.conf

and add the following two lines

setenv CPLUS_INCLUDE_PATH /usr/local/include
setenv C_INCLUDE_PATH /usr/local/include

and restart your system. You can verify your action by typing

env | grep INCLUDE_PATH

The variables should be set.

HowTo: Install LAPACK and BLAS on Mac OS

Today, I’ve installed two popular packages:

Both packages are required when you deal with linear algebra operations, e.g. solving linear equation systems. Originally, both packages were written in FORTRAN, but I want to use them coding in C++. The solution is to create a libraray! There’re a few steps to consider while installing the packages. First, you have to install BLAS, because LAPACK requires it. If you have downloaded both packages, unzip them. Switch to the BLAS folder and execute

$ make

to compile all fortran files. After that, execute

$ mv blas_UNIX.a libblas.a

to rename the created library. Now, you created a library called “libblas.a”. You just copy that file to your library folder. Therefore execute the following command

sudo cp libblas.a /usr/local/lib/

Et voila. You’ve installed the BLAS package. Now switch to the LAPACK folder and adjust the file “make.inc”. If you set all parameter correctly, execute the command

$ make

Now, you created a library e.g. called “lapack_MACOS.a”. Copy that file to your library folder by executing

sudo cp liblapack.a /usr/local/lib/

Congratulation, you’ve installed BLAS and LAPACK on Mac OS!

Note: I’ve installed BLAS and LAPACK operating on Mac OS 10.8.x!

Dynamic Figure Headings in Matlab

In this post, I will explain to customize a figure title in a Matlab plot. Maybe the title of a figure shall depend on some variables. But how do we do that? The answer is acutally simple. Consider a (integer/double/float) variable var. Define the title as follows

title( { ['Some title depending on',num2str(var),' and ...'] } )

Without the “[” and “]” brackets, Matlab interprets the string with line breaks after “on” and after the value of var.

Numerical methods for Ordinary Differential Equations – Part 1

Dealing with ordinary differential equations (ODEs) is a great possibility understanding many natural processes. But in many non-academic cases, an analytical solution isn’t known. In this case numerical methods are of great importance. Consider a general ODE with an initial value y0 in IRn and a finite time horizon I = [a,b]:

y'(t) = f(t,y) with y(0) = y0

It’s well known to claim Lipschitz continuity for the function f in the 2nd argument to gain the existence and uniqueness of a solution. We notice, that the time horizon is a compact intervall but has an uncountable cardinality. In the first step, we discretize the intervall I into (not necessary) equidistant subintervalls [t0, t1], (tk, tk+1] for k = 1..n-1. This leads to

a = t0 < t1 < t2 < ... < tn = b.

We notice yk:= y(tk) and h := (x1 – x0). The simplest numerical methods is known as explicit Euler method. It holds

y_{k+1} = y_k + h * f( t_k , y_k ) for k = 1..n-1

Next time, I’ll present some convergence and stability analysis for the explicit Euler method and the big disadvantage of this method. In the next few weeks, I’ll treat numerical methods for stiff ODEs, because they’re object of my investigation within my master thesis.

Syntax Highlighting for *.tcc Files in TextMate

Programming in TextMate without syntax highlighting sucks. I prefer and recommend (!) to divide the headers from the implementation but when coding some template classes, it isn’t useful to divide the code into an *.hpp headerfile and an *.cpp source file. Due to this fact, I divide the code into

  • *.hpp – header file with declarations
  • *.tcc – templated file with implementation

But a problem arose, when I opened that type of file with TextMate: There’s no syntax highlighting!

Here is a solution for this problem:

  1. Click on Bundles -> Bundle Editior -> Edit Languages
  2. Select the language ‘C’. There you have to select ‘C++’ and edit the 5th line where
    fileTypes = ( 'cc', 'cpp', ... )

    is defined.

  3. Add ‘tcc’ into this array. Therefore
    fileTypes = ( 'tcc', 'cc', 'cpp', ... )

Next time, when you open a *.tcc file, syntax highlighting is on.