1

I'm attemping to follow this article to be able to code in c++ on MacOS, but my code isn't running? Or the output isn't going into the terminal.

I've gotten to the "Run helloworld.cpp" step in the article and have chosen "C/C++: clang++ build and debug active file." When I run the code, it brings me to the debug console and says main is asking for permission to access files in my Desktop folder.

Here is my tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-fdiagnostics-color=always",
                "-std=c++17",
                "-stdlib=libc++",
                "-g",
                "-pedantic-errors",
                "-Wall",
                "-Weffc++",
                "-Wextra",
                "-Wsign-conversion",
                "-Werror",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Build finished successfully.

  • 2
    Did you grant the permission? Also, the flag `-stdlib=libc++` is unnecessary. Macs use libc++ by default. – sweenish Jan 09 '23 at 17:18
  • It looks like the run code button only compiled the code and did not execute it. From a drop-down button next to it select "Run Code" and you should be able to see the output. A simpler way would be executing the `./main` binary directly. – Sahil Tah Jan 09 '23 at 19:38
  • I granted the permission, but it didn't work. I removed the flag from my tasks.json. @SahilTah For some reason, the drop-down button only shows a "Debug C/C++ File" and "Run C/C++ File" button. There is no "Run Code" only. How do I execute the ./main binary directly? I'm fairly new to coding. Thanks for the help! – Michel Liao Jan 10 '23 at 01:08
  • @MichelLiao I'd suggest you refer to a few videos on youtube on how people set up their vscode for c++. As for compiling and execution, cmd+j (or right-click on the file name and open the integrated terminal) should open a terminal and use the g++ command for compiling and then the execution. I could give you the command directly but understand the basic usage of these commands first. `g++ helloworld.cpp -o helloworld` and then `./helloworld`. – Sahil Tah Jan 10 '23 at 20:10
  • Does this answer your question? [How can I compile and run C/C++ code in a Unix console or Mac terminal?](https://stackoverflow.com/questions/221185/how-can-i-compile-and-run-c-c-code-in-a-unix-console-or-mac-terminal) – Sahil Tah Jan 10 '23 at 20:11

0 Answers0