简体   繁体   中英

Running a C++ Program on Mac in Visual Studio Code

i am trying to run:

    #include <iostream>

int main() {
    std::cout << "Hello World!";
    return 0;
}

on my Mac. I installed C++ and Code Runner in Visual Studio Code. But I get the following error:

[Running] cd "/Users/NAME/Documents/Program/C++/" && g++ test.cpp -o test && "/Users/NAME/Documents/Program/C++/"test Undefined symbols for architecture x86_64:   "_main", referenced from:
     implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

[Done] exited with code=1 in 0.081 seconds

To build and run the helloWorld.cpp or any other project you need to create the build setting first.

Considering you already created the helloworld.cpp file, then follow the steps as below:

  1. you'll create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the Clang C++ (or g++ if you want to build with gcc) compiler to create an executable file from the source code.
  2. It's important to have helloworld.cpp open in the editor because the next step uses the active file in the editor as context to create the build task in the next step.
  3. From the main menu, choose Terminal > Configure Default Build Task. A dropdown will appear listing various predefined build tasks for the compilers that VS Code found on your machine. Choose C/C++ clang++ build active file (or g++ if you want to build with gcc) to build the file that is currently displayed (active) in the editor.
  4. This will create a tasks.json file in the .vscode folder and open it in the editor.

My tasks.json for example looks like this (I used g++)

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-std=c++17",
                "-stdlib=libc++",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
  1. Go back to helloworld.cpp . Because we want to build helloworld.cpp it is important that this file be the one that is active in the editor for the next step.
  2. To run the build task that you defined in tasks.json , press ⇧⌘B or from the Terminal main menu choose Run Build Task.
  3. Create a new terminal using the + button and you'll have a new terminal with the helloworld folder as the working directory. Run ls and you should now see the executable helloworld along with the debugging file (helloworld.dSYM).
  4. You can run helloworld in the terminal by typing ./helloworld

Note: it's a short overview of what you see in the official documentation here . you can replace clang with gcc to compile and build with gcc.

Terminal output:

  • clang++
xecuting task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/Projects/test/helloworld.cpp -o /Users/Projects/test/helloworld <
  • g++
Executing task: /usr/bin/g++ -std=c++17 -stdlib=libc++ -g /Users/Projects/test/helloworld.cpp -o /Users/Projects/test/helloworld <

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM