且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有C ++ 11的CUDA 6.0的CMake脚本

更新时间:2023-11-10 18:06:46

CMAKE_CXX_FLAGS可能会干扰NVCC编译,因此您必须谨慎放置它们.

The CMAKE_CXX_FLAGS can interfere with the NVCC compilation so you have to be careful about where you put them.

我仅使用CUDA库,通过一个非常简单的示例重新创建了您的设置.重新排列和编辑了一些CMake命令后,我得以编译并运行该程序.

I recreated your setup with a really simple example using only the CUDA library. After rearranging and editing a few of the CMake commands I was able to compile and run the program.

CMakeLists.txt:

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(pupil_tracker)

include_directories(include)
include_directories(include/cuda)

# removed a space and added C++11 functionality (note the double '--' for NVCC)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_20,code=sm_20;--std=c++11)
set(CUDA_HOST_COMPILER clang++) # shouldn't fail anymore

# didn't test with Boost
# find_package(Boost COMPONENTS system filesystem REQUIRED)
# include_directories(${Boost_INCLUDE_DIR})

find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})

# didn't test with OpenCV
# set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}) # So CMake finds FindOpenCV.cmake
# find_package(OpenCV REQUIRED)
# include_directories(${OpenCV_INCLUDE_DIRS})

CUDA_ADD_LIBRARY(cuda_obj STATIC src/cuda/Tools.cu) # works for me now

# moved the CXX flags to after the cuda_add_library call
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
add_executable(main src/main.cpp src/Tools.cpp) # only used one c++ class for this test

# target_link_libraries(main ${Boost_LIBRARIES})
target_link_libraries(main ${CUDA_LIBRARIES})
# target_link_libraries(main ${OpenCV_LIBS})
target_link_libraries(main cuda_obj) # changed ${cuda_obj} to cuda_obj

install(TARGETS main DESTINATION ../bin)

可以从CMake文件中推断项目目录的设置,但为澄清起见,它看起来像:

The project directory setup can be inferred from the CMake file but for clarification it looks like:

ProjectDir:

ProjectDir:

  • CMakeLists.txt
  • 包括
    • Tools.hpp
    • cuda
      • Tools.cuh
      • CMakeLists.txt
      • include
        • Tools.hpp
        • cuda
          • Tools.cuh
          • main.cpp
          • Tools.cpp
          • cuda
            • Tools.cu
            • main.cpp
            • Tools.cpp
            • cuda
              • Tools.cu

              Tools.cuh:

              Tools.cuh:

              #ifndef TOOLS_CUH
              #define TOOLS_CUH
              
              extern "C"
              {
                  void do_cuda_stuff();
              }
              
              #endif
              

              Tools.cu:

              #include <cuda_runtime.h>
              #include <stdio.h>
              
              extern "C"
              {
                  __global__
                  void do_cuda_stuff_kernel()
                  {
                      int idx = blockIdx.x * blockDim.x + threadIdx.x;
                      printf("Hello from thread %d!\n", idx);
                  }
              
                  void do_cuda_stuff()
                  {
                      // 2 blocks, 3 threads each
                      do_cuda_stuff_kernel<<<2, 3>>>();
                      cudaDeviceSynchronize(); // to print results
                  }
              }
              

              Tools.hpp:

              Tools.hpp:

              #ifndef TOOLS_HPP
              #define TOOLS_HPP
              
              class Tools
              {
              public:
                  int execute();
              };
              
              #endif
              

              Tools.cpp:

              Tools.cpp:

              #include "Tools.hpp"
              #include "Tools.cuh"
              
              int Tools::execute()
              {
                  do_cuda_stuff();
                  return 0;
              }
              

              main.cpp:

              #include "Tools.hpp"
              
              int main(void)
              {
                  Tools tools;
                  return tools.execute();
              }
              

              输出:

              Hello from thread 3!
              Hello from thread 4!
              Hello from thread 5!
              Hello from thread 0!
              Hello from thread 1!
              Hello from thread 2!
              

              我目前正在使用CUDA 7.5和OSX 10.10,但我认为最大的问题是CMAKE_CXX_FLAGS变量的位置.希望这可以帮助!干杯!

              I'm currently using CUDA 7.5 and OSX 10.10 but I think the biggest issue was the placement of the CMAKE_CXX_FLAGS variable. Hope this helps! Cheers!