|
| 1 | +# Installing VSCode on Mac OSX |
| 2 | + |
| 3 | +- Navigate to [Visual Studio Code](https://code.visualstudio.com/) |
| 4 | +- Download latest version (*I am using --Version: 1.60.1*) |
| 5 | + - Open with **Archive Utility** |
| 6 | +- Drag **Visual Studio Code** into **Applications folder** |
| 7 | + |
| 8 | +## Installing Extensions |
| 9 | + |
| 10 | +1. C/C++ |
| 11 | +p.s. This [link](https://code.visualstudio.com/docs/languages/cpp#_install-the-extension) could be more useful. |
| 12 | + - Launch VSCode |
| 13 | + - Select the Extensions view icon, bottom, on the Activity bar, on the far left |
| 14 | + - Search for `c++` |
| 15 | + - Select the one supported by Microsoft (purple circle) and `install` |
| 16 | +2. CodeLLDB |
| 17 | + - Search for `codelldb` |
| 18 | + - Select the only one supported by Vadim Chugunov and `install` |
| 19 | + |
| 20 | +# Building and Running C++ Programs with VSCode on Mac OSX |
| 21 | + |
| 22 | +## Configure json files |
| 23 | +- Launch VSCode |
| 24 | +- Select `Open Folder` |
| 25 | +- Select the folder where you want to edit and compile your C++ codes |
| 26 | +- Copy below codes and save file as `main.cpp` for example |
| 27 | + |
| 28 | +`` |
| 29 | +#include <iostream> |
| 30 | +using namespace std; |
| 31 | + |
| 32 | +int main() { |
| 33 | + cout << "Hello!" << endl; |
| 34 | + return 0; |
| 35 | +} |
| 36 | +`` |
| 37 | + |
| 38 | +- Select `View` at the Mac menu Bar, then `Command Palatte...` |
| 39 | +- Select `C/C++: Edit Configurations (UI)` |
| 40 | +- Modify **Compiler path** by clicking on the drop down arrow and select `/usr/bin/g++` |
| 41 | +- Modify **C++ standard** to `c++20` |
| 42 | +- Save... Notice `.vscode` > `c_cpp_properties.json` is automatically generated on the Activity bar |
| 43 | + |
| 44 | +VSCode require the `.cpp` file to be selected prior building it |
| 45 | + |
| 46 | +- Select the `main.cpp` just created |
| 47 | +- Select `Terminal` at the Mac menu Bar, then `Configure Default Build Task...` |
| 48 | +- Select `C/C++: g++ build active file` |
| 49 | +- A new file, `tasks.json`, is automatically generated, which contains information on how to build C++ projects |
| 50 | +- Modify line 8 - 13 as below: |
| 51 | + |
| 52 | +`` |
| 53 | +"args": [ |
| 54 | + "-g", |
| 55 | + "-Wall", // to generate all warnings |
| 56 | + "-std=c++20", // use c++20 standard |
| 57 | + // "${file}", |
| 58 | + "${fileDirname}/*.cpp", // compile all c++ files |
| 59 | + "-o", |
| 60 | + "${fileDirname}/${fileBasenameNoExtension}" |
| 61 | +], |
| 62 | +`` |
| 63 | + |
| 64 | +## Building C++ programs |
| 65 | + |
| 66 | +- Select `main.cpp` and let's compile the code |
| 67 | +- Select `Terminal` at the Mac menu bar, then `Run Build Task...` |
| 68 | +- Terminal should automatically appear towards the bottom and expected output |
| 69 | + |
| 70 | +`` |
| 71 | +> Executing task: C/C++: g++ build active file < |
| 72 | +
|
| 73 | +Starting build... |
| 74 | +/usr/bin/g++ -g -Wall -std=c++20 /Users/.../*.cpp -o /Users/.../main |
| 75 | +Build finished successfully. |
| 76 | + |
| 77 | +Terminal will be reused by tasks, press any key to close it. |
| 78 | +`` |
| 79 | + |
| 80 | +- Notice `Build finished successfully` then the `main` executable file is created on the Activity bar |
| 81 | + |
| 82 | +## Running C++ Programs |
| 83 | + |
| 84 | +- Right click on the `main` executable file and select `Open in Integrated Terminal` |
| 85 | +- The terminal will then open at the proper file path, and input command `ls` will list the directories under the file path. The expected output is |
| 86 | + |
| 87 | +``main main.cpp main.dSYM`` |
| 88 | + |
| 89 | +- To **run** or **execute** the file, input command `./main` |
| 90 | + |
| 91 | +## Running multiple C++ programs |
| 92 | +According to my file structure, which I have `test1` and `test2` directory with respective `.cpp` files, this is to avoid build error |
| 93 | + |
| 94 | +`` |
| 95 | +clang: error: no such file or directory: 'test2.cpp' |
| 96 | +clang: error: no input files |
| 97 | +`` |
| 98 | + |
| 99 | +Despite the `.cpp` file name is different, I learned that there will be buile errors if `tes1.cpp` and `test2.cpp` are in the same directory during build. |
0 commit comments