Cmake + CUDA + OpenCV [英] Cmake + CUDA + OpenCV
问题描述
我有一个使用OpenCV的工作C ++项目。我创建了一个CMakeLists.txt文件,以帮助人们编译它,一切工作正常。
I have a working C++ project that uses OpenCV. I am creating a CMakeLists.txt file to help people compile it, and everything works fine.
简而言之:如何使用CMake创建一个makefile编译一个 .cu文件转换为与NVCC的.o,并将其与其余的.o文件链接?
In short : How do I use CMake to create a makefile to compile a ".cu" file into a ".o" with NVCC and link it with the rest of the ".o" files ?
长版本:
想要添加一个必须使用NVCC编译的CUDA文件。
我设法手动编译,但我不知道如何使用CMake创建一个makefile这样做:
The long version : I want to add a CUDA file that has to be compiled with NVCC. I manage to compile it by hand, but I can't figure out how to use CMake to create a makefile for doing so:
- 在CMake中将NVCC将我的.cu文件编译为.o文件(如果安装了CUDA工具包)
- 使用g ++创建的.o文件
- 不使用NVCC编译所有项目(如果没有CUDA支持,我的项目仍然可以编译)
现在,我有这样的:
IF(CUDA_FOUND)
MESSAGE( STATUS "I found CUDA !" )
SET(HAVE_CUDA ${CUDA_FOUND} CACHE BOOL "Set to TRUE if CUDA is found, FALSE otherwise")
cuda_compile(CudaKernel CudaKernels.cu)
ENDIF(CUDA_FOUND)
这会创建一个.o文件,但我不知道在哪里/如何使用它。
This creates a .o file, but I don't know where/how to use it.
然后我计划添加一个标志:
I then plan to add a flag like:
if(HAVE_CUDA)
set (CMAKE_CXX_FLAGS "-DGPU_OPENCV_ENABLE=1")
endif(HAVE_CUDA)
任何想法?
推荐答案
好的,我想出来了:
我们可以使用Cmake为cuda部分创建一个目标文件并链接到cpp可执行文件编译结束。
We can use Cmake to create an object file for the cuda part and link it to the cpp executable at the end of the compilation.
cmake_minimum_required(VERSION 2.8)
find_package(CUDA REQUIRED)
IF(CUDA_FOUND)
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-O3 -gencode arch=compute_20,code=sm_20)
cuda_compile(CudaKernel CudaKernels.cu)
cuda_add_library(CudaKernels CudaKernels.cu)
add_definitions(-DGPU_OPENCV_ENABLE)
ENDIF()
CMake:
target_link_libraries(Executablename
CudaKernels
libraries...)
希望它帮助别人!
这篇关于Cmake + CUDA + OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!