Mac M1 with Vscode C++ environment


Preface

提示:本教程默认电脑安装Vscode 和 clang++

About whether clang++ is installed on the computer

Keyboard command + space and enter ter in the search box to select the terminal.

clang++ --version

The following figure appears and clang++ is installed locally.

Insert image description here


1. Open VSCode and create a new folder

Create three folders in the root directory

  • target stores compiled files
  • src stores source code, that is, .cpp suffix file
  • inc stores header files, which are files with .h suffix.

提示: 目录结构一定建立要正确
Insert image description here

2. Install the plug-in

  • Install and search for C/C++ as shown in the figure

Insert image description here

  • Install code runner

Insert image description here

  • Install CodeLLDB

Insert image description here

3. Generate configuration files

  • Create hello.cpp file under src
    Insert image description here
  • Paste the following code in hello.cpp
#include <iostream>
using namespace std;

int main()
{
    
    

    cout<<"hello world"<<endl;
    return 0;
} 
  • Generate task.json
  1. Click the title button

Insert image description here
2. Click clang++ to generate the active file.
Insert image description here
3. You will create the .vscode file yourself to generate task.json.
Insert image description here
Modify the task.json parameters.主要能够编译多文件 也就能够引入自定义头文件

Before modification
Insert image description here
After modification

"-g",
"${fileDirname}/*.cpp",
"-o",
"${workspaceFolder}/target/${fileBasenameNoExtension}"

Insert image description here

  • Generate launch.json file
  1. Click as shown in the picture
    Insert image description here
    to modify launch.json
"program": "${workspaceFolder}/${fileBasenameNoExtension}",

Insert image description here

  • Create c_cpp_properties.json.
    Press shaift + command + p (⇧⌘P) and click Edit Con...
    Insert image description here

  • Set the code runner
    to create settings.json in .vacode

{
    
    
    "C_Cpp.errorSquiggles": "disabled",
    "files.associations": {
    
    
        "__locale": "c",
        "__string": "c",
        "string": "c",
        "string_view": "c",
        "cstring": "c",
        "locale": "c",
        "max.h": "c",
        "ostream": "cpp"
    },
    "code-runner.runInTerminal": false
}

Then set it up as shown in the figure
Insert image description here
Insert image description here
覆盖掉原来 cpp

"cpp": "cd $dir && g++ *.cpp -o $workspaceRoot/target/$fileNameWithoutExt && $workspaceRoot/target/$fileNameWithoutExt",

Insert image description here

At this point, all configurations are completed. Next, run

Four, run

  • Run the code you just pasted in hello.cpp, right-click -> RunCode

Insert image description here
这里需要注意 结果 输出 这个不能写也就是程序有输入代码不可以 如果要输入东西 在 .vscode -> settings.json 修改 "code-runner.runInTerminal": true 即可

Test custom header files

  • Create the swap.h file in the inc folder
#include <iostream>
using namespace std;

void swap(int a,int b);

Insert image description here

  • Create swap.cpp under src
#include "../inc/swap.h"
void swap(int a,int b){
    
    
    int temp = a;
    a = b;
    b = temp;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
}

Insert image description here

  • Modify hello.cpp
#include <iostream>
#include "../inc/swap.h"
using namespace std;


int main()
{
    
    
    cout << "hello" << endl;
    int a = 10;
    int b = 20;
    swap(a,b);
}

Insert image description here
Right click and run

Test multi-file debugging

Insert image description here
Insert image description here
Finally came inInsert image description here


注意: 如果你同一目录下 多个 .c / .cpp文件里都有 main函数 / std命名空间 要单独建立文件夹里面放目标文件 如图 (这里c截图演示)
Insert image description here

Summarize

OK, you're done. If it helps you, please give it a like. Thank you.

Guess you like

Origin blog.csdn.net/prjh_/article/details/132370892