简体   繁体   English

在 Mac 上使用 Visual Studio Code 运行 C++ 程序

[英]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.在我的 Mac 上。 I installed C++ and Code Runner in Visual Studio Code.我在 Visual Studio Code 中安装了 C++ 和 Code Runner。 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.要构建和运行 helloWorld.cpp 或任何其他项目,您需要先创建构建设置。

Considering you already created the helloworld.cpp file, then follow the steps as below:考虑到您已经创建了helloworld.cpp文件,请按照以下步骤操作:

  1. you'll create a tasks.json file to tell VS Code how to build (compile) the program.您将创建一个 tasks.json 文件来告诉 VS Code 如何构建(编译)程序。 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.此任务将调用 Clang C++(或 g++,如果您想使用 gcc 构建)编译器从源代码创建一个可执行文件。
  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.在编辑器中打开 helloworld.cpp 很重要,因为下一步使用编辑器中的活动文件作为上下文来创建下一步中的构建任务。
  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.将出现一个下拉列表,其中列出了 VS Code 在您的机器上找到的编译器的各种预定义构建任务。 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.选择C/C++ clang++ build active file (或者 g++ 如果你想用 gcc 构建)来构建当前在编辑器中显示(活动)的文件。
  4. This will create a tasks.json file in the .vscode folder and open it in the editor.这将在 .vscode 文件夹中创建一个 tasks.json 文件并在编辑器中打开它。

My tasks.json for example looks like this (I used g++)例如,我的tasks.json看起来像这样(我使用了 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 .回到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.因为我们要构建helloworld.cpp所以这个文件是下一步在编辑器中处于活动状态的文件是很重要的。
  2. To run the build task that you defined in tasks.json , press ⇧⌘B or from the Terminal main menu choose Run Build Task.要运行您在tasks.json定义的构建任务,请按 ⇧⌘B 或从终端主菜单中选择运行构建任务。
  3. Create a new terminal using the + button and you'll have a new terminal with the helloworld folder as the working directory.使用 + 按钮创建一个新终端,您将拥有一个以 helloworld 文件夹作为工作目录的新终端。 Run ls and you should now see the executable helloworld along with the debugging file (helloworld.dSYM).运行 ls,您现在应该会看到可执行文件 helloworld 以及调试文件 (helloworld.dSYM)。
  4. You can run helloworld in the terminal by typing ./helloworld您可以在终端中输入 ./helloworld 来运行 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.您可以用 gcc 替换 clang 以使用 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 <

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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