diff --git a/hpvm/test/benchmarks/CMakeLists.txt b/hpvm/test/benchmarks/CMakeLists.txt
index 4d4f0691fc0e70cd9b532947688da264b2ca4e7b..ab12243bebac18a1570a1cd05e90b5309faed84a 100644
--- a/hpvm/test/benchmarks/CMakeLists.txt
+++ b/hpvm/test/benchmarks/CMakeLists.txt
@@ -34,5 +34,22 @@ function(add_hpvm_benchmark
   add_custom_target(${target_name} DEPENDS ${output_bin_path})
 endfunction(add_hpvm_benchmark)
 
+function(add_hpvm_cpu_gpu_benchmark
+  target_prefix common_flags language_mode main_src util_bitcodes
+)
+  set(all_flags_cpu ${common_flags} "-DDEVICE=CPU_TARGET")
+  add_hpvm_benchmark(
+    "${target_prefix}_cpu" "${target_prefix}-cpu" "${all_flags_cpu}" ${language_mode}
+    ${main_src} "${util_bitcodes}"
+  )
+  if(HPVM_USE_OPENCL)
+    set(all_flags_gpu ${common_flags} "--opencl" "-DDEVICE=GPU_TARGET")
+    add_hpvm_benchmark(
+      "${target_prefix}_gpu" "${target_prefix}-gpu" "${all_flags_gpu}" ${language_mode}
+      ${main_src} "${util_bitcodes}"
+    )
+  endif()
+endfunction()
+
 add_subdirectory(hpvm-cava)
 add_subdirectory(pipeline)
diff --git a/hpvm/test/benchmarks/hpvm-cava/CMakeLists.txt b/hpvm/test/benchmarks/hpvm-cava/CMakeLists.txt
index d89a01a05669a0c49fecf1ccbd25029fce14143b..33867168d10f6ed34eb067df9792fdbef9b76276 100644
--- a/hpvm/test/benchmarks/hpvm-cava/CMakeLists.txt
+++ b/hpvm/test/benchmarks/hpvm-cava/CMakeLists.txt
@@ -8,17 +8,4 @@ set(
 
 # Sets ${util_bitcodes}
 hpvm_compile_util_sources("${compiler_flags}" "${other_srcs}" "c")
-
-set(all_flags_cpu ${compiler_flags} "-DDEVICE=CPU_TARGET" "-lpthread")
-add_hpvm_benchmark(
-  "hpvm_cava_cpu" "hpvm-cava-cpu" "${all_flags_cpu}" "c"
-  src/main.c "${util_bitcodes}"
-)
-
-if(HPVM_USE_OPENCL)
-  set(all_flags_gpu ${compiler_flags} "-DDEVICE=GPU_TARGET" "--opencl" "-lpthread")
-  add_hpvm_benchmark(
-    "hpvm_cava_gpu" "hpvm-cava-gpu" "${all_flags_gpu}" "c"
-    src/main.c "${util_bitcodes}"
-  )
-endif()
+add_hpvm_cpu_gpu_benchmark(hpvm_cava "${compiler_flags}" c src/main.c "${util_bitcodes}")
diff --git a/hpvm/test/benchmarks/pipeline/CMakeLists.txt b/hpvm/test/benchmarks/pipeline/CMakeLists.txt
index db2b8eeab1647f4ac72c05127877ecec1f949c90..cede337ca2f606a9008056f42ad0b8b9c5aec00c 100644
--- a/hpvm/test/benchmarks/pipeline/CMakeLists.txt
+++ b/hpvm/test/benchmarks/pipeline/CMakeLists.txt
@@ -1,15 +1,16 @@
 find_package(OpenCV 2)
 if(${OpenCV_FOUND})
-  set(
-    all_flags
-    -O3 -I${OpenCV_INCLUDE_DIRS}
-    -ffast-math -fno-lax-vector-conversions -fno-vectorize -fno-slp-vectorize
-    -lpthread
+  foreach(incl_dir ${OpenCV_INCLUDE_DIRS})
+    list(APPEND all_flags "-I${incl_dir}")
+  endforeach()
+  foreach(link_lib ${OpenCV_LIBS})
+    list(APPEND all_flags "-l${link_lib}")
+  endforeach()
+  list(
+    APPEND all_flags -L "${OpenCV_INSTALL_PATH}/lib"
+    -O3 -ffast-math -fno-lax-vector-conversions -fno-vectorize -fno-slp-vectorize
   )
-  add_hpvm_benchmark("pipeline_cpu" "pipeline-cpu" "${all_flags}" "c++" src/main.cc "")
-  if(HPVM_USE_OPENCL)
-    add_hpvm_benchmark("pipeline_gpu" "pipeline-gpu" "${all_flags};--opencl" "c++" src/main.cc "")
-  endif()
+  add_hpvm_cpu_gpu_benchmark("pipeline" "${all_flags}" c++ src/main.cc "")
 else()
   message(WARNING "opencv-2 not found; not compiling HPVM benchmark 'pipeline'.")
 endif()
diff --git a/hpvm/test/benchmarks/pipeline/src/main.cc b/hpvm/test/benchmarks/pipeline/src/main.cc
index 057c13b62745ba618b13b9f2c1443fb41ca45bdb..5fcb205ca7b441d5e01c7c797bc34d53428f1a12 100644
--- a/hpvm/test/benchmarks/pipeline/src/main.cc
+++ b/hpvm/test/benchmarks/pipeline/src/main.cc
@@ -27,6 +27,10 @@
 #define HEIGHT 640
 #define WIDTH 480
 
+#ifndef DEVICE
+#error "The macro 'DEVICE' must be defined to CPU_TARGET or GPU_TARGET."
+#endif
+
 std::string input_window = "GPU Pipeline - Input Video";
 std::string output_window = "GPU Pipeline - Edge Mapping";