简体   繁体   English

使用Xcode和CMake构建CUDA库失败

[英]Build CUDA library using Xcode and CMake fail

I am trying to use CMake to generate a Xcode project and build a CUDA library with it. 我正在尝试使用CMake生成Xcode项目并使用它构建CUDA库。 The code I used for collecting & building CUDA library "caffe2_cpp_gpu" is as follows: 我用于收集和构建CUDA库“ caffe2_cpp_gpu”的代码如下:

list(APPEND CUDA_NVCC_FLAGS "-std=c++11" "-Wno-deprecated-gpu-targets")    
file(GLOB CUDA_SOURCES "${PROJECT_SOURCE_DIR}/source/caffe2/operator/*.cu" )
  cuda_add_library(caffe2_cpp_gpu STATIC ${CUDA_SOURCES})
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang++" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
  list(APPEND ALL_LIBRARIES -Wl,-force_load caffe2_cpp_gpu)
else()
  list(APPEND ALL_LIBRARIES -Wl,--whole-archive caffe2_cpp_gpu -Wl,--no-whole-archive)
endif()

Note that this code can be run if Unix Makefile is used. 请注意,如果使用Unix Makefile,则可以运行此代码。 But cannot run if Xcode is used. 但是,如果使用Xcode,则无法运行。
In the project it shows a file is missing: missing file 在项目中,它显示文件丢失: 文件丢失
And the CUDA library "caffe2_cpp_gpu" cannot be created. 并且无法创建CUDA库“ caffe2_cpp_gpu”。

Does anyone know how to solve it? 有人知道如何解决吗?

I found the answer... If I append another kind of file (.cc, .h, etc.) into the CUDA libary, it will work. 我找到了答案...如果我将另一种文件(.cc,.h等)附加到CUDA库中,它将起作用。 It seems like XCode cannot build single .cu file into static library (.a). 似乎XCode无法将单个.cu文件构建到静态库(.a)中。
So here is what I did: 所以这就是我所做的:

file(GLOB All_SOURCE "${PROJECT_SOURCE_DIR}/source/caffe2/operator/*.cu" "${PROJECT_SOURCE_DIR}/source/caffe2/operator/*.cc" "${PROJECT_SOURCE_DIR}/source/caffe2/operator/*.h")
cuda_add_library(caffe2_lib ${All_SOURCE})

Instead of building library seperately for CPU and GPU. 而不是为CPU和GPU分别构建库。 I merge them into a single library file. 我将它们合并到一个库文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM