简体   繁体   English

为什么这个 hello world 程序无法链接?

[英]Why is this hello world program failing to link?

#include <stdio.h>
int main() {
   printf("Hello, World!");
   return 0;
}

I installed VScode and gcc on Mac Silicon, tried running the above hello world program in C, but I get the following error:我在 Mac Silicon 上安装了 VScode 和 gcc,尝试在 C 中运行上述 hello world 程序,但出现以下错误:

Undefined symbols for architecture arm64:
  "_main", referenced from:
 implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

When I run the following commands on the terminal I get this:当我在终端上运行以下命令时,我得到了这个:

which gcc
/usr/bin/gcc

which gcc-11
/opt/homebrew/bin/gcc-11

I'm not sure if I installed the compiler incorrectly(first time trying C).我不确定我是否错误地安装了编译器(第一次尝试 C)。

For just starting out, I recommend you just have a simple task like this in tasks.json (Terminal -> Configure Tasks):刚开始时,我建议您在tasks.json (终端 -> 配置任务)中执行类似这样的简单任务:

{
  "tasks": [
    {
      "label": "Run my C",
      "type": "shell",
      "command": "gcc -o myoutput ${file} && ./myoutput",
      "group": {
           "kind": "build",
           "isDefault": true
       },

      }
    }
  ],
"version": "2.0.0"
}

This will compile it and immediately run ./myoutput (inside vscode's terminal).这将编译它并立即运行./myoutput (在 vscode 的终端内)。 isDefault means that it is the default build task so you can run it by Ctrl+Shift+B (also under Terminal -> Run Build Task). isDefault表示它是默认构建任务,因此您可以通过 Ctrl+Shift+B 运行它(也在终端 -> 运行构建任务下)。

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

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