1. Windows system c++ environment configuration
1.1 Configure the MinGw compiler
(1) Download mingw64
According to the package of mingw64, I have put it on the Baidu network disk, and you can download it by yourself:
Link: https://pan.baidu.com/s/1NoPGAYFuP5ysXTf8wtvbEA?pwd=wd6w Extraction code: wd6w
(2) Configure environment variables
Unzip the mingw64.zip
downloaded file, find the path where the decompressed bin
file is located
, and then add the path where the bin is located, as I have here, D:\install\mingw64\bin
to the system environment variable.
(3) Verify whether the installation is successful Enter or
in the cmd window of the command prompt , if the version number is displayed, the installation is successfulgcc -v
gcc --version
1.2 Configure C/C++ environment
The C++ environment is configured through c_pp_propertoes.json
, launch.json
and three files.tasks.json
Method 1 Use tools to automatically configure
This article introduces a very simple environment configuration method, which vscode c++配置器
can be realized with the help of the ones provided on the Internet.
-
Download the installation package at https://v4.vscch.tk/
-
Unzip the installation package, and click
vscch.exe
to follow step by step; the installation is very simple, just follow the default installation method.
This tool can automatically identify the installation location of the MinGW compiler. If it is not added to the environment variable, it will automatically help configure it to the environment variable.
-
If MinGW is not downloaded, it will prompt you to download it, and then help to configure environment variables automatically.
-
c_pp_propertoes.json
After the installation is complete, the configured ,launch.json
andtasks.json
, andhelloworld.cpp
test codes will be automatically generated under the working path you choose . It can run and test whether the c++ environment is configured successfully:
注意:
according to the automatic generationjson配置
, it is only valid to write c++ code during installation指定的工作目录下
, so the following is done,修改
so that the 3 json can be copied to other directories, and the c++ program can also be run and debugged. willsingle file build
be changed tobuild
launch.json
The modification is as follows:
tasks.json
The modification is as follows:
Method 2 Create 3 json files directly
Create a directory on the project directory .vscode
and create c_cpp_properties.json
, launch.json
, tasks.json
, three json files according to the configuration I provided.
(1)c_cpp_properties.json
{
"configurations": [
{
"compilerPath": "D:\\install\\mingw64\\bin\\g++.exe",
"cppStandard": "c++17",
"includePath": [
"${
{workspaceFolder}}/**"
],
"intelliSenseMode": "windows-gcc-x64",
"name": "Win32"
}
],
"version": 4
}
(2) launch.json
{
"configurations": [
{
"MIMode": "gdb",
"args": [],
"cwd": "${fileDirname}",
"env": {
"PATH": "D:\\install\\mingw64\\bin;${env:PATH}"
},
"environment": [],
"externalConsole": true,
"internalConsosleOptions": "neverOpen",
"miDebuggerPath": "D:\\install\\mingw64\\bin\\gdb.exe",
"name": "build",
"preLaunchTask": "build",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"request": "launch",
"stopAtEntry": false,
"type": "cppdbg"
}
],
"version": "0.2.0"
}
(3) tasks.json
{
"options": {
"env": {
"Path": "D:\\install\\mingw64\\bin;${env:Path}"
}
},
"tasks": [
{
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++17"
],
"command": "D:\\install\\mingw64\\bin\\g++.exe",
"group": {
"isDefault": true,
"kind": "build"
},
"label": "build",
"presentation": {
"clear": true,
"echo": false,
"focus": false,
"panel": "shared",
"reveal": "silent",
"showReuseMessage": false
},
"problemMatcher": "$gcc",
"type": "process"
},
{
"args": [],
"command": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"dependsOn": "single file build",
"label": "run and pause",
"options": {
"env": {
"Path": "D:\\install\\mingw64\\bin;${env:Path}"
}
},
"presentation": {
"clear": true,
"echo": false,
"focus": false,
"panel": "shared",
"reveal": "never",
"showReuseMessage": false
},
"problemMatcher": [],
"type": "pause-console"
}
],
"version": "2.0.0"
}
json
Note: Set the path in these 3 files D:\\install\\mingw64
to your own path
1.3 C/C++ environment test
Write a test helloworld.cpp
, the code is as follows:
// 按下 F6 编译运行。
// 按下 F5 编译调试。
// 按下 Ctrl + Shift + B 编译。
#include <iostream>
int main() {
// 在标准输出中打印 "Hello, world!"
std::cout << "Hello, world!" << std::endl;
}
// 此文件编译运行将输出 "Hello, world!"。
// 按下 F6 后,你将在弹出的终端窗口中看到这一行字。
See the configuration and code I have uploaded to Baidu Netdisk:
Link: https://pan.baidu.com/s/13R79Wxn91Z4G7RCcXpr8fQ?pwd=xe2x
Extraction code: xe2x
Reference: https://zhuanlan.zhihu.com/p/545908287?utm_id=0
2. Linux system c++ environment configuration
2.1 Configuration details
The configuration of the linux system vsocde is basically the same as that of the window system, c_pp_propertoes.json
and launch.json
is configured through 4 tasks.json
files .settings.json
c++环境
(1) settings.json
{
"files.associations": {
"*.cpp": "cpp",
"*.cu": "cuda-cpp",
"deque": "cpp",
"string": "cpp",
"vector": "cpp",
"*.tcc": "cpp",
"__hash_table": "cpp",
"__split_buffer": "cpp",
"__tree": "cpp",
"array": "cpp",
"bitset": "cpp",
"initializer_list": "cpp",
"iterator": "cpp",
"map": "cpp",
"queue": "cpp",
"random": "cpp",
"set": "cpp",
"stack": "cpp",
"string_view": "cpp",
"unordered_map": "cpp",
"utility": "cpp",
"__atomic": "cpp",
"__functional_base": "cpp",
"__functional_base_03": "cpp",
"__tuple": "cpp",
"algorithm": "cpp",
"chrono": "cpp",
"type_traits": "cpp",
"filesystem": "cpp",
"functional": "cpp",
"limits": "cpp",
"memory": "cpp",
"ratio": "cpp",
"tuple": "cpp",
"istream": "cpp",
"ostream": "cpp",
"future": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"atomic": "cpp",
"hash_map": "cpp",
"hash_set": "cpp",
"bit": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"list": "cpp",
"unordered_set": "cpp",
"exception": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"system_error": "cpp",
"fstream": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"mutex": "cpp",
"new": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"ios": "cpp",
"__nullptr": "cpp",
"__bit_reference": "cpp",
"__node_handle": "cpp",
"__locale": "cpp",
"variant": "cpp"
}
}
settings.json
The configuration provided above can be used without modification.
(2)c_cpp_propertoes.json
c_cpp_propertoes.json:
Configure options for c++ compilation, including compiler paths, C/C++ annotations, and specify search paths for header files (such as opencv, etc.)
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/trt8cuda112cudnn8/include/**",
"/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/cpp-packages/opencv4.2/include/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++11",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
Configure includePath
"includePath": [
"${workspaceFolder}/**",
"/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/trt8cuda112cudnn8/include/**",
"/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/cpp-packages/opencv4.2/include/**"
],
includePath
: Set the search path of the header file, so that the compiler can find the corresponding header file.
- The first item:
"${workspaceFolder}/**"
add the working path of the project as the search path of the header file, this item is added by default - The second item: When configuring TensorRT deployment, you need to rely on it
头文件
, including tensorrt's own, cuda, cudnn, and protobuf header files. (If there is no dependency in the projectTensorRT
,cuda
no configuration is required)
ls /home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/trt8cuda112cudnn8/include/
You can see some dependent header files:
cd to one of them such as cuda, you can see in detail what cuda contains.h文件
- The third item: Configure the dependent opencv header files. (If the project does not depend on opencv, no configuration is required)
Configure the gcc compiler path
"compilerPath": "/usr/bin/gcc" # 设置gcc编译器即可,不需要设置g++
Through ls /usr/bin
, you can see gcc, g++
that the compilers are all in this directory
Specifies the C/C++ language annotation version
"cStandard": "gnu11",
"cppStandard": "gnu++11",
IntelliSense
"intelliSenseMode": "linux-gcc-x64"
(3)tasks.json
tasks.json: 指定 编译时需要执行cmake命令
, and each launch(debug)
time, it will be run first tasks
(here, it will be compiled once).
Therefore, tasks
the following label
names need to be set the same as the parameterslaunch.json
in , for example, both ."preLaunchTask"
build
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make pro -j6"
}
]
}
command
: Execute the cmake command, where ispro
the name of the executable file (makefile
generated specified in可执行文件的名称
),-j6
which means that 6 processes are executed at the same time. If you want to compile faster, you can set the number to be larger.
(4) launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "program-debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/workspace/pro",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/workspace",
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"environment": [
{
"name": "LD_LIBRARY_PATH",
"value": "/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/trt8cuda112cudnn8/lib64:/home/yuanwushui/anaconda3/lib:/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/trt8cuda112cudnn8/py39:/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/cpp-packages/opencv4.2/lib:/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/lib:$LD_LIBRARY_PATH"
}
],
"setupCommands": [
{
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
launch.json
Set options for c++ debug. "configurations"
The following configuration parameters are described below:
program
: Specify the path of the compiled executable filepro
, wherepro
the name of the output executable file is specified in the makefile.
"program": "${workspaceFolder}/workspace/pro",
cwd
: the directory where the executable file is located"externalConsole"
: When running, whether to run on an external console. If setTrue
, it will run in the CMD console (windows), if false, it will run in the console where the compiler is located. The default is setfalse
tomiDebuggerPath
: Specify the path of the debugger (gdb)environment
: Configure environment variables. The environment variable name is specifiedLD_LIBRARY_PATH
by the environment variable valuevalue
. LD_LIBRARY_PATH is mainly used to specify what needs to be opened外部库文件的搜索路径
.vlaue
Specify the search path for library files in , separated by:
. If the project needs to depend ontensorrt
,cuda
,anaconda
,opencv
, you need to add these dependent library files. Note that value needs to:
be separated by the search paths of different libraries, and finally needs to:$LD_LIBRARY_PATH
end with .
"environment": [
{
"name": "LD_LIBRARY_PATH",
"value": "/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/trt8cuda112cudnn8/lib64:/home/yuanwushui/anaconda3/lib:/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/trt8cuda112cudnn8/py39:/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/cpp-packages/opencv4.2/lib:/home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/lib:$LD_LIBRARY_PATH"
}
],
setupCommands
: Set printing options, such as beautifying the print output, just keep the default, no need to modify
"setupCommands": [
{
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
preLaunchTask
: Needed before Launch (debug)依赖的task任务
,注意
it needs to be the same as the name of the task settingtasks.json
in , for example, both are build . By setting this option, there is no need to manually compile the executable file during debugging, and the system will automatically help compile it.label
不然无法调试和编译
preLaunchTask
"preLaunchTask": "build"
2.2 Detailed explanation of makefile
cc := g++
name := pro
workdir := workspace
srcdir := src
objdir := objs
stdcpp := c++11
cuda_home := /home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/trt8cuda112cudnn8
syslib := /home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/lib
cpp_pkg := /home/yuanwushui/anaconda3/lib/python3.9/site-packages/trtpy/cpp-packages #opencv4.2
cuda_arch :=
nvcc := $(cuda_home)/bin/nvcc -ccbin=$(cc)
# 定义cpp的路径查找和依赖项mk文件
cpp_srcs := $(shell find $(srcdir) -name "*.cpp")
cpp_objs := $(cpp_srcs:.cpp=.cpp.o)
cpp_objs := $(cpp_objs:$(srcdir)/%=$(objdir)/%)
cpp_mk := $(cpp_objs:.cpp.o=.cpp.mk)
# 定义cu文件的路径查找和依赖项mk文件
cu_srcs := $(shell find $(srcdir) -name "*.cu")
cu_objs := $(cu_srcs:.cu=.cu.o)
cu_objs := $(cu_objs:$(srcdir)/%=$(objdir)/%)
cu_mk := $(cu_objs:.cu.o=.cu.mk)
# 定义opencv和cuda需要用到的库文件
link_cuda := cudart cudnn
link_trtpro :=
link_tensorRT := nvinfer nvinfer_plugin
link_opencv := opencv_core opencv_imgproc opencv_imgcodecs
link_sys := stdc++ dl protobuf
link_librarys := $(link_cuda) $(link_tensorRT) $(link_sys) $(link_opencv)
# 定义头文件路径,请注意斜杠后边不能有空格
# 只需要写路径,不需要写-I
include_paths := src \
$(cuda_home)/include/cuda \
$(cuda_home)/include/tensorRT \
$(cpp_pkg)/opencv4.2/include \
$(cuda_home)/include/protobuf
# 定义库文件路径,只需要写路径,不需要写-L
library_paths := $(cuda_home)/lib64 $(syslib) $(cpp_pkg)/opencv4.2/lib
# 把library path给拼接为一个字符串,例如a b c => a:b:c
# 然后使得LD_LIBRARY_PATH=a:b:c
empty :=
library_path_export := $(subst $(empty) $(empty),:,$(library_paths))
# 把库路径和头文件路径拼接起来成一个,批量自动加-I、-L、-l
run_paths := $(foreach item,$(library_paths),-Wl,-rpath=$(item))
include_paths := $(foreach item,$(include_paths),-I$(item))
library_paths := $(foreach item,$(library_paths),-L$(item))
link_librarys := $(foreach item,$(link_librarys),-l$(item))
# 如果是其他显卡,请修改-gencode=arch=compute_75,code=sm_75为对应显卡的能力
# 显卡对应的号码参考这里:https://developer.nvidia.com/zh-cn/cuda-gpus#compute
# 如果是 jetson nano,提示找不到-m64指令,请删掉 -m64选项。不影响结果
cpp_compile_flags := -std=$(stdcpp) -w -g -O0 -m64 -fPIC -fopenmp -pthread
cu_compile_flags := -std=$(stdcpp) -w -g -O0 -m64 $(cuda_arch) -Xcompiler "$(cpp_compile_flags)"
link_flags := -pthread -fopenmp -Wl,-rpath='$$ORIGIN'
cpp_compile_flags += $(include_paths)
cu_compile_flags += $(include_paths)
link_flags += $(library_paths) $(link_librarys) $(run_paths)
# 如果头文件修改了,这里的指令可以让他自动编译依赖的cpp或者cu文件
ifneq ($(MAKECMDGOALS), clean)
-include $(cpp_mk) $(cu_mk)
endif
$(name) : $(workdir)/$(name)
all : $(name)
run : $(name)
@cd $(workdir) && ./$(name) $(run_args)
$(workdir)/$(name) : $(cpp_objs) $(cu_objs)
@echo Link $@
@mkdir -p $(dir $@)
@$(cc) $^ -o $@ $(link_flags)
$(objdir)/%.cpp.o : $(srcdir)/%.cpp
@echo Compile CXX $<
@mkdir -p $(dir $@)
@$(cc) -c $< -o $@ $(cpp_compile_flags)
$(objdir)/%.cu.o : $(srcdir)/%.cu
@echo Compile CUDA $<
@mkdir -p $(dir $@)
@$(nvcc) -c $< -o $@ $(cu_compile_flags)
# 编译cpp依赖项,生成mk文件
$(objdir)/%.cpp.mk : $(srcdir)/%.cpp
@echo Compile depends C++ $<
@mkdir -p $(dir $@)
@$(cc) -M $< -MF $@ -MT $(@:.cpp.mk=.cpp.o) $(cpp_compile_flags)
# 编译cu文件的依赖项,生成cumk文件
$(objdir)/%.cu.mk : $(srcdir)/%.cu
@echo Compile depends CUDA $<
@mkdir -p $(dir $@)
@$(nvcc) -M $< -MF $@ -MT $(@:.cu.mk=.cu.o) $(cu_compile_flags)
# 定义清理指令
clean :
@rm -rf $(objdir) $(workdir)/$(name) $(workdir)/*.trtmodel $(workdir)/*.onnx
@rm -rf $(workdir)/image-draw.jpg $(workdir)/input-image.jpg $(workdir)/pytorch.jpg
# 防止符号被当做文件
.PHONY : clean run $(name)
# 导出依赖库路径,使得能够运行起来
export LD_LIBRARY_PATH:=$(library_path_export)
2.3 Case Description
to be continued