我在使用cmake
和make
编译应用程序时遇到问题。项目的源文件组织如下:
SOURCE/
CMakeLists.txt
myApp.cc
include/
classA.hh
classB.hh
src/
classA.cc
classB.cc
classB.cu
我的CMakeLists.txt文件如下:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(myApp)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
FIND_PACKAGE(GDCM REQUIRED)
IF(GDCM_FOUND)
INCLUDE(${GDCM_USE_FILE})
SET(GDCM_LIBRARIES gdcmCommon vtkgdcm)
ELSE(GDCM_FOUND)
MESSAGE(FATAL_ERROR "Cannot find GDCM, did you set GDCM_DIR?")
ENDIF(GDCM_FOUND)
SET(CUDA_TOOLKIT_ROOT_DIR="/Developer/NVIDIA/CUDA-7.5/")
FIND_PACKAGE(CUDA REQUIRED)
SET(CUDA_PROPAGATE_HOST_FLAGS ON)
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -gencode arch=compute_30,code=sm_30)
SET(LIB_TYPE SHARED)
SET(CUDA_SEPARABLE_COMPILATION ON)
LINK_DIRECTORIES(/lib/FFTW/INSTALL/lib)
INCLUDE_DIRECTORIES(/lib/FFTW/INSTALL/include)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
FILE(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)
FILE(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
FILE(GLOB cudafile ${PROJECT_SOURCE_DIR}/src/*.cu)
CUDA_ADD_EXECUTABLE(myApp myApp ${headers} ${sources} ${cudafile})
TARGET_LINK_LIBRARIES(myApp ${VTK_LIBRARIES} ${GDCM_LIBRARIES} fftw3)
当我尝试使用make编译项目时(成功运行cmake之后),我得到:
nvcc fatal : A single input file is required for a non-link phase when an outputfile is specified
CMake Error at myApp_generated_classB.cu.o.cmake:207 (message):
Error generating
../BUILD/CMakeFiles/myApp.dir/src/./myApp_generated_classB.cu.o
是否将类的源文件分解为.cc和.cu文件有问题?
答案 0 :(得分:2)
这不是对问题中所述问题的确切原因的全面解释;然而,它以相当令人满意的方式解决了这个问题。
首先,显然使用FIND_PACKAGE(VTK)
(因此似乎需要 VTK CMake文件的FIND_PACKAGE(GDCM)与{{1}之间存在冲突})和vtkgdcm
。这是最近reported on Mantis。为避免这种冲突,我使用:
nvcc
代替,
LINK_DIRECTORIES( {VTK_Directory}/INSTALL/lib)
INCLUDE_DIRECTORIES({VTK_Directory}/INSTALL/include/vtk-6.2)
LINK_DIRECTORIES( {GDCM_Directory}/INSTALL/lib)
INCLUDE_DIRECTORIES({GDCM_Directory}/INSTALL/include/gdcm-2.4)
第二次,对于项目的CUDA部分,我将所有内容放入FIND_PACKAGE(VTK REQUIRED)
FIND_PACKAGE(GDCM REQUIRED)
文件并使用.cu
创建目标文件。然后我使用本机C ++编译器像往常一样使用CUDA_COMPILE(cuda_o myCUDAstudff.cu)
创建可执行文件。由于我使用本机C ++编译器而不是nvcc,我需要在我的内核(myCUDAstudff.cu)文件中包含以下头文件:
ADD_EXECUTABLE( ... ${cuda_o})
并链接到#include <cuda.h>
#include <cuda_runtime.h>
中的libcudart
,我使用了共享库。我想不出用libcudart_static.a做同样的方法。
或者,可以使用TARGET_LINK_LIBRARIES()
代替上述所有步骤(即CUDA_COMPILE(),ADD_EXECUTABLE(),...)。