1 Using ArrayFire on Linux {#using_on_linux}
4 Once you have [installed](\ref installing) ArrayFire on your system, the next
5 thing to do is set up your build system. On Linux, you can create ArrayFire
6 projects using almost any editor, compiler, or build system. The only
7 requirements are that you include the ArrayFire header directories and link with
8 the ArrayFire library you intend to use i.e. CUDA, OpenCL, CPU, or Unified
11 ## <a name="big-picture"/> The big picture
13 On Linux, we recommend installing ArrayFire to `/opt/arrayfire` directory. The
14 installer will populate files in the following sub-directories:
16 include/arrayfire.h - Primary ArrayFire include file
17 include/af/*.h - Additional include files
18 lib/libaf* - CPU, CUDA, and OpenCL libraries (.a, .so)
19 lib/libforge* - Visualization library
20 lib/libcu* - CUDA backend dependencies
21 lib/libOpenCL.so - OpenCL ICD Loader library
22 lib/libglbinding* - OpenGL graphics dependencies
23 share/ArrayFire/cmake/* - CMake config (find) scripts
24 share/ArrayFire/examples/* - All ArrayFire examples
26 Because ArrayFire follows standard installation practices, you can use basically
27 any build system to create and compile projects that use ArrayFire. Among the
28 many possible build systems on Linux we suggest using ArrayFire with either
29 CMake or Makefiles with CMake being our preferred build system.
31 ## Prerequisite software
33 To build ArrayFire projects you will need a compiler
35 #### Fedora, Centos and Redhat
37 Install EPEL repo (not required for Fedora)
40 yum install epel-release
44 Install build dependencies
47 yum install gcc gcc-c++ cmake3 make
50 #### Debian and its derivatives
52 Install common dependencies
55 apt install build-essential cmake cmake-curses-gui
60 We recommend that the CMake build system be used to create ArrayFire projects.
61 As [discussed above](#big-picture), ArrayFire ships with a series of CMake
62 scripts to make finding and using our library easy.
64 First create a file called `CMakeLists.txt` in your project directory:
66 cd your-project-directory
69 and populate it with the following code:
71 find_package(ArrayFire)
72 add_executable(<my_executable> [list your source files here])
74 # To use Unified backend, do the following.
75 # Unified backend lets you choose the backend at runtime
76 target_link_libraries(<my_executable> ArrayFire::af)
78 where `my_executable` is the name of the executable you wish to create. See the
79 [CMake documentation](https://cmake.org/documentation/) for more information on
80 how to use CMake. To link with a specific backend directly, replace the
81 `ArrayFire::af` with the following for their respective backends.
83 * `ArrayFire::afcpu` for CPU backend.
84 * `ArrayFire::afcuda` for CUDA backend.
85 * `ArrayFire::afopencl` for OpenCL backend.
87 Next we need to instruct CMake to create build instructions and then compile. We
88 suggest using CMake's out-of-source build functionality to keep your build and
89 source files cleanly separated. To do this open the CMake GUI.
91 cd your-project-directory
97 *NOTE:* If you have installed ArrayFire to a non-standard location, CMake can
98 still help you out. When you execute CMake specify the path to ArrayFire
99 installation root as `ArrayFire_DIR` variable.
101 For example, if ArrayFire were installed locally to `/home/user/ArrayFire` then
102 you would modify the `cmake` command above to contain the following definition:
104 cmake -DArrayFire_DIR=/home/user/ArrayFire ..
106 You can also specify this information in the `ccmake` command-line interface.
110 Building ArrayFire projects with Makefiles is fairly similar to CMake except you
111 must specify all paths and libraries manually.
113 As with any `make` project, you need to specify the include path to the
114 directory containing `arrayfire.h` file. This should be `-I
115 /opt/arrayfire/include` if you followed our installation instructions.
117 Similarly, you will need to specify the path to the ArrayFire library using the
118 `-L` option (e.g. `-L/opt/arrayfire/lib`) followed by the specific ArrayFire
119 library you wish to use using the `-l` option (for example `-lafcpu`,
120 `-lafopencl`, `-lafcuda`, or `-laf` for the CPU, OpenCL, CUDA, and unified
121 backends, respectively.
123 Here is a minimal example Makefile which uses ArrayFire's CPU backend:
126 LIB_PATHS=-L/opt/arrayfire/lib
127 INCLUDES=-I/opt/arrayfire/include
128 CC=g++ $(COMPILER_OPTIONS)
129 COMPILER_OPTIONS=-std=c++11 -g
131 all: main.cpp Makefile
132 $(CC) main.cpp -o test $(INCLUDES) $(LIBS) $(LIB_PATHS)