CPP项目中使用CUDA(opencv+cuda or pybind+cuda)

该文展示了如何使用CMake构建结合CUDA和OpenCV的C++项目。它包括设置C++标准、查找CUDA和OpenCV库、定义NVCC标志、编译CUDA源码以及链接OpenCV库。此外,还提及了与Python绑定的PyBind11库的使用。
摘要由CSDN通过智能技术生成

在这里插入图片描述

cmake_minimum_required(VERSION 2.8.12)
project(flow LANGUAGES CXX)


set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include_directories(include
        ${CUDA_INCLUDE_DIRS}
        ${OpenCV_INCLUDE_DIRS}
        )

link_directories(${OpenCV_LIBRARY_DIRS})

find_package(CUDA REQUIRED)
find_package(OpenCV REQUIRED)


FILE(GLOB SOURCES "*.cu" "*.cpp" "*.cuh" "*.h" )

set(CUDA_NVCC_FLAGS "-g -G")

CUDA_ADD_EXECUTABLE(flow hell0.cu example.cpp)  # add_executable(flow example.cpp)

target_link_libraries(flow ${OpenCV_LIBS})
#include "hell0.cuh"
int main(){
    cuda_main();
    return 0;
}
#include <stdio.h>

__global__ void cuda_hello(){
    printf("gpu\n");
}



extern "C" void cuda_main() {
    cuda_hello<<<1,10>>>();
    cudaDeviceSynchronize();//witout this no output
}

extern "C"
void cuda_main();

opencv+cuda

cmake_minimum_required(VERSION 3.5)

project(cuda_cv LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include_directories(include
        ${CUDA_INCLUDE_DIRS}
        ${OpenCV_INCLUDE_DIRS}
        )

link_directories(${OpenCV_LIBRARY_DIRS})

find_package(CUDA REQUIRED)
find_package(OpenCV REQUIRED)

#INCLUDE(/home/psdz/cmake-3.9.0/Modules/FindCUDA.cmake)

FILE(GLOB SOURCES "*.cu" "*.cpp" "*.c" "*.h")

set(CUDA_NVCC_FLAGS "-g -G")

CUDA_ADD_EXECUTABLE(cuda_cv main.cu)

target_link_libraries(cuda_cv ${OpenCV_LIBS})

pybind+cuda

cmake_minimum_required(VERSION 2.8.12)
project(example LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PYBIND11_PYTHON_VERSION "3.7")

#include_directories("/usr/include/python3.10/")

include_directories(include
        ${CUDA_INCLUDE_DIRS}
        ${OpenCV_INCLUDE_DIRS}
        )
link_directories(${OpenCV_LIBRARY_DIRS})

find_package(CUDA REQUIRED)
find_package(OpenCV REQUIRED)
include_directories("/home/pdd/anaconda3/envs/yolocopy/bin/")


FILE(GLOB SOURCES "*.cu" "*.cpp" "*.cuh" "*.h" )

set(CUDA_NVCC_FLAGS "-g -G")

add_subdirectory(pybind11)
pybind11_add_module(example example.cpp)
#add_executable(untitled example.cpp)

#target_link_libraries(${PROJECT_NAME} INTERFACE
#        ${OpenCV_LIBRARIES}
##        pybind11::pybind11
#        )
target_link_libraries(${PROJECT_NAME} PRIVATE
       ${OpenCV_LIBRARIES}
        pybind11::pybind11
        )
#link_libraries(${OpenCV_LIBRARIES})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值