The CMAKE_CXX_FLAGS
可能会干扰 NVCC 编译,因此您必须小心放置它们的位置。
我仅使用 CUDA 库通过一个非常简单的示例重新创建了您的设置。重新排列和编辑一些 CMake 命令后,我能够编译并运行该程序。
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 文件中推断出来,但为了澄清起见,它看起来像:
项目目录:
- CMakeLists.txt
-
include
-
src
工具.cuh:
#ifndef TOOLS_CUH
#define TOOLS_CUH
extern "C"
{
void do_cuda_stuff();
}
#endif
工具.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
}
}
工具.hpp:
#ifndef TOOLS_HPP
#define TOOLS_HPP
class Tools
{
public:
int execute();
};
#endif
工具.cpp:
#include "Tools.hpp"
#include "Tools.cuh"
int Tools::execute()
{
do_cuda_stuff();
return 0;
}
主要.cpp:
#include "Tools.hpp"
int main(void)
{
Tools tools;
return tools.execute();
}
Output:
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
多变的。希望这可以帮助!干杯!