distribute shared library with your binary (set rpath)

When a Linux binary is started, the run-time linker uses three sources for finding the needed libraries. First it is possible to set the LD_LIBRARY_PATH environment variable, second the “rpath” (this is a path encoded into the binary while compile time) and last but not least the systems’ default directory like /usr/lib. Now consider the situation where you have to run a binary on a machine where you don’t have root privileges: Here you can’t just install / copy the library to the standard folder, therefore you have to go for one of the first two solutions. Setting the environment variable is seen as bad practice.

Although using rpath is also seen as evil / bad practice, there are situations where you just have no choice. Therefore just copy the shared library into the same folder as your binary (or whatever folder you also distribute) and compile the binary with the following (additional) linker flags:

compile C and C++ code into one binary in Linux

Sometimes you have a project written in C and you want to add some code (may be open source software) which is provided in C++. So you may want to call a function written in C++ from C. For that purpose, you have to change the function prototype of the concerned C++ function like:

The ifdefs are only needed, if you are including the header file containing this code into a C and C++ source file!

When it comes to compiling, use the specific compiler like gcc for C code and g++ for C++ code. Compile each sourcefile(s) into a object file like:

The last line will link the object files together.
A good tutorial is found here.

Compile MATLAB functions in C and link them with Intel MKL CBLAS

The MATLAB utility “mex” (enter “help mex” in command window in MATLAB) is a wrapper for your actual compiler, like gcc. It does not support intels icc. But this is no reason not to use the Intel MKL libraries! Write your code and compile it with:

Looks ugly, but is just a matter of copy pasting from the following website. Then gcc will compile your code and link the Intel libraries! The linking process is somewhat tricky, therefore use

http://software.intel.com/en-us/articles/intel-mkl-link-line-advisor/

to determine everything after the file name in the compiling command. Remember to select static linking on the website.