Cmake For Mac


Last week, I shared how to create a CMake project for Windows. Since CMake is a cross-platform software building tool, I am going to make my C/C++ project support Linux and macOS.


  • CMake for macOS:
  • CMake for Linux:

In contrast to the C/C++ sample code for Windows, the code for Linux and macOS is a little bit different. My goal is to merge them into one .cxx file.

Cmake Gui For Mac

How to Make C/C++ Code Compatible with Multiple Platforms

I used the predefined macros to detect the operating system and customize relevant C/C++ code. Insert the following definitions to

  • A CMake toolchain file for iOS, watchOS and tvOS development with full simulator support and toggable options! Now exposes Autoconf compatible triples, accessible via the APPLETARGETTRIPLE variable.; ios-cmake.
  • For example, the Linux-x8664 tar file is all under the directory cmake–Linux-x8664. This prefix can be removed as long as the share, bin, man and doc directories are moved relative to each other. To build the source distributions, unpack them with zip or tar and follow the instructions in.

Use macros to control code logic in BarcodeReader.cxx.

How to Configure CMake Project

Make is a tool which controls the generation of executables and other non-source files. Create a temporary directory, which we denote as buildopencv, where you want to put the.


In my previous article, I didn’t mention how to select a generator. By default, CMake generates an x86 project in which I couldn’t link DynamsoftBarcodeReaderx64.dll. Let’s list the available generators on Windows:

According to the help information, we can specify a generator with Win64 architecture:

Link the x64 library in CMakeLists.txt:

Linux & macOS

Set the library search path:

Link shared libraries:

Set install destination:

Build and install the executable file and library:

Note: the default destinations are /usr/local/bin and /usr/local/lib. When you run the executable BarcodeReader, you will see the error message: cannot open shared object file: No such file or directory.

Cmake Gui For Mac

Can we fix the issue by changing the value of CMAKE_INSTALL_PREFIX to ‘/usr/lib’?

It is okay for Linux, but not permitted on the latest version of macOS.

A usual solution is to export LD_LIBRARY_PATH:

CMake provides a better way that changes RPATH.

Build and install the project again. We can now run barcode reader app with no errors.

Source Code

When I compile OpenCV for Mac OSX 10.8 using XCode 4.5.2 and cmake 2.8.9 my installed libraries aren't configured with the correct corresponding installation path.

cmake -G 'Unix Makefiles' -D CMAKE_BUILD_TYPE=RELEASE -D WITH_QT=ON -D WITH_OPENGL=ON -DCMAKE_INSTALL_PREFIX=/opt/local ../opencv.ram/

The CMAKE_INSTALL_PREFIX=/opt/local puts the libraries in the correct place after a 'make install' but the embedded path names are configured with a relative path name such as 'lib/libopencv' and not '/opt/local/lib/libopencv' as desired.

otool -D /opt/local/lib/libopencv_core.2.4.3.dylib/opt/local/lib/libopencv_core.2.4.3.dylib:lib/libopencv_core.2.4.dylib

The libraries in the build location after the make command but before the 'make install' are configured with the correct absolute path name corresponding to the build tree location:

otool -D /Volumes/ramdisk/osx_ffmpeg/lib/libopencv_core.2.4.3.dylib/Volumes/ramdisk/osx_ffmpeg/lib/libopencv_core.2.4.3.dylib:/Volumes/ramdisk/osx_ffmpeg/lib/libopencv_core.2.4.dylib

Mac Cmake Install

The relative path seems to be set during installation in the generated cmake_install.cmake file via the install_name_tool command as shown here.

Cmake Install Command

grep -C1 install_name /Volumes/ramdisk/osx_ffmpeg/modules/core/cmake_install.cmake NOT IS_SYMLINK '${file}') EXECUTE_PROCESS(COMMAND '/usr/bin/install_name_tool' -id 'lib/libopencv_core.2.4.dylib'

Basically I'd like to configure the installed libraries with correct embedded absolute path names, so that I can link to them directly without using DYLD_LIBRARY_PATH or the like. Presumably this is the desired default build behavior? Perhaps my build syntax is missing something.

How To Use Cmake

I have found a number of links to folks who have remedied this with post installation scripts using the install_name_tool command to modify the path names, such as shown in the link below, but I'm guessing there is a way to avoid this.

install_name_tool -id pwd/opencv/lib/libopencv_core.2.1.dylib opencv/lib/libopencv_core.2.1.dylib

If I omit the CMAKE_INSTALL_PREFIX=/opt/local cmake option the libraries are installed in the default /usr/local/lib location but still have a relative embedded path name.


UPDATE: I haven't found a cmake fix for this but did create a simpler post installation bash script that should fix the install path for opencv libraries and their dependencies:

editretagflag offensiveclosemergedelete

Cmake For Mac Download


I'm running into the same issue with Qt5 and macdeployqt, which complains about wrong paths (the relative ones mentioned above) to the opencv .dylib. Any update on this? None of the proposed solution here solved the problem. The relative paths are:lib/libopencv_core.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)lib/libopencv_highgui.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)lib/libopencv_imgproc.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)

A simple solution is cmake -D BUILD_SHARED_LIBS=OFF. Static libraries do not use relative path.