The main thing is to modify setup.py in ext_modules
modular
Respectively in extra_compile_args
and extra_link_args
Add ’-g’, It is expressed here as debug Pattern , If you compile at this time , Back in c++ After the code, it will jump continuously , The reason is because c++ It will be optimized automatically when compiling , So we need to gcc Don't let it optimize when compiling , Therefore, you need to add ’-O0’, This will be correct debug c or c++ Code. .
After modification, the code is
ext_modules = [
Pybind11Extension("sampler_core",
['sampler_core.cpp'],
extra_compile_args = ['-fopenmp','-g','-O0'],
extra_link_args = ['-fopenmp','-g'],),
]
Then recompile it
First in vscode Install plug-in on C/C++ Extension Pack
, And then put launch.json Revised as follows
{
// Use IntelliSense Learn about properties .
// Hover to see the description of an existing property .
// For more information , Please visit : https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "/home/amax/anaconda3/envs/torch/bin/python", //-- Modify this column for your execution pytorch Of python route
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false
}
]
}
stay python call C++ Add breakpoints to your code ( You can also use it pdb In the code pdb.set_trace()), Use
Use the first method in the web page to solve
*(type(*)[size])array_name
Not yet ,zz Students should try to succeed