一、vsCode使用cuda
前情提要:配置好mingw:
1.安装cuda
参考:
**CUDA Toolkit安装教程(Windows):**https://blog.csdn.net/qq_42951560/article/details/116131410
2.在vscode中添加includePath
c_cpp_properties.json添加 cuda的include路径
"includePath": [
"${workspaceFolder}/**",
"C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.1\\include\\**"
],
{
"configurations": [
{
"name": "windows-clang-x64",
"includePath": [
"${workspaceFolder}/**",
"C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.1\\include\\**"
],
"compilerPath": "E:/C-software/vs2015/VC/bin/cl.exe",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "${default}",
"compilerArgs": [
""
]
}
],
"version": 4
}
3.配置run的命令
Code Runner扩展打开,点击扩展设置,进入到setting.json中
给code-runner.executorMap
添加
"code-runner.executorMap": {
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -std=c++14 -I E:\\C-software\\VScode\\MinGW\\mingw64\\include -lopencv_core452 -lopencv_imgcodecs452 -lopencv_imgproc452 -lopencv_calib3d452 -lopencv_dnn452 -lopencv_features2d452 -lopencv_flann452 -lopencv_gapi452 -lopencv_highgui452 -lopencv_ml452 -lopencv_objdetect452 -lopencv_photo452 -lopencv_stitching452 -lopencv_video452 -lopencv_videoio452 && $dir$fileNameWithoutExt",
"cu": "cd $dir; nvcc $fileName -o $fileNameWithoutExt.exe -I'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.1\\include' && $dir$fileNameWithoutExt"
}
上面中的 cpp 添加的是opencv运行时的参数
cu 添加的是cuda运行时的参数
4.测试
使用cuda编程,文件名后缀为.cu
测试代码:hello.cu
#include <stdio.h>
#include <cuda.h>
__global__ void hello()
{
printf("hello \n");
}
int main() {
hello<<<1,4 >>>();
cudaDeviceSynchronize();
return 1;
}
5.可能出现的问题
报错:报错nvcc fatal : Cannot find compiler 'cl.exe' in PATH
参考:https://blog.csdn.net/qq_37960402/article/details/89022534
一般cl.exe的地址是在:你安装的Visual Studio \VS\VC\bin\amd64里面,如果是默认安装的话,这个位置就在C:\Program Files\Microsoft Visual Studio***\VC\bin\amd64
我本机的路径:E:\C-software\vs2015\VC\bin\amd64
,把这个路径添加到环境变量中
二、vscode使用第三方库配置
参考:https://www.cnblogs.com/ttyyou/p/13780718.html
https://blog.csdn.net/mantou_riji/article/details/123278824
-
C_Cpp_Properties.json是用于配置编译器环境的,包括启动器代号、位数(这些是自定义的)、编译选项、启动设置、编译模式等。
includePath指向C/C++标准库、用户头文件所在位置。
不需要CMake也可以直接编写C/C++ -
launch.json
是vscode用于调试的配置文件,比如指定调试语言环境,指定调试类型等等。我们第一次运行程序的时候,就可以选择让vscode自动帮我们创建出来。我们这里以调试node.js环境为例。- 点击debug时会有一个create a launch.json的链接。
-
tasks.json
(1)作用:tasks.json文件告诉VSCode代码如何编译代码
(2)位置:终端->配置任务 可以打开tasks.json -
launch.json
(1)作用:launch.json 是用于运行和调试的配置文件 -
https://zhouyuming.blog.csdn.net/article/details/105442498?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-105442498-blog-123593009.pc_relevant_multi_platform_whitelistv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-105442498-blog-123593009.pc_relevant_multi_platform_whitelistv2&utm_relevant_index=2
参考:https://blog.csdn.net/Wannna/article/details/105109375?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-105109375-blog-88796413.pc_relevant_aa2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-105109375-blog-88796413.pc_relevant_aa2&utm_relevant_index=1
https://blog.csdn.net/baidu_38634017/article/details/99875321
1.配置c_cpp_properties.json文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceRoot}",
"F:\\Project\\SP++3.0\\SP++3.0\\include" /*此处设置为库的include目录下*/
],
"browse": {
"path": [
"${workspaceRoot}",
"F:\\Project\\SP++3.0\\SP++3.0\\lib" /*此处设置为库的lib目录下*/
]
},
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\mingw64\\bin\\g++.exe", /*此处配置你的编译器路径,没有下载的小伙伴,请百度mingw的安装*/
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
2.配置launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false, /*此处设置是否使用外部终端,默认为false(使用内置的终端),如需要可以修改为false*/
"MIMode": "gdb",
"miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe", /*此处设置为你编译器的dbg.exe路径*/
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
3.配置tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "D:\\mingw64\\bin\\g++.exe", /*此处设置为你的编译器路径*/
"args": [
"-g",
"${file}",
"-L'F:\\Project\\SP++3.0\\SP++3.0\\lib'", /*注意,此行指定库的lib目录,默认没有*/
"-I'F:\\Project\\SP++3.0\\SP++3.0\\include'", /*此处指定库的include目录,默认没有*/
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\mingw64\\bin" /*此处设置你编译器路径的bin目录下*/
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
4.需要注意的是:
1.VScode只是个编译器,并没有自带编译器,所以需要网上自行下载编译器。本文采用的是mingw编译器,需要的小伙伴可以自行百度。
2.本文采用的库是SP++库(别人写的C++信号处理库)为例,其他的库文件可以仿照设置。
3.需要设置的地方我均在代码中用注释的形式标注出来,使用时请将我写的注释删掉(文中为了指出需要设置的地方,才加了此注释)。
4.实际上也可以在vscode的terminal(终端)中使用命令直接运行,不用配置文件,但是为了方便使用,建议配置好文件,方便进行debug之类的操作。可以在命令行使用下列命令进行编译(生成exe二进制文件)(-L指定lib目录,-I指定include目录,xxx.cpp是你需要进行编译的文件,xxx.exe是你要编译的文件的名称)
g++ xxx.cpp -L’xxx\lib’ -I’xxx\include’ -o xxx.exe
1
然后通过下列命令运行生成的二进制文件:
./xxx.exe