c++ - 在Visual Studio Code中的Mac上运行C++程序

标签 c++ compiler-errors

我正在尝试运行:

    #include <iostream>

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

在我的Mac上。我在Visual Studio Code中安装了C++和Code Runner。但是我收到以下错误:
[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

最佳答案

要构建并运行helloWorld.cpp或任何其他项目,您需要首先创建构 build 置。

考虑到您已经创建了helloworld.cpp文件,然后执行以下步骤:

  • 您将创建一个task.json文件来告诉VS Code如何构建(编译)程序。该任务将调用Clang C++(如果要使用gcc进行编译,则调用g++)编译器,以从源代码创建可执行文件。
  • 在编辑器中打开helloworld.cpp很重要,因为下一步将使用编辑器中的 Activity 文件作为上下文,以在下一步中创建构建任务。
  • 从主菜单中,选择“终端”>“配置默认构建任务”。将出现一个下拉列表,列出VS Code在您的计算机上找到的编译器的各种预定义构建任务。选择C/C++ clang++ build active file(如果要使用gcc进行编译,则选择g++)来生成当前在编辑器中显示( Activity )的文件。
  • 这将在.vscode文件夹中创建一个task.json文件,并在编辑器中将其打开。

  • 例如,我的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
                }
            }
        ]
    }
    
  • 返回到helloworld.cpp。因为我们要构建helloworld.cpp,所以重要的是,此文件应成为下一步在编辑器中处于 Activity 状态的文件。
  • 要运行您在tasks.json中定义的构建任务,请按⇧⌘B或从终端主菜单中选择“运行构建任务”。
  • 使用+按钮创建一个新终端,您将拥有一个以helloworld文件夹作为工作目录的新终端。运行ls,您现在应该看到可执行文件helloworld和调试文件(helloworld.dSYM)。
  • 您可以通过在终端中输入./helloworld
  • 来运行helloworld。

    注意:这是对您在官方文档here中看到的内容的简短概述。您可以将glang替换为clang以进行gcc编译和构建。

    终端输出:
  • 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 <
    

    关于c++ - 在Visual Studio Code中的Mac上运行C++程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60745519/

    相关文章:

    python - Nuitka编译python时出现 fatal error LNK1112

    module - 如何包含工作?

    c++ - 实体不是球体的模糊底

    c++ - 如何检查端口是否处于 TIME_WAIT 状态或已被另一个应用程序占用

    c++ - 为什么这个成员变量没有被正确初始化?

    c++ - 将 'this' 指针作为 LPARAM 传递

    c - 错误 : too few arguments to function `printDay' (C language)

    java - 按钮 ActionPerfomed 将字符串从 JList 移动到 JList

    c++ - STL中的优先队列

    c++ - 为什么我能够创建一个复制构造函数并重载 QObject 子类的赋值运算符?