Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Using cf4ocl in a new project

Nuno Fachada edited this page Jul 4, 2016 · 2 revisions

This guide presumes you have cf4ocl installed in your system.

Include header

Source files should first include the cf4ocl header:

#include <cf4ocl2.h>

Compiling and linking

The following assumes a working installation of the pkg-config build tool, which is usually bundled with GLib. If this is not the case, or if you prefer not to use this tool, click here for an alternative approach.

If pkg-config can't find cf4ocl, make sure the cf4ocl2.pc file is in its search path. For example, if cf4ocl2 was installed in /usr/local/ (which is not a default library search path in some OSes), one would first set the PKG_CONFIG_PATH environment variable with the appropriate value:

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

There are two ways of using cf4ocl in your project:

  • Using the CMake build framework.
  • Manually specifying compiler options in the command line.

The latter option gives a good idea of how to specify these dependencies with other build systems.

CMake

If client code uses CMake as its build system, then it's very simple to integrate cf4ocl. Simply add the following lines to your CMakeLists.txt file:

# Find required libraries
find_package(PkgConfig REQUIRED)
pkg_check_modules(CF4OCL2 REQUIRED cf4ocl2>=2.0.0)

# Library include directories
include_directories(${CF4OCL2_INCLUDE_DIRS})

Then, for each target, specify cf4ocl as a link library:

add_executable(myprog myprog.c)
target_link_libraries(myprog ${CF4OCL2_LIBRARIES})

CMake can then create the project or build files for several targets such as Unix Makefiles, Visual Studio, Xcode, Eclipse, etc.

The cf4ocl-examples project follows this approach.

Manually

If you prefer to manually set build options, directly in the compiler/linker options, in a makefile or in your IDE, follow these guidelines.

Compiling

Depending on how cf4ocl was installed, it may be necessary to specify the location of the cf4ocl header (and the headers of the cf4ocl dependencies) with the -I flag. These can be obtained with pkg-config:

pkg-config --cflags cf4ocl2
Linking

It is necessary to specify cf4ocl (and other libraries) with the -l (and possibly -L) flags. Using pkgconfig, this is again a simple process:

pkg-config --libs cf4ocl2
Example with gcc or Clang

For example, to compile and link a simple program which uses cf4ocl with gcc or clang, one could do:

$ gcc `pkg-config --cflags cf4ocl2` myprog.c -o myprog `pkg-config --libs cf4ocl2`

Additional notes

You can try this with the mysum program described in the tutorial.