CMake:如何将CUDA添加到现有项目 [英] CMake: how to add cuda to existing project
问题描述
我有一个构建库的项目,我想向它添加一些cuda支持。
I have a project that builds a library and I want to add some cuda support to it.
结构为:
| Basedir
| _subdir1
| _subdir2
|Basedir
|_subdir1
|_subdir2
CMakeLists.txt文件的基本结构:(subdir2不重要)。
在Basedir中:
The basic structure of the CMakeLists.txt files: (subdir2 is not important).
in Basedir:
cmake_minimum_required(VERSION 2.6)
PROJECT(myproject)
find_package(CUDA)
INCLUDE_DIRECTORIES(${MYPROJECT_SOURCE_DIR})
ADD_SUBDIRECTORY(subdir1)
ADD_SUBDIRECTORY(subdir2)
在子目录1中:
ADD_LIBRARY(mylib shared
file1.cpp
file2.cpp
file3.cpp
)
INSTALL(
TARGETS mylib
DESTINATION lib
PERMISSIONS
OWNER_READ OWNER_WRITE OWNER_EXECUTE
GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
)
FILE(GLOB_RECURSE HEADERS RELATIVE ${MYPROJECT_SOURCE_DIR}/myproject *.h)
FOREACH(HEADER ${HEADERS})
STRING(REGEX MATCH "(.*)[/\\]" DIR ${HEADER})
INSTALL(FILES ${HEADER} DESTINATION include/myproject/${DIR})
ENDFOREACH(HEADER)
我实际上并不真正知道如何放置cuda支持它。我想将file2.cpp替换为file2.cu,但是这样做了,但是它没有构建.cu文件,仅构建了cpp文件。
I actually don't really know how to put the cuda-support into it. I want to replace file2.cpp with file2.cu and I did that, but it didn't build the .cu file, only the cpp files.
是否必须添加CUDA_ADD_EXECUTABLE()来包含任何cuda文件?然后如何将其链接到其他文件?
Do I have to add CUDA_ADD_EXECUTABLE() to include any cuda-files? How will I then link it to the other files?
我尝试将以下内容添加到subdir1中的CMakeLists.txt中:
I tried adding the following to the CMakeLists.txt in subdir1:
CUDA_ADD_EXECUTABLE(cuda file2.cu OPTIONS -arch sm_20)
这将编译文件,但会生成可执行的cuda。如何将其链接到mylib?
刚好?
That will compile the file but build an executable cuda. How do I link it to mylib? Just with?:
TARGET_LINK_LIBRARIES(cuda mylib)
我必须承认我没有cmake经验,但是我想你已经明白了。
I have to admit that I'm not experienced in cmake, but I guess you figured that.
推荐答案
您可以将 CUDA_ADD_LIBRARY
用于 mylib
项目。它可用于 CUDA_ADD_EXECUTABLE
,但适用于库。
You can use CUDA_ADD_LIBRARY
for mylib
project. It works as CUDA_ADD_EXECUTABLE
but for libraries.
CUDA_ADD_LIBRARY(mylib SHARED
file1.cpp
file2.cu
file3.cpp
OPTIONS -arch sm_20
)
TARGET_LINK_LIBRARIES(mylib ${CUDA_LIBRARIES})
这篇关于CMake:如何将CUDA添加到现有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!