本文共 1872 字,大约阅读时间需要 6 分钟。
在Linux系统下使用Visual Studio Code(Vscode)运行C++程序,可以按照以下步骤操作:
新建项目文件夹:在桌面或其他存储设备上新建一个文件夹,例如“test”。
安装Vscode并打开文件夹:安装完成后,打开Vscode,通过菜单栏选择“文件” -> “打开文件夹”,找到并打开“test”文件夹。
创建.vscode文件夹:右键点击“test”文件夹,选择“新建” -> “文件夹”,创建名为“.vscode”的文件夹。
添加launch.json和tasks.json文件:
{ "version": "0.2.0", "configurations": [ { "name": "C/C++", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "preLaunchTask": "compile", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ]}{ "version": "2.0.0", "tasks": [ { "label": "Build", "type": "shell", "command": "g++", "args": [ "'-Wall'", "'-std=c++17'", "'${file}", "-o", "'${fileBasenameNoExtension}.exe'" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": [ "$gcc" ] } ]}添加C++代码文件:在项目文件夹中新建一个名为“hello.cpp”的文件,内容如下:
#includeusing namespace std;int main() { cout << "Hello, World!" << endl; return 0;}
编译和运行程序:
Ctrl + Shift + B 或菜单栏中的“构建”按钮,Vscode会根据tasks.json中的配置编译程序。如果编译成功,终端会显示“Build completed”信息。F5,程序会在终端中运行并显示输出结果。通过以上步骤,您可以在Linux系统下使用Vscode成功运行C++程序。遇到问题时,检查文件路径、编译器安装和权限设置,确保所有配置正确无误。
转载地址:http://kefg.baihongyu.com/