$darkmode
A high-performance general-purpose compute library
/home/abuild/rpmbuild/BUILD/arrayfire-full-v3.9.0/docs/pages/using_on_windows.md
Go to the documentation of this file.
1 Using ArrayFire with Microsoft Windows and Visual Studio {#using_on_windows}
2 ============================================================================
3 
4 If you have not already done so, please make sure you have installed,
5 configured, and tested ArrayFire following the [installation instructions](#installing).
6 
7 # The big picture {#big-picture-windows}
8 
9 The ArrayFire Windows installer creates the following:
10 1. **AF_PATH** environment variable to point to the installation location. The
11  default install location is `C:\Program Files\ArrayFire\v3`
12 2. **AF_PATH/include** : Header files for ArrayFire (include directory)
13 3. **AF_PATH/lib** : All ArrayFire backends libraries, dlls and dependency dlls
14  (library directory)
15 4. **AF_PATH/examples** : Examples to get started.
16 5. **AF_PATH/cmake** : CMake config files
17 6. **AF_PATH/uninstall.exe** : Uninstaller
18 
19 The installer will prompt the user for following three options.
20 * Do not add **%%AF_PATH%/lib** to PATH
21 * Add **%%AF_PATH%/lib** to PATH environment variable of current user
22 * Add **%%AF_PATH%/lib** to PATH environment variable for all users
23 
24 If you chose not to modify PATH during installation please make sure to do so
25 manually so that all applications using ArrayFire libraries will be able to find
26 the required DLLs.
27 
28 # Build and Run Helloworld {#section1}
29 
30 This can be done in two ways either by using CMake build tool or using Visual
31 Studio directly.
32 
33 ## Using CMake {#section1part1}
34 1. Download and install [CMake](https://cmake.org/download/), preferrably the
35  latest version.
36 2. Open CMake-GUI and set the field __Where is the source code__ to the root
37  directory of examples.
38 3. Set the field __Where to build the binaries__ to
39  **path_to_examples_root_dir/build** and click the `Configure` button towards
40  the lower left bottom.
41 4. CMake will prompt you asking if it has to create the `build` directory if
42  it's not already present. Click yes to create the build directory.
43 5. Before the configuration begins, CMake will show you a list(drop-down menu)
44  of available Visual Studio versions on your system to chose from. Select one
45  and check the radio button that says **Use default native compilers** and
46  click finish button in the bottom right corner.
47 6. CMake will show you errors in red text if any once configuration is finished.
48  Ideally, you wouldn't need to do anything and CMake should be able to find
49  ArrayFire automatically. Please let us know if it didn't on your machine.
50 7. Click **Generate** button to generate the Visual Studio solution files for
51  the examples.
52 8. Click **Open Project** button that is right next to **Generate** button to
53  open the solution file.
54 9. You will see a bunch of examples segregated into three sets named after the
55  compute backends of ArrayFire: cpu, cuda & opencl if you have installed all
56  backends. Select the helloworld project from any of the installed backends
57  and mark it as startup project and hit `F5`.
58 10. Once the helloworld example builds, you will see a console window with the
59  output from helloworld program.
60 
61 ## Using Visual Studio {#section1part2}
62 
63 1. Open Visual Studio of your choice and create an empty C++ project.
64 2. Right click the project and add an existing source file
65  `examples/helloworld/helloworld.cpp` to this project.
66 3. Add `"$(AF_PATH)/include;"` to _Project Properties -> C/C++ -> General ->
67  Additional Include Directories_.
68 4. Add `"$(AF_PATH)/lib;"` to _Project Properties -> Linker -> General ->
69  Additional Library Directories_.
70 5. Add `afcpu.lib` or `afcuda.lib` or `afopencl.lib` to _Project Properties ->
71  Linker -> Input -> Additional Dependencies_. based on your preferred backend.
72 6. (Optional) You may choose to define `NOMINMAX`, `AF_<CPU/CUDA/OPENCL>` and/or
73  `AF_<DEBUG/RELEASE>` in your projects. This can be added to _Project
74  Properties -> C/C++ -> General -> Preprocessor-> Preprocessory definitions_.
75 7. Build and run the project. You will see a console window with the output from
76  helloworld program.
77 
78 # Using ArrayFire within Existing Visual Studio Projects {#section2}
79 This is divided into three parts:
80 * [Part A: Adding ArrayFire to an existing solution (Single Backend)](#section2partA)
81 * [Part B: Adding ArrayFire CUDA to a new/existing CUDA project](#section2partB)
82 * [Part C: Project with all ArrayFire backends](#section2partC)
83 
84 ## Part A: Adding ArrayFire to an existing solution (Single Backend) {#section2partA}
85 
86 Note: If you plan on using Native CUDA code in the project, use the steps under
87 [Part B](#section2partB).
88 
89 Adding a single backend to an existing project is quite simple.
90 
91 1. Add `"$(AF_PATH)/include;"` to _Project Properties -> C/C++ -> General ->
92  Additional Include Directories_.
93 2. Add `"$(AF_PATH)/lib;"` to _Project Properties -> Linker -> General ->
94  Additional Library Directories_.
95 3. Add `afcpu.lib`, `afcuda.lib`, `afopencl.lib`, or `af.lib` to _Project
96  Properties -> Linker -> Input -> Additional Dependencies_. based on your
97  preferred backend.
98 
99 ## Part B: Adding ArrayFire CUDA to a new/existing CUDA project {#section2partB}
100 Lastly, if your project contains custom CUDA code, the instructions are slightly
101 different as it requires using a CUDA NVCC Project:
102 
103 1. Create a custom "CUDA NVCC project" in Visual Studio
104 2. Add `"$(AF_PATH)/include;"` to _Project Properties -> CUDA C/C++ -> General
105  -> Additional Include Directories_.
106 3. Add `"$(AF_PATH)/lib;"` to _Project Properties -> Linker -> General ->
107  Additional Library Directories_.
108 4. Add `afcpu.lib`, `afcuda.lib`, `afopencl.lib`, or `af.lib` to _Project Properties ->
109  Linker -> Input -> Additional Dependencies_. based on your preferred backend.
110 
111 ### Part C: Project with all ArrayFire backends {#section2partC}
112 If you wish to create a project that allows you to use all the ArrayFire
113 backends with ease, you should use `af.lib` in step 3 from [Part
114 A](#section2partA).
115 
116 You can alternately download the template project from [ArrayFire Template
117 Projects](https://github.com/arrayfire/arrayfire-project-templates)
118 
119 # <a name="section3" />Using ArrayFire with CMake
120 ArrayFire ships with a series of CMake scripts to make finding and using our
121 library easy.
122 
123 First create a file called `CMakeLists.txt` in your project directory:
124 
125  cd your-project-directory
126  touch CMakeLists.txt
127 
128 and populate it with the following code:
129 
130  find_package(ArrayFire)
131  add_executable(<my_executable> [list your source files here])
132 
133  # To use Unified backend, do the following.
134  # Unified backend lets you choose the backend at runtime
135  target_link_libraries(<my_executable> ArrayFire::af)
136 
137 where `<my_executable>` is the name of the executable you wish to create. See the
138 [CMake documentation](https://cmake.org/documentation/) for more information on
139 how to use CMake. To link with a specific backend directly, replace the
140 `ArrayFire::af` with the following for their respective backends.
141 
142 * `ArrayFire::afcpu` for CPU backend.
143 * `ArrayFire::afcuda` for CUDA backend.
144 * `ArrayFire::afoneapi` for oneAPI backend.
145 * `ArrayFire::afopencl` for OpenCL backend.
146 
147 Next we need to instruct CMake to create build instructions and then compile. We
148 suggest using CMake's out-of-source build functionality to keep your build and
149 source files cleanly separated. To do this open the CMake GUI.
150 
151 * Under source directory, add the path to your project
152 * Under build directory, add the path to your project and append /build
153 * Click configure and choose a 64 bit Visual Studio generator.
154 * If configuration was successful, click generate. This will create a
155  my-project.sln file under build. Click `Open Project` in CMake-GUI to open the
156  solution and compile the ALL_BUILD project.