我尝试使用CUDA和C ++代码构建带有静态库的项目。它需要与外部库(moderngpu)链接。 CUDA和C ++代码各自独立工作,并且之前已经使用make编译。但是,当尝试使用CMake构建代码时(通过CLion,如果重要),我收到以下错误:
Error running 'main': Cannot run program "/home/mnemonic/Programs/myProject/bin/src/CMakeFiles/hvm.dir/cmake_device_link.o" (in directory "/home/mnemonic/Programs/myProject/bin/src/CMakeFiles/hvm.dir"): error=13, Permission denied
我已经试图解决这个问题好几天了,但是还没有取得任何进展。我根本没有获得权限错误是没有意义的,当我将其设置为可执行文件时,它所抱怨的.o
文件无法运行。我最好的猜测是它的某种链接问题。任何帮助将不胜感激。
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.9 FATAL_ERROR)
PROJECT(myProject LANGUAGES CXX CUDA)
FIND_PACKAGE(CUDA 8.0 REQUIRED)
SET(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
-Wall -Wextra -Wno-deprecated -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op \
-Wmissing-declarations -Wredundant-decls -Wshadow -Woverloaded-virtual -Wpedantic \
-ffast-math \
")
SET(CMAKE_CUDA_COMPILER /usr/local/cuda-8.0/bin/nvcc)
SET(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} \
-Xcompiler -pthread,-fno-strict-aliasing,-g,-O2,-g,-fwrapv,-O3,-Wall,-fPIC \
-gencode arch=compute_61,code=sm_61 \
")
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(lib)
src/CMakeLists.txt
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} source)
ADD_EXECUTABLE(stuff ${source})
TARGET_LINK_LIBRARIES(stuff LINK_PUBLIC thing thingKernels)
lib/CMakeLists.txt
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
INCLUDE_DIRECTORIES(
/usr/local/src/moderngpu/include
/usr/local/cuda-8.0/include
)
ADD_LIBRARY(thing STATIC
${CMAKE_CURRENT_SOURCE_DIR}/thing.cpp
)
ADD_LIBRARY(thingKernels STATIC
../include/cuda/thing.cu
/usr/local/src/moderngpu/src/mgpucontext.cu
/usr/local/src/moderngpu/src/mgpuutil.cpp
)
TARGET_INCLUDE_DIRECTORIES(thing PUBLIC ../include ${CMAKE_CURRENT_SOURCE_DIR})
TARGET_INCLUDE_DIRECTORIES(thingKernels PUBLIC ../include ${CMAKE_CURRENT_SOURCE_DIR})
作为参考,这是有效的Makefile
ifndef CUDA_VERSION
CUDA_VERSION = 8.0
endif
ifndef COMPUTE_CAPABILITY
COMPUTE_CAPABILITY = 61
endif
CPP_COMPILER = g++
CPP_FLAGS = -pthread -fno-strict-aliasing -g -O2 -g -fwrapv -O3 -Wall -fPIC
CUDA_COMPILER = nvcc -m64
CUDA_FLAGS = -Xcompiler -pthread,-fno-strict-aliasing,-g,-O2,-g,-fwrapv,-O3,-Wall,-fPIC
CUDA_FLAGS += -gencode arch=compute_$(COMPUTE_CAPABILITY),code=sm_$(COMPUTE_CAPABILITY)
CUDA_INCLUDE = /usr/local/cuda-$(CUDA_VERSION)/include
MGPU_INCLUDE = /usr/local/src/moderngpu/include
INCLUDE_FLAGS = -I$(CUDA_INCLUDE) -I$(MGPU_INCLUDE)
CUDA_LIB = /usr/local/cuda-$(CUDA_VERSION)/lib64
LINK_FLAGS = -L$(CUDA_LIB)
MGPU_SRC = /usr/local/src/moderngpu/src
TARGETS = thing
all: $(TARGETS)
thing: thing.co | mgpuutil.o mgpucontext.o
$(CPP_COMPILER) $(CPP_FLAGS) $< $(LINK_FLAGS) -lcudart -o $@ $|
%.co: %.cu
$(CUDA_COMPILER) $(CUDA_FLAGS) $(INCLUDE_FLAGS) $(LINK_FLAGS) -o $@ -c $<
mgpucontext.o: $(MGPU_SRC)/mgpucontext.cu
$(CUDA_COMPILER) $(CUDA_ARGS) $(INCLUDE_FLAGS) -o $@ -c $<
mgpuutil.o: $(MGPU_SRC)/mgpuutil.cpp
$(CUDA_COMPILER) $(CUDA_ARGS) $(INCLUDE_FLAGS) -o $@ -c $<
clean:
rm -f *.o *.co $(TARGETS)
答案 0 :(得分:1)
我编写了插件CLion CUDA Run Patcher ,以便在CLion中为此错误提供临时修复,直到JetBrains提供CPP-10292中跟踪的正确修复。
插件是必要的,因为CLion忽略了CMake中的任何目标名称,包括目标属性OUTPUT_NAME
指定的名称。
在CPP-866跟踪CLION获得完整的CUDA支持之前,这个问题很可能无法解决。