Clion +VS2010 +CUDA7.5 +Cmake3.23.4 成功编译CUDA程序【成功进入断点在release下】
项目结构:
根目录的CMakeLists.txt文件
# CMakeLists.txt for G4CU project
project(project)
# required cmake version
cmake_minimum_required(VERSION 2.8)
add_subdirectory(cuda)
set (EXTRA_LIBS ${EXTRA_LIBS} gpu)
#release生成调试信息
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi /Od")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
ADD_EXECUTABLE(project main.cpp)
target_link_libraries (project ${EXTRA_LIBS})
main.cpp
#include <stdio.h>
#include <iostream>
extern "C"
void useCUDA();
int main()
{
std::cout<<"Hello C++"<<std::endl;
std::cout<<"Hello C++"<<std::endl;
std::cout<<"Hello C++"<<std::endl;
std::cout<<"Hello C++"<<std::endl;
int aaa = 1000;
int bbb = 20000;
float c = 1.f * aaa/bbb;
std::cout<<"Hello C++"<<std::endl;
useCUDA();
return 0;
}
cuda\CMakeLists.txt
# CMakeLists.txt for G4CU project
project(gpu)
# required cmake version
cmake_minimum_required(VERSION 2.8)
# packages
find_package(CUDA)
#include_directories ("${PROJECT_SOURCE_DIR}")
# nvcc flags -g for debug
#set(CUDA_NVCC_FLAGS -O3;-G;-g)
#set(CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20;-G;-g)
#set(CUDA_NVCC_FLAGS -gencode arch=compute_52,code=sm_52;-G;-g)
file(GLOB_RECURSE CURRENT_HEADERS *.h *.hpp *.cuh)
file(GLOB CURRENT_SOURCES *.cpp *.cu)
source_group("Include" FILES ${CURRENT_HEADERS})
source_group("Source" FILES ${CURRENT_SOURCES})
#cuda_add_library(gpu SHARED ${CURRENT_HEADERS} ${CURRENT_SOURCES})
cuda_add_library(gpu STATIC ${CURRENT_HEADERS} ${CURRENT_SOURCES})
cuda\foo.cu
#include "foo.cuh"
#define CHECK(res) { if(res != cudaSuccess){printf("Error :%s:%d , ", __FILE__,__LINE__); \
printf("code : %d , reason : %s \n", res,cudaGetErrorString(res));exit(-1);}}
__global__ void foo()
{
printf("CUDA!\n");
}
void useCUDA()
{
foo<<<1,5>>>();
CHECK(cudaDeviceSynchronize());
}
cuda\foo.cuh
#ifndef FOO_CUH
#define FOO_CUH
#include <stdio.h>
extern "C"
void useCUDA();
#endif
配置Toolchains
配置Cmake
编译 / 加载Cmake项目
编译/ build
输出
2024年8月15日14:51:10
复现成功
新的需求:能否在release模式下进入debug模式?
能否在Release模式下进入 debug模式?
- 可以的:
# CMakeLists.txt for G4CU project
project(project)
# required cmake version
cmake_minimum_required(VERSION 2.8)
add_subdirectory(cuda)
set (EXTRA_LIBS ${EXTRA_LIBS} gpu)
#release生成调试信息
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi /Od")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
ADD_EXECUTABLE(project main.cpp)
target_link_libraries (project ${EXTRA_LIBS})
在cmake文件里,写的set和 IDE这里设置是等价的:
-DCMAKE_CXX_FLAGS_RELEASE:STRING="/Zi /Od" -DCMAKE_EXE_LINKER_FLAGS_RELEASE:STRING="/DEBUG /OPT:REF /OPT:ICF" -DCMAKE_SHARED_LINKER_FLAGS_RELEASE:STRING="/DEBUG /OPT:REF /OPT:ICF"