Setting up an ArrayFire Project on OSX

Brian Kloppenborg ArrayFire Leave a Comment

Continuing our blog series "Learning ArrayFire from scratch" today we will discuss how to set up an ArrayFire project on OSX using XCode, CMake, or Makefiles.

Once you have installed ArrayFire on your system, the next thing to do is set up your build system. On OSX, you may create ArrayFire project using almost any editor, compiler, or build system. The only requirement is that you can include the ArrayFire header directory, and link with the ArrayFire library you intend to use.

The big picture

By default, the ArrayFire OSX installer will place several files in your computer's /usr/local  directory. The installer will populate this directory with files in the following sub-directories:

Because ArrayFire follows standard installation practices, you can use basically any build system to create and compile projects that use ArrayFire. Among the many possible build systems on Linux we suggest using ArrayFire with either CMake or Makefiles with CMake being our preferred build system.


Although we recommend using CMake to build ArrayFire projects on OSX (because you can save on re-compiling some files for multiple-backend deployments), you can use XCode if this is your preferred development platform. To save some time, we have created an sample XCode project in our ArrayFire Project Templates repository which is configured to build code for the CPU, OpenCL, CUDA, and Unified backends.

To set up a basic C/C++ project in XCode do the following:

  1. Start up XCode. Choose OSX -> Application, Command Line Tool for the project:
  2. Fill in the details for your project and choose either C or C++ for the project:
  3. Next we need to configure the build settings. In the left-hand pane, click on the project. In the center pane, click on "Build Settings" followed by the "All" button:
  4. Now search for "Header Search Paths" and add /usr/local/include  to the list:
  5. Then search for "Library Search Paths" and add /usr/local/lib  to the list:
  6. Next, we need to make sure the executable is linked with an ArrayFire library: To do this, click the "Build Phases" tab and expand the "Link with Binary Library" menu:
  7. In the search dialog that pops up, choose the "Add Other" button from the lower right. Specify the /usr/local/lib  folder:
  8. Next, select the ArrayFire library with which you wish to link your program. Your options will be:
In the picture below, we have elected to link with the OpenCL backend:


9. Lastly, lets test ArrayFire's functionality. In the left hand pane open the main.cpp` file and insert the following code:


Finally, click the build button and you should see some information about your graphics card in the lower-section of your screen:



We recommend that the CMake build system be used to create ArrayFire projects. If you are writing a new ArrayFire project in C/C++ from scratch, we suggest you grab a copy of our CMake Project Example; however, it is useful to read the documentation below in case you need to add ArrayFire to an existing project.

As discussed above, ArrayFire ships with a series of CMake scripts to make finding and using our library easy. The scripts will automatically find all versions of the ArrayFire library and pick the most powerful of the installed backends (typically CUDA).

First create a file called CMakeLists.txt  in your project directory:

and populate it with the following code:

where my_executable  is the name of the executable you wish to create. See the CMake documentation for more information on how to use CMake. Clearly the above code snippet precludes the use of both CUDA and OpenCL, see theArrayFire CMake Example for an example of how to build executables for both backends from the same CMake script.

In the above code listing, the FIND_PACKAGE  will find the ArrayFire include files, libraries, and define several variables including:

If you wish to use a specific backend, the find script also defines these variables:

Therefore, if you wish to target a specific specific backend, simply replace ${ArrayFire_LIBRARIES}  with ${ArrayFire_CPU} , ${ArrayFire_OPENCL} , ${ArrayFire_CUDA} , or ${ArrayFire_Unified} in the TARGET_LINK_LIBRARIES  command above. If you intend on building your software to link with all of these backends, please see the CMake Project Example which makes use of some fairly fun CMake tricks to avoid re-compiling code whenever possible.

Next we need to instruct CMake to create build instructions and then compile. We suggest using CMake's out-of-source build functionality to keep your build and source files cleanly separated. To do this:

NOTE: If you have installed ArrayFire to a non-standard location, CMake can still help you out. When you execute CMake specify the path to the ArrayFireConfig*  files that are found in the share/ArrayFire/cmake  subdirectory of the installation folder. For example, if ArrayFire were installed locally to /opt/ArrayFire  then you would modify the cmake command above to contain the following definition:

You can also specify this information in the ccmake command-line interface.


Building ArrayFire projects with Makefiles is fairly similar to CMake except you must specify all paths and libraries manually. As with any make project, you need to specify the include path to the directory containing arrayfire.h file. This should be -I /usr/local/include  if you followed our installation instructions. Similarly, you will need to specify the path to the ArrayFire library using the -L  option (e.g. -L/usr/local/lib ) followed by the specific ArrayFire library you wish to use using the -l option (for example -lafcpu , -lafopencl , -lafcuda , or -laf  for the CPU, OpenCL, CUDA, and unified backends respectively.

Here is a minimial example MakeFile which uses ArrayFire's CPU backend: