tasks.json配置
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build1",
"command": "D:\\software\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"${workspaceFolder}/rail_class/*",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\software\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
}
],
"version": "2.0.0"
}
launch.json配置
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\software\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build1"
}
]
}
代码结构
图片.png
代码
main.cpp
#include "rail_class/rail_class.hpp"
int main() {
int i = 0;
RAIIClass rail;
i++;
system("pause");
return 0;
}
rail_class.hpp
#ifndef RAIL_CLASS_HPP
#define RAIL_CLASS_HPP
#include <iostream>
#include <windows.h>
using namespace std;
class RAIIClass {
public:
RAIIClass();
~RAIIClass();
private:
CRITICAL_SECTION mCriticalSection;
};
#endif
rail_class.cpp
#include "rail_class.hpp"
RAIIClass::RAIIClass() {
InitializeCriticalSection(&this->mCriticalSection);
EnterCriticalSection(&this->mCriticalSection);
}
RAIIClass::~RAIIClass() {
LeaveCriticalSection(&this->mCriticalSection);
DeleteCriticalSection(&this->mCriticalSection);
std::cout << "删除 RAIIClass~" << std::endl;
}
网友评论