Convince cmake to fix the linker path for mosek

Alec Jacobson

April 24, 2020

weblog/

Often I'll install mosek into a common place like /usr/local/mosek/. Mosek annoyingly uses dynamic libraries, and I'll often get errors like this:

dyld: Library not loaded: libmosek64.8.0.dylib
  Referenced from: /Users/ajx/Dropbox/adobe/osqp-bbw/debug/./osqpbbw
  Reason: image not found
Abort trap: 6

The hacky way to fix this is to monkey with DYLD_LIBRARY_PATH and DYLD_FALLBACK_LIBRARY_PATH.

A better way is to use install_name_tool to change the raw library name to a real path. This means issuing an obscure command after linking my executable.

install_name_tool -change libmosek64.dylib /usr/local/mosek/8/tools/platform/osx64x86/bin/libmosek64.dylib

Except it's not libmosek64.dylib it's libmosek64.some.shit.dylib. Somehow the linker determines this, but cmake doesn't know about the some.shit. so it's hard to make a post-build command to run this command automatically. This is the closest I came:

add_custom_command(TARGET ${PROJECT_NAME}
  POST_BUILD COMMAND
  ${CMAKE_INSTALL_NAME_TOOL} -change 
  `otool -L ${PROJECT_NAME} | sed -n -e \""s/.*\\(libmosek.*dylib\\).*/\\1/p"\"`
  ${MOSEK_LIBRARIES} ${PROJECT_NAME})

The otool command will extract the libmosek64.some.shit.dylib from the binary called ${PROJECT_NAME} and then set up an install_name_tool command to fix it (holy escapes! there must be a cleaner way!).